具有传递依赖性的maven程序集插件依赖项

时间:2014-03-19 12:29:05

标签: java maven maven-3 maven-assembly-plugin

我不明白如何在程序集

中向<dependencySet>添加部分依赖项

例如我依赖于group:artifact1和group:artifact2都依赖于group:artifact0。

我创建了汇编描述符,它应该复制group:artifact2以及所有依赖项。

如果pom.xml中group:artifact1的依赖关系出现在依赖group:artifact2之前,我在目标dir中只有group:artifact2,但如果我改变了依赖关系的顺序,那么我在目标目录中都有两个工件。

结果我希望有两个具有不同库集的目录。

这里的简单例子

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输出

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"

3 个答案:

答案 0 :(得分:2)

您需要为要创建的每个目录创建多个dependencySet,然后使用includesexcludes来控制哪个依赖项位于哪个目录中。

例如,如果您希望spring-aop最终位于aop目录而spring-jdbc最终位于jdbc目录中,则可以使用以下{{ 1}} S:

dependencySet

您需要努力确保提供正确的传递依赖关系,可能需要添加额外的<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> ,但您可以获得要点。

答案 1 :(得分:2)

您应该将pom文件中程序集中的依赖项定义为依赖项。这将确保建筑的顺序由Maven自动确定,并且将始终相同。 此外,您只需要提供以下描述符,将所有依赖项打包到zip文件中:

<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>

让transivite依赖进入不同的位置:

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

${project.groupId}排除了项目工件。您可以重复此部分,因为您经常需要复制它。

答案 2 :(得分:0)

为maven-assembly插件http://jira.codehaus.org/browse/MASSEMBLY-357打开了一个旧bug,其中有类似的例子。