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

时间:2021-04-15 23:08:38

Maven's assembly plugin enables the creation of a big jar including all dependencies with descriptorRef jar-with-dependencies.

Maven的程序集插件可以创建一个大jar,包括所有依赖于descriptorRef jar-with-dependencies的依赖项。

How can one exclude some of these dependencies? It seems like it does not have such a configuration? Is there another solution?

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

3 个解决方案

#1


10  

This example indicates one way to do this.

此示例表明了一种方法。

Essentially we would use the excludes option available in dependencySet as documented here.

基本上我们将使用dependencySet中可用的excludes选项,如此处所述。

#2


15  

Add the <scope>provided</scope> to the dependencies you don't want included in the jar-with-dependencies, e.g.

提供的 添加到您不希望包含在jar-with-dependencies中的依赖项,例如

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

#3


3  

You should use the excludes option available in dependencySet.
Follow below.:

您应该使用dependencySet中提供的excludes选项。遵循以下:

Example in your pom.xml:

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

Now in your new file jar-with-deps-with-exclude.xml (where dependencySet live):

现在在你的新文件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>

That's all.

就这样。

#1


10  

This example indicates one way to do this.

此示例表明了一种方法。

Essentially we would use the excludes option available in dependencySet as documented here.

基本上我们将使用dependencySet中可用的excludes选项,如此处所述。

#2


15  

Add the <scope>provided</scope> to the dependencies you don't want included in the jar-with-dependencies, e.g.

提供的 添加到您不希望包含在jar-with-dependencies中的依赖项,例如

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

#3


3  

You should use the excludes option available in dependencySet.
Follow below.:

您应该使用dependencySet中提供的excludes选项。遵循以下:

Example in your pom.xml:

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

Now in your new file jar-with-deps-with-exclude.xml (where dependencySet live):

现在在你的新文件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>

That's all.

就这样。