英文:
Unused classes not packaged in a Maven multi-module project
问题
我正在使用 Maven 3.6.2 构建一个多模块 Java 项目。example 模块依赖于 core 模块,因此在 example 模块的 POM 文件中有一个依赖,如下所示:
<dependency>
<groupId>com.company</groupId>
<artifactId>core</artifactId>
<version>${project.version}</version>
</dependency>
问题是,当我运行 mvn clean install 时,并不是所有来自 core 模块的类都被打包进 example 的 JAR 文件中。只有在 example 项目中使用的那些类才会被打包。我需要所有的类都被打包,以便后续的处理。
我正在使用 maven-shade-plugin 插件,并进行如下配置:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<minimizeJar>true</minimizeJar>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
<exclude>module-info.class</exclude>
</excludes>
</filter>
</filters>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<finalName>${project.name}</finalName>
</configuration>
</execution>
</executions>
</plugin>
我该如何将 core 模块中的所有类都包含在 example 模块的 JAR 文件中,而无需在 example 模块中使用它们?
英文:
I'm using Maven 3.6.2 with a multi-module Java project. The example module depends on the core module, so I have a dependency in the example module's POM like this:
<dependency>
<groupId>com.company</groupId>
<artifactId>core</artifactId>
<version>${project.version}</version>
</dependency>
The issue is that not all of the classes from the core module are being packaged into the example jar when I run mvn clean install. Only the classes that are used in the example project are being packaged. I need all classes packaged for a later process.
I'm using the maven-shade-plugin with this configuration:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<minimizeJar>true</minimizeJar>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
<exclude>module-info.class</exclude>
</excludes>
</filter>
</filters>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<finalName>${project.name}</finalName>
</configuration>
</execution>
</executions>
</plugin>
How can I include all classes from the core module in the example jar without having to use them in the example jar?
答案1
得分: 1
问题出在 minimizeJar 参数上。根据 https://maven.apache.org/plugins/maven-shade-plugin/shade-mojo.html#minimizeJar:
> 当设置为 true 时,依赖项将在类级别上被削减,仅保留构件所需的传递闭包。
我移除了该参数,然后它按预期工作。
英文:
The issue was the minimizeJar parameter. According to https://maven.apache.org/plugins/maven-shade-plugin/shade-mojo.html#minimizeJar:
> When true, dependencies will be stripped down on the class level to
> only the transitive hull required for the artifact.
I removed that parameter and it worked as expected.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论