Tuesday, July 12, 2011

maven copy Dependenies/Resources

Add the following plugin to your pom to copy dependencies :
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.3</version>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${deployment.dir}</outputDirectory>                                      
                            <excludeArtifactIds>javaee-api</excludeArtifactIds>
                        </configuration>   
                    </execution>
                   
                </executions>
            </plugin>

 Add the following plugin to your pom to copy Resources :

            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.5</version>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${deployment.dir}</outputDirectory>
                            <resources>         
                                <resource>
                                    <directory>target</directory>
                                    <filtering>true</filtering>
                                    <includes>
                                        <include>*.jar</include>
                                    </includes>
                                </resource>
                            </resources>             
                        </configuration>           
                    </execution>
                </executions>
            </plugin>
 
You can then simply run the Project and have the needed libraries in your deployment location.

But remember to define the properties for addressing the target library. In this example :


    <properties>
        <glassfish.home>/opt/programs/glassfish</glassfish.home>
        <deployment.dir>${glassfish.home}/glassfish/domains/my-domain/lib</deployment.dir>
    </properties>

my maven assembly plugin

I want the provided dependencies in the current project and all the included submodules to take place in the current project's target/lib-lib directory.

the assembly.xml file :


<assembly xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2" xsi:schemalocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<id>assembly</id>
<formats>
<format>dir</format>
</formats>

<modulesets>
<moduleset>


<useallreactorprojects>true</useallreactorprojects>


<includes>


<include>CHILD-MODULE-1-GROUP-ID:
CHILD-MODULE-1-ARTIFACT-ID</include>
<include>CHILD-MODULE-2-GROUP-ID:CHILD-MODULE-2-ARTIFACT-ID</include>
</includes>

<binaries>
<dependencysets>
<dependencyset>
<outputdirectory>/lib-lib</outputdirectory>

<unpack>false</unpack>
<scope>provided</scope>
</dependencyset>
</dependencysets>
<includedependencies>true</includedependencies>
</binaries>
</moduleset>
</modulesets>

</assembly>


the pom.xml file :

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.2.1</version>
                <configuration>
                    <descriptors>
                        <descriptor>assembly.xml</descriptor>
                    </descriptors>
                </configuration>
            </plugin>
        </plugins>
    </build>

Now use the "package assembly:assembly" action to execute it.