Check out our Angular Book Series.

How do I copy files with Maven?

Last week, I wrote about running a Gulp Script from Maven. The Gulp Script was located in a directory outside of the Java project.

This week I'll show you how to copy the file from the external directory into the Java project.

We're going to use the maven-resources-plugin to make this work.

First, set it up as a plugin in the Maven POM file:


<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>

This adds the artifactId--AKA the plugin name--and the version number. For each set of files we want to copy, we'll need to add an execution block, so start there:


<executions>
<execution>
<id>copy-resources-A5</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>

This specifies the id of the copy, which is just a unique name for the process. It is done on the validation phase. And the goal is to copy-resources.

Before we end the execution block, we need to add the configuration. this will tell us what we are copying and where we are copying it too.


<configuration>
<outputDirectory>${basedir}/src/main/webapp/A5</outputDirectory>
<resources>
<resource>
<directory>C:/Projects/lw/A5/chapter7/Angular5TypeScript/build</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>

The outputDirectory specifies the location of the copied files. I made them relative tot he $(basedir), which is the root of the Java project. In the Angular 5 files in the A5 directory of the Java Project's webapp.

The source directory is listed under resources, resource.

Finally close off the open tags, which is the execution tag, the executions tag, and the plugin tag:


</execution>
</executions>
</plugin>

I'm using this approach to copy the results of an external Gulp script into the final WAR, so that the files will be surfable via the Tomcat instance.

It has been working great for me.

How do I run a Gulp Script in a different directory from Maven?

If you want to run a Gulp Script from Maven, you use the frontend plugin, right? Unfortunately, that seems to assume that the Gulp script is part of the Java project.

I'm creating a Java Backend to the LearnWith series and am sharing UI code between multiple backend technologies. As such, my directory structure is different and the UI code is not in the web root. I want to run a gulp script in a different directory, and then copy the results into the project's webapp directory. How do you do it?

I was able to do it with the exec Maven Plugin.

First, set up the plugin in the plugins portion of Maven POM:


<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>

The groupId, arftifactId, and version are specified. For each script you want to run you'll need an execution statement:


<executions>
<execution>
<id>Build Angular 5</id>
<phase>validate</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>gulp</executable>
<arguments>
<argument>build</argument>
</arguments>
<workingDirectory>C:\Projects\lw\A5\chapter7\Angular5TypeScript</workingDirectory>
</configuration>
</execution>
</executions>

The id is a unique name for the build script. The phase is validate, because it is. The goal should be set to exec. The configuration is where the real magic happens. We tell it to execute the gulp command with the argument 'build', so this will run:


gulp build

We want to make sure that the script runs in the proper directory and that is specified with the workingDirectory tag. Presumably this directory will be outside the current directory structure.

Finally, close the plugin tag:


</plugin>

That's all you need to do to run an external Gulp Script from a Maven project. This assumes, of course, you have Gulp installed. Next week I'm going to write about copying the processed files into the Java project.

How do I copy Multiple Directories with Maven?

I've been playing around with Java since a lot of my clients use it as their service layer to power their Angular applications. As such I'm digging into Maven a common build tool for Java applications.

Java development is more complicated than something like PHP or ColdFusion. With PHP or ColdFusion I can just write a file, throw it in a web directory and load it. Java requires a compilation process, which complicates things a bit. Since I'm building a service layer as part of the Learn With applications, I wanted to copy specific files into my final WAR file. I wanted to copy the front end from the books into the final WAR file, without having to replicate all the UI code in the Java project.

The Maven ">Resource plugin lets me copy files from outside the Java project into the Java project before building the project. Something like this works:


<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/src/main/webapp/your-dir-here</outputDirectory>
<resources>
<resource>
<directory>C:/non-packaged-resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>

This works great to copy non-project files from the resource directory:


<resource>
<directory>C:/non-packaged-resources</directory>
<filtering>true</filtering>
</resource>

This will be external to the project. Into the outputDirectory


<outputDirectory>${basedir}/src/main/webapp/your-dir-here</outputDirectory>

Which is part of the project. Once the files are in the web-app directory they will be compiled into the final WAR file and accessible when that WAR is deployed to a Java server, such as Tomcat.

However, I'm dealing with multiple UI technologies and wanted to copy over all the files. How do I do it? The answer is to put multiple execution blocks inside the tag:


<executions>
<execution>
<id>copy-resources-AJS</id>
<!-- here the phase you need -->
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/src/main/webapp/AJS</outputDirectory>
<resources>
<resource>
<directory>C:/Projects/lw/AJS/chapter1/angularApp</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
<execution>
<id>copy-resources-A4</id>
<!-- here the phase you need -->
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/src/main/webapp/A4</outputDirectory>
<resources>
<resource>
<directory>C:/Projects/lw/A4/chapter1/Angular4TypeScript/build</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
<execution>
<id>copy-resources-A5</id>
<!-- here the phase you need -->
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/src/main/webapp/A5</outputDirectory>
<resources>
<resource>
<directory>C:/Projects/lw/A5/chapter1/Angular5TypeScript/build</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>

Make sure that each execution block has a unique ID, or else Maven will get confused. This copies my Angular 5 source files to:


${basedir}/src/main/webapp/A5

And they can be surfable using "http://localhost:port/A5/"

The Angular 4 source files to:


${basedir}/src/main/webapp/A4

And they can be surfable using "http://localhost:port/A4/"

And the AngularJS files will be put to:


${basedir}/src/main/webapp/AJS

And they can be surfable using "http://localhost:port/AJS/".

That is how you can copy multiple external directories into your Java project using Maven.

All Content Copyright 2005, 2006, 2007, 2008, 2009 Jeffry Houser. May not be reused without permission
BlogCFC was created by Raymond Camden. This blog is running version 5.9.2.002.