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:

view plain print about
1<plugin>
2 <artifactId>maven-resources-plugin</artifactId>
3 <version>3.0.2</version>
4 <executions>
5 <execution>
6 <id>copy-resources</id>
7 <!-- here the phase you need -->
8 <phase>validate</phase>
9 <goals>
10 <goal>copy-resources</goal>
11 </goals>
12 <configuration>
13 <outputDirectory>${basedir}/src/main/webapp/your-dir-here</outputDirectory>
14 <resources>
15 <resource>
16 <directory>C:/non-packaged-resources</directory>
17 <filtering>true</filtering>
18 </resource>
19 </resources>
20 </configuration>
21 </execution>
22 </executions>
23 </plugin>

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

view plain print about
1<resource>
2 <directory>C:/non-packaged-resources</directory>
3 <filtering>true</filtering>
4 </resource>

This will be external to the project. Into the outputDirectory

view plain print about
1<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:

view plain print about
1<executions>
2 <execution>
3 <id>copy-resources-AJS</id>
4 <!-- here the phase you need -->
5 <phase>validate</phase>
6 <goals>
7 <goal>copy-resources</goal>
8 </goals>
9 <configuration>
10 <outputDirectory>${basedir}/src/main/webapp/AJS</outputDirectory>
11 <resources>
12 <resource>
13 <directory>C:/Projects/lw/AJS/chapter1/angularApp</directory>
14 <filtering>true</filtering>
15 </resource>
16 </resources>
17 </configuration>
18 </execution>
19 <execution>
20 <id>copy-resources-A4</id>
21 <!-- here the phase you need -->
22 <phase>validate</phase>
23 <goals>
24 <goal>copy-resources</goal>
25 </goals>
26 <configuration>
27 <outputDirectory>${basedir}/src/main/webapp/A4</outputDirectory>
28 <resources>
29 <resource>
30 <directory>C:/Projects/lw/A4/chapter1/Angular4TypeScript/build</directory>
31 <filtering>true</filtering>
32 </resource>
33 </resources>
34 </configuration>
35 </execution>
36 <execution>
37 <id>copy-resources-A5</id>
38 <!-- here the phase you need -->
39 <phase>validate</phase>
40 <goals>
41 <goal>copy-resources</goal>
42 </goals>
43 <configuration>
44 <outputDirectory>${basedir}/src/main/webapp/A5</outputDirectory>
45 <resources>
46 <resource>
47 <directory>C:/Projects/lw/A5/chapter1/Angular5TypeScript/build</directory>
48 <filtering>true</filtering>
49 </resource>
50 </resources>
51 </configuration>
52 </execution>
53 </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:

view plain print about
1${basedir}/src/main/webapp/A5

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

The Angular 4 source files to:

view plain print about
1${basedir}/src/main/webapp/A4

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

And the AngularJS files will be put to:

view plain print about
1${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.