A Developer's Diary

Jan 12, 2013

Maven - Copying artifacts to specific folder

Above is the directory structure of web application generated using maven. Use maven-dependency-plugin for copying artifacts to desired folder under WEB-INF directory

The application has only one dependent artifact item as shown in the pom.xml below
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.webapp</groupId>
  <artifactId>MyWeb</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>MyWeb Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>MyWeb</finalName>
  </build>
</project>

Use maven-dependency-plugin to copy an artifact to a specific directory. Add the lines below to your pom.xml under build tag to include the dependency plugin and run it as part of the mvn package command. Make sure you have also defined the copy goal.
<build>
    <finalName>MyWeb</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.6</version>
            <executions>
                <execution>
                    <id>id</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy</goal>
                    </goals>
     <configuration>
                    <artifactItems>
                        <artifactItem>
                            <groupId>junit</groupId>
                            <artifactId>junit</artifactId>
                            <outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/services</outputDirectory>
                        </artifactItem>
                    </artifactItems>
     </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Note: The plugins tag should NOT be within pluginManagement The pluginManagement tag is a way to share the same plugin configuration across all your project modules whereas plugins tag is an actual invocation of the plugin.

No comments :

Post a Comment