我想从我的 jar 中排除一些 class 文件。我正在使用 maven-assembly-plugin。它仍然添加文件。我没有收到任何错误

I wish to exclude some class files from my jar. I am using maven-assembly-plugin. It still adds the files. I dont get any error

我没有收到此代码的任何错误。只是我想排除的文件仍然被添加。我正在使用 eclipse

的 maven 插件
<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <executions>
        <execution>
            <id>only</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>com.Main</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <excludes>
                    <exclude>**/com/uiservices/controllers/*.*      </exclude>
                </excludes>
            </configuration>
        </execution>
    </executions>
</plugin>

maven-assembly-plugin 不是这样的。

你想做的是覆盖程序集描述符的配置jar-with-dependencies,这是不可能的。

如果您想做的是创建一个与程序集 jar-with-dependencies 类似的 jar,但没有您自己项目的某些特定 类,您必须编写自己的程序集并且在 maven-assembly-plugin 中调用它,如下所示。

src/assembly/jar-with-deps-with-exclude.xml 中的程序集:

<assembly
    xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
    <!-- TODO: a jarjar format would be better -->
    <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>
        </dependencySet>
    </dependencySets>
    <fileSets>
        <fileSet>
            <outputDirectory>/</outputDirectory>
            <directory>${project.build.outputDirectory}</directory>
            <excludes>
                <exclude>com/uiservices/controllers/*.*</exclude>
            </excludes>
        </fileSet>
    </fileSets>
</assembly>

这将创建一个程序集,其中没有解压缩的依赖项,并且添加了您的 类,排除的除外。

然后在你的 pom.xml 中:

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <executions>
        <execution>
            <id>only</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
            <configuration>
                <descriptors>
                    <descriptor>src/assembly/jar-with-deps-with-exclude.xml</descriptor> 
                </descriptors>
            </configuration>
        </execution>
    </executions>
</plugin>

但是如果你需要的是没有排除类的经典jar,你可以直接在maven-jar-plugin中排除它们:

<plugin>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <excludes>
            <exclude>com/uiservices/controllers/*.*</exclude>
        </excludes>
    </configuration>
</plugin>

我遇到了与 maven-assembly-plugin 类似的问题:

  1. 项目 beans-conversion 有文件 application.propertieslogback.xml,以及 logback.xsd
  2. 项目 extract-conversion 也有名为 application.propertieslogback.xmllogback.xsd
  3. 的文件
  4. 要求 extract-conversion.jar 应包含 beans-conversion.jar 内容,但 extract-conversion.jarapplication.propertieslogback.xmllogback.xsd 应覆盖 beans-conversion.jar.

解法:

我们在提取转换的 pom.xml.

中使用 maven-shade-plugin 解决了这个问题,如下所示
...
<dependency>
    <groupId>com.mycompany</groupId> 
    <artifactId>beans-conversion</artifactId> 
    <version>1.0.0-SNAPSHOT</version>
</dependency>
...
<build>
    <finalName>${project.artifactId}</finalName>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>application.properties</include>
                <include>logback.xml</include>
                <include>logback.xsd</include>
            </includes>
            <filtering>true</filtering>
        </resource>
    </resources>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.3</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.4.3</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <filters>
                            <filter>
                                <artifact>com.mycompany:beans-conversion</artifact>
                                <excludes>
                                    <exclude>application.properties</exclude>
                                    <exclude>logback.xml</exclude>
                                    <exclude>logback.xsd</exclude>
                                </excludes>
                            </filter>
                            <filter>
                                <artifact>*:*</artifact>
                                <excludes>
                                    <exclude>META-INF/*.SF</exclude>
                                    <exclude>META-INF/*.DSA</exclude>
                                    <exclude>META-INF/*.RSA</exclude>
                                </excludes>
                            </filter>
                        </filters>
                    </configuration>
                </execution>
            </executions>
        </plugin>                   
    </plugins>
</build>

使用 maven-shade-plugin 参考:Maven dependency: exclude one class

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.2.1</version>
    <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>shade</goal>
        </goals>
        <configuration>
        <finalName>${project.artifactId}-jar-with-dependencies</finalName>
         <transformers>
            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> 
              <manifestEntries>
                 <Main-Class>com.taobao.arthas.core.Arthas</Main-Class>
              </manifestEntries>
             </transformer>
            </transformers>
          <filters>
            <filter>
              <artifact>*:termd-core</artifact>
              <excludes>
                <exclude>META-INF/services/io.termd.core.spi.ConfigProvider</exclude>
              </excludes>
            </filter>
          </filters>
        </configuration>
      </execution>
    </executions>
  </plugin>