maven assembly plugin dependencySet with transitive dependencies

柔情痞子 提交于 2019-12-07 13:27:13

问题


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"

回答1:


You need to create multiple dependencySets, one for each directory you want to create, then use includes or excludes to control which dependencies go in which directory.

For example, if you wanted spring-aop to end up in an aop directory and spring-jdbc to end up in a jdbc directory, you would use the following dependencySets:

<dependencySets>

  <dependencySet>
    <outputDirectory>aop</outputDirectory>
    <useProjectArtifact>false</useProjectArtifact>
    <includes>
      <include>org.springframework:spring-aop:jar</include>
    </includes>        
  </dependencySet>

  <dependencySet>
    <outputDirectory>jdbc</outputDirectory>
    <useProjectArtifact>false</useProjectArtifact>
    <includes>
      <include>org.springframework:spring-jdbc:jar</include>
    </includes>
  </dependencySet>

</dependencySets>

You'll need to play about to ensure that the correct transitive dependencies are pulled in, maybe by adding additional includes, but you get the gist.




回答2:


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.




回答3:


There is old bug opened for maven-assembly plugin https://issues.apache.org/jira/browse/MASSEMBLY-873 with similar examples.



来源:https://stackoverflow.com/questions/22505886/maven-assembly-plugin-dependencyset-with-transitive-dependencies

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!