如何从Maven组件插件中排除依赖项:具有依赖项的jar?

vulvrdjw  于 2022-12-11  发布在  Maven
关注(0)|答案(4)|浏览(201)

Maven的汇编插件允许创建一个大jar,其中包括所有具有descriptorRef jar-with-dependencies的依赖项。
如何排除其中的一些依赖项?它似乎没有这样的配置?是否有其他解决方案?

e0bqpujr

e0bqpujr1#

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

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

c86crjj02#

您应该使用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>

就这样了

5gfr0r5j

5gfr0r5j3#

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

<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

xuo3flqw

xuo3flqw4#

另一种选择是切换到功能更丰富的maven-shade-plugin,它可以做到这一点,而不需要任何外部汇编文件或标记为“provided”(这可能不是你想要的,因为它迫使用户在他们的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,而是一个original-jar...,然后只是一个常规的输出jar。这个插件甚至可以让你过滤掉特定的类。

相关问题