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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22505886/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 16:07:18  来源:igfitidea点击:

maven assembly plugin dependencySet with transitive dependencies

javamavenmaven-3maven-assembly-plugin

提问by Alexander Kudrevatykh

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

我不明白如何<dependencySet>在程序集中添加部分依赖项

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

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

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

我创建的程序集描述符应该复制 group:artifact2 和所有依赖项。

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.

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

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

结果我想有两个包含不同库集的目录。

Simple example here

简单的例子在这里

pom.xml

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

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"

采纳答案by Alexander Kudrevatykh

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

为 maven-assembly 插件https://issues.apache.org/jira/browse/MASSEMBLY-357打开了旧错误,其中包含类似的示例。

回答by Nick Holt

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

您需要创建多个dependencySets,一个用于您要创建的每个目录,然后使用includesexcludes控制哪些依赖项进入哪个目录。

For example, if you wanted spring-aopto end up in an aopdirectory and spring-jdbcto end up in a jdbcdirectory, you would use the following dependencySets:

例如,如果您想spring-aop在一个aop目录spring-jdbc中结束并在一个jdbc目录中结束,您将使用以下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.

您需要努力确保引入正确的传递依赖项,也许是通过添加额外的includes,但您已经掌握了要点。

回答by khmarbaise

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:

您应该在 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>

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.

${project.groupId}排除的项目工件。您可以根据需要经常重复此部分以进行复制。