繁体   English   中英

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

[英]How to exclude dependencies from maven assembly plugin : jar-with-dependencies?

Maven 的程序集插件允许创建一个大的 jar 包括所有依赖 descriptorRef jar-with-dependencies

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

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

    <dependency>
      <groupId>storm</groupId>
      <artifactId>storm</artifactId>
      <version>0.6.1-SNAPSHOT</version>
      <scope>provided</scope>
    </dependency>

您应该使用dependencySet中可用的excludes选项。
关注以下:

您的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>
...

现在在你的新文件jar-with-deps-with-exclude.xmldependencySet所在的位置):

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

就这样。

示例指示了执行此操作的一种方法:

 <dependencySets>
    <dependencySet>
      ....
      <excludes>
        <exclude>commons-lang:commons-lang</exclude>
        <exclude>log4j:log4j</exclude>
      </excludes>
    </dependencySet>
    ....
  </dependencySets>

本质上,我们将使用dependencySet中可用的excludes选项。

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

另一种选择是切换到功能更丰富的maven-shade-plugin ,它可以在没有任何外部程序集文件或标记为“已提供”的情况下执行此操作(这可能不是你想要的,因为它迫使用户在他们的 pom 中具有这种依赖性). 这是一个例子:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.3.0</version>
    <configuration>
        <artifactSet>
            <excludes>
                <exclude><group-id>:<artifact-id>:<classifier></exclude>
                <exclude>org.apache.logging.log4j:log4j-core</exclude>
            </excludes>
        </artifactSet>
    </configuration>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
        </execution>
    </executions>
</plugin>

https://maven.apache.org/plugins/maven-shade-plugin/index.html

请注意,这不会生成一个jar-with-dependencies项的 jar,而是一个original-jar...然后只是常规的 output jar。这个插件甚至可以让您过滤掉特定的类。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM