【问题标题】:Include specific dependencies with maven assembly plugin使用 Maven 程序集插件包含特定的依赖项
【发布时间】:2015-03-29 02:28:13
【问题描述】:

我正在使用 maven 程序集插件,以便我可以包含特定的依赖项(基本上我想 在我的 jar 文件中包含特定的传递依赖项)类。这是我来自pom.xml的相关代码sn-p -

<build>
    <plugins>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
                <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>
      </plugin>  

      </plugins>
  </build> 

看起来可以从链接http://maven.apache.org/plugins/maven-assembly-plugin/advanced-descriptor-topics.html 但是只要在下面提到代码 sn-p 以包含两种类型的依赖项 com.companyA.* 和 com.companyB.* ,除了 我不想排除所有其他依赖项

<dependencySets>
    <dependencySet>
      <includes>
        <include>com.companyA.*</include>
        <include>com.companyB.*</include>
      </includes>
    </dependencySet>
  </dependencySets>

但是pom.xml依赖集的内容无效。我不知道如何在这里实现目标?

【问题讨论】:

    标签: java maven pom.xml


    【解决方案1】:

    我想你和我有同样的问题。在这里你只能像这样在 pom.xml 中进行部分配置:

    <plugin>    
                  <artifactId>maven-assembly-plugin</artifactId>    
                  <configuration>    
                        <descriptors>    
                            <descriptor>assembly.xml</descriptor>    
                       </descriptors>    
                  </configuration>    
                    <executions>  
                        <execution>  
                            <phase>install</phase>  
                            <goals>  
                                <goal>single</goal>  
                            </goals>  
                            <configuration>  
                                <outputDirectory>d:/temp/stock</outputDirectory>  
                            </configuration>  
                        </execution>  
                    </executions>  
    
             </plugin>  
    

    但您可以在 assembly.xml 中配置大部分程序集配置,如下所示:

    <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>jar-with-dependencies</id>  
        <formats>  
            <format>dir</format>  
        </formats>  
        <includeBaseDirectory>false</includeBaseDirectory>  
        <dependencySets>  
            <dependencySet>  
                <useProjectArtifact>true</useProjectArtifact>  
                <outputDirectory>/</outputDirectory>  
                <includes>  
                    <include>org.tkxing.stock:org.tkxing.stock.test</include>  
                </includes>         
            </dependencySet>  
            <dependencySet>  
                <useProjectArtifact>false</useProjectArtifact>  
                <outputDirectory>lib/</outputDirectory>  
                <excludes>  
                    <exclude>org.springframework:spring-beans</exclude>  
                    <exclude>org.springframework:spring-asm</exclude>  
                    <exclude>org.springframework:spring-core</exclude>  
                    <exclude>org.springframework:spring-aop</exclude>  
                    <exclude>org.springframework:spring-context</exclude>  
                    <exclude>org.springframework:spring-expression</exclude>  
                    <exclude>org.springframework:spring-jms</exclude>  
                    <exclude>org.springframework:spring-tx</exclude>  
                </excludes>  
            </dependencySet>  
        </dependencySets>  
    
        <fileSets>  
            <fileSet>  
                <directory>conf</directory>  
                <outputDirectory>conf</outputDirectory>  
            </fileSet>  
            <fileSet>  
                <directory>bundles</directory>  
                <outputDirectory>bundles</outputDirectory>  
            </fileSet>  
        </fileSets>  
    
        <files>  
            <file>  
                <source>log4j.xml</source>  
                <outputDirectory>/</outputDirectory>  
            </file>  
        </files>  
    
    </assembly>  
    

    希望这可以帮助其他人

    【讨论】:

      【解决方案2】:

      我通常使用两个具有互斥模式的 &lt;dependencySet&gt; 条目,就像这样

      <dependencySet>
        <outputDirectory>lib</outputDirectory>
        <scope>runtime</scope>
        <excludes>
          <exclude>com.mypkg.*:*</exclude>
        </excludes>
      </dependencySet>
      
      <dependencySet>
        <outputDirectory>bin</outputDirectory>
        <scope>runtime</scope>
        <includes>
          <include>com.mypkg.*:*</include>
        </includes>
      </dependencySet>
      

      在这种情况下,它将com.mypkg 中的所有内容复制到bin 文件夹,而不是将com.mypkg 中的所有内容复制到lib 文件夹

      这种方法应该足以满足您的目标

      【讨论】:

        猜你喜欢
        • 2016-02-23
        • 2015-09-08
        • 2014-04-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-12
        相关资源
        最近更新 更多