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.

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.