How do I copy files with Maven?
Jan 16,
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:
2 <artifactId>maven-resources-plugin</artifactId>
3 <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:
2 <execution>
3 <id>copy-resources-A5</id>
4 <phase>validate</phase>
5 <goals>
6 <goal>copy-resources</goal>
7 </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.
2 <outputDirectory>${basedir}/src/main/webapp/A5</outputDirectory>
3 <resources>
4 <resource>
5 <directory>C:/Projects/lw/A5/chapter7/Angular5TypeScript/build</directory>
6 <filtering>true</filtering>
7 </resource>
8 </resources>
9 </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:
2</executions>
3</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.