Welcome to Techy Tunes !

Follow Me

Copying multiple resources to a web app with maven-resources-plugin



By   Unknown      May 01, 2015      Labels: , ,  
There are couple of other ways to copy resources. but I find this is the easiest method.

maven-resources-plugin

<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<executions>
    <execution>
        <id>copy-libs</id>
        <phase>process-resources</phase>
        <goals>
            <goal>copy-resources</goal>
        </goals>
        <configuration>
            <outputDirectory>${basedir}/target/my-project/WEB-INF/lib</outputDirectory>
            <resources>
                <resource>
                    <directory>${deps.home}/lib</directory>
                </resource>
            </resources>
        </configuration>
    </execution>
    <execution>
        <id>copy-xml</id>
        <phase>process-resources</phase>
        <goals>
            <goal>copy-resources</goal>
        </goals>
        <configuration>
            <outputDirectory>${basedir}/target/classes/com/blogspot/techytunes/my-project/dao</outputDirectory>
            <resources>
                <resource>
                    <directory>${basedir}/src/main/java/com/blogspot/techytunes/my-project/dao/</directory>
                    <excludes>
                        <exclude>*.java</exclude>
                    </excludes>
                </resource>
            </resources>
        </configuration>
    </execution>
</executions>
</plugin>

  • We need multiple executions ( We need to copy from different sources to different destinations) . That's why we need the <executions> tag. Inside this tag we can define several executions using <execution> tag.
  • First, I have to copy some libs. I give an id to that process as <id>copy-libs</id>
  • Maven build life cycle has several phases. We need to use "process-resources" phase with the goal "copy-resources"
  • When packaging the web-app, maven takes the project folder from the target directory and creates the war file. In my case, it takes the my-project folder and packages its content to a war file. This happens in the package phase.
  • We add the basic configuration in the <configuration> tag. <outputDirectory> specifies the destination to copy.
  • In the <resources> tag we can add multiple <resource> elements. Everything we put in here, will be copied to the above destination.
  • To copy to another destination, we simply need another execution.
  • The second execution copy-xml is slightly different from copying resources. Some xml files needed to be copied to the destination of classes. 
  • After the process-resources phase there comes compile phase where maven compile all the classes to the target/classes directory. So we take the required xml files and copy them to the relevant destination in the target/classes/... 
  • we pick the xml files from src/main/java/com/blogspot/techytunes/my-project/dao and copy them to target/classes/com/blogspot/techytunes/my-project/dao
  • In the source directory (src/main/java/com/blogspot/techytunes/my-project/dao) we have .java files along with the xml. We use <excludes> tag to exclude .java files being copied to the destination.
  • When maven is in his compile phase, he will compile the classes and put it to the target/classes directory
Thats it. Good or bad, comments are always welcome :D

About Unknown

A Software Engineer who desperately loves Music. Works in WSO2 Telco . Loves cricket, hates Buriyani.

No comments :

Post a Comment