Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

maven assembly plugin dependencySet with transitive dependencies

I don't understand how to add part of dependencies to <dependencySet> in assembly

for example I have dependency on group:artifact1 and group:artifact2 that both depend on group:artifact0.

I create assembly descriptor that should copy group:artifact2 with all dependencies.

If dependency on group:artifact1 in pom.xml appears before dependency on group:artifact2, I have only group:artifact2 in target dir, but if I change order of dependencies, than I have both artifacts in target dir.

In result I want to have two directories with different set of libraries.

Simple example here

pom.xml

<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>                                                                                                                                                                      
    <groupId>com.kudrevatykh.stackoverflow</groupId>                                                                                                                                                        
    <artifactId>assembly-question</artifactId>                                                                                                                                                              
    <version>0.0.1-SNAPSHOT</version>                                                                                                                                                                       
    <properties>                                                                                                                                                                                            
        <spring.version>3.2.3.RELEASE</spring.version>                                                                                                                                                      
    </properties>                                                                                                                                                                                           
    <dependencies>                                                                                                                                                                                          
        <dependency>                                                                                                                                                                                        
            <groupId>org.springframework</groupId>                                                                                                                                                          
            <artifactId>spring-aop</artifactId>                                                                                                                                                             
            <version>${spring.version}</version>                                                                                                                                                            
        </dependency>                                                                                                                                                                                       
        <dependency>                                                                                                                                                                                        
            <groupId>org.springframework</groupId>                                                                                                                                                          
            <artifactId>spring-jdbc</artifactId>                                                                                                                                                            
            <version>${spring.version}</version>                                                                                                                                                            
        </dependency>                                                                                                                                                                                       
    </dependencies>                                                                                                                                                                                         
    <build>                                                                                                                                                                                                 
        <plugins>                                                                                                                                                                                           
            <plugin>                                                                                                                                                                                        
                <artifactId>maven-assembly-plugin</artifactId>                                                                                                                                              
                <version>2.4</version>                                                                                                                                                                      
                <executions>                                                                                                                                                                                
                    <execution>                                                                                                                                                                             
                        <id>install</id>                                                                                                                                                                    
                        <phase>package</phase>                                                                                                                                                              
                        <goals>                                                                                                                                                                             
                            <goal>single</goal>                                                                                                                                                             
                        </goals>                                                                                                                                                                            
                        <configuration>                                                                                                                                                                     
                            <descriptor>install.xml</descriptor>                                                                                                                                            
                        </configuration>                                                                                                                                                                    
                    </execution>                                                                                                                                                                            
                </executions>                                                                                                                                                                               
            </plugin>                                                                                                                                                                                       
        </plugins>                                                                                                                                                                                          
    </build>                                                                                                                                                                                                
</project>                                                                                                                                                                                                  

install.xml

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    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>install</id>                                                                                                                         
    <includeBaseDirectory>false</includeBaseDirectory>                                                                                       
    <formats>                                                                                                                                
        <format>dir</format>                                                                                                                 
    </formats>                                                                                                                               
    <dependencySets>                                                                                                                         
        <dependencySet>                                                                                                                      
            <useProjectArtifact>false</useProjectArtifact>                                                                                   
            <includes>                                                                                                                       
                <include>org.springframework:spring-jdbc:jar</include>                                                                       
            </includes>                                                                                                                      
            <useTransitiveFiltering>true</useTransitiveFiltering>                                                                            
            <outputDirectory>/</outputDirectory>                                                                                             
        </dependencySet>                                                                                                                     
    </dependencySets>                                                                                                                        
</assembly>                                                                                                                                  

mvn --version output

Apache Maven 3.0.4 (r1232337; 2012-01-17 12:44:56+0400)
Maven home: C:\Users\akudrevatykh\bin\apache-maven-3.0.4
Java version: 1.7.0_11, vendor: Oracle Corporation
Java home: C:\Users\akudrevatykh\bin\jdk1.7.0_11\jre
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 7", version: "6.1", arch: "x86", family: "windows"
like image 387
Alexander Kudrevatykh Avatar asked Mar 20 '23 04:03

Alexander Kudrevatykh


1 Answers

You should define the dependencies you like to have in your assembly in the pom file as dependencies. This will make sure the order of building is automatically determined by Maven and will be the same all the time. Furthermore you just need to give the following descriptor like this which will package all the dependencies into a zip file:

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">

  <id>dist-assembly</id>

  <formats>
      <format>zip</format>
  </formats>

  <includeBaseDirectory>false</includeBaseDirectory>

  <dependencySets>
      <dependencySet>
          <outputDirectory>/</outputDirectory>
          <useProjectArtifact>false</useProjectArtifact>
          <unpack>false</unpack>
          <scope>runtime</scope>
      </dependencySet>
  </dependencySets>
</assembly>

Let the transivite dependencies go into different location:

<dependencySets>
  <dependencySet>
      <useProjectArtifact>false</useProjectArtifact>
      <useTransitiveDependencies>true</useTransitiveDependencies>
      <outputDirectory>lib</outputDirectory>
      <unpack>false</unpack>
      <excludes>
        <exclude>${project.groupId}:*</exclude>
      </excludes>
  </dependencySet>
</dependencySets>

The ${project.groupId} excludes the project artifacts. You can repeat this part as often you need them to be copied.

like image 177
khmarbaise Avatar answered Apr 25 '23 01:04

khmarbaise