如何从maven程序集插件中排除依赖项:jar-with-dependencies?

Jér*_*nge 25 java jar maven maven-assembly-plugin

Maven的程序集插件可以创建一个大jar,包括所有与descriptorRef的依赖关系jar-with-dependencies.

如何排除其中一些依赖?好像它没有这样的配置?还有其他解决方案吗?

Jer*_*ens 24

添加<scope>provided</scope>到您不希望包含在jar-with-dependencies中的依赖项,例如

    <dependency>
      <groupId>storm</groupId>
      <artifactId>storm</artifactId>
      <version>0.6.1-SNAPSHOT</version>
      <scope>provided</scope>
    </dependency>
Run Code Online (Sandbox Code Playgroud)


Rag*_*ram 11

示例表明了一种方法:

 <dependencySets>
    <dependencySet>
      ....
      <excludes>
        <exclude>commons-lang:commons-lang</exclude>
        <exclude>log4j:log4j</exclude>
      </excludes>
    </dependencySet>
    ....
  </dependencySets>
Run Code Online (Sandbox Code Playgroud)

基本上我们会使用excludes可用的选项dependencySet.

另见:https://maven.apache.org/plugins/maven-assembly-plugin/assembly-component.html

  • 这个例子假设我知道dependencySets在pom.xml中的位置.一个完整的例子会有所帮助. (14认同)
  • 这给出了一个错误:maven3的"无法识别的标签:'dependencySets'" (6认同)
  • 这里的问题(7 年之后)是这仅出现在程序集定义文件中,它不与“jar-with-dependencies”一起使用。 (3认同)

fel*_*gnu 11

您应该使用excludes可用的选项dependencySet.
遵循以下:

pom.xml中的示例:

...
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.3</version>
        <configuration>
          <finalName>../final/${project.artifactId}</finalName>
          <archive>
            <manifest>
              <addClasspath>false</addClasspath>
              <mainClass>com.entrerprise.App</mainClass>
            </manifest>
          </archive>
          <descriptors>
            <descriptor>src/main/resources/jar-with-deps-with-exclude.xml</descriptor>
          </descriptors>
          <appendAssemblyId>false</appendAssemblyId>
        </configuration>
        <executions>
          <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
...
Run Code Online (Sandbox Code Playgroud)

现在在你的新文件jar-with-deps-with-exclude.xml(其中dependencySet live):

 <?xml version="1.0" encoding="UTF-8"?>
    <assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
        <id>jar-with-dependencies-and-exclude-classes</id>
        <formats>
          <format>jar</format>
        </formats>
        <includeBaseDirectory>false</includeBaseDirectory>
        <dependencySets>
          <dependencySet>
            <outputDirectory>/</outputDirectory>
            <useProjectArtifact>false</useProjectArtifact>
            <unpack>true</unpack>
            <scope>runtime</scope>
            <excludes>
              <exclude>junit:junit</exclude>
              <exclude>commons-cli:commons-cli</exclude>
              <exclude>org.apache.maven.wagon:wagon-ssh</exclude>
            </excludes>
           </dependencySet>
        </dependencySets>
        <fileSets>
          <fileSet>
            <outputDirectory>/</outputDirectory>
            <directory>${project.build.outputDirectory}</directory>
          </fileSet>
        </fileSets>
      </assembly>
Run Code Online (Sandbox Code Playgroud)

就这样.

  • 比Raghuram的答案更好 (5认同)