簡體   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