未使用的类不会打包在Maven多模块项目中。

huangapple go评论95阅读模式
英文:

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:

&lt;dependency&gt;
    &lt;groupId&gt;com.company&lt;/groupId&gt;
    &lt;artifactId&gt;core&lt;/artifactId&gt;
    &lt;version&gt;${project.version}&lt;/version&gt;
&lt;/dependency&gt;

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:

&lt;plugin&gt;
    &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
    &lt;artifactId&gt;maven-shade-plugin&lt;/artifactId&gt;
    &lt;version&gt;3.2.1&lt;/version&gt;
    &lt;configuration&gt;
        &lt;createDependencyReducedPom&gt;false&lt;/createDependencyReducedPom&gt;
        &lt;minimizeJar&gt;true&lt;/minimizeJar&gt;
        &lt;filters&gt;
            &lt;filter&gt;
                &lt;artifact&gt;*:*&lt;/artifact&gt;
                &lt;excludes&gt;
                    &lt;exclude&gt;META-INF/*.SF&lt;/exclude&gt;
                    &lt;exclude&gt;META-INF/*.DSA&lt;/exclude&gt;
                    &lt;exclude&gt;META-INF/*.RSA&lt;/exclude&gt;
                    &lt;exclude&gt;module-info.class&lt;/exclude&gt;
                &lt;/excludes&gt;
            &lt;/filter&gt;
        &lt;/filters&gt;
    &lt;/configuration&gt;
    &lt;executions&gt;
        &lt;execution&gt;
            &lt;phase&gt;package&lt;/phase&gt;
            &lt;goals&gt;
                &lt;goal&gt;shade&lt;/goal&gt;
            &lt;/goals&gt;
            &lt;configuration&gt;
                &lt;finalName&gt;${project.name}&lt;/finalName&gt;
            &lt;/configuration&gt;
        &lt;/execution&gt;
    &lt;/executions&gt;
&lt;/plugin&gt;

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.

huangapple
  • 本文由 发表于 2020年9月5日 06:26:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63748661.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定