Check out our Angular Book Series.

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.

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
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.