使用 maven 程序集插件包含特定的依赖项

Include specific dependencies with maven assembly plugin

我正在使用 maven 程序集插件,这样我就可以包含特定的依赖项(基本上我想 在我的 jar 文件中包含特定的传递依赖项)class。这是 pom.xml -

中我的相关代码片段
<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> 

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

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

但是 pom.xmldependencySets 的内容无效。这里不知道如何实现objective?

我通常使用两个具有互斥模式的 <dependencySet> 条目,就像这样

<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 文件夹

这种方法应该足以满足您要完成的任务

我想你和我有同样的问题。在这里你只能在 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>  

希望这对其他人有帮助