maven 不重新打包依赖项

发布于 2024-12-24 01:20:06 字数 1356 浏览 0 评论 0原文

我使用 Maven 程序集插件将所有依赖项收集到一个 jar 文件中。我怎样才能告诉maven不要重新打包依赖项并将它们作为jar文件包含到生成的jar中?

目前我使用以下插件配置。

<plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>package.Program</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>

想要的 jar 文件结构:

my-jar-with-dependencies.jar
|-dependency1.jar
|-|-class1.class
|-dependency2.jar
|-|-class2.class
|-...........

而不是

my-jar-with-dependencies.jar
|-class1.class
|-class2.class
|-.............

I use maven assembly plugin to collect all dependencies into one jar file. How can I tell maven not to repack dependencies and include them as jar files into resulting jar?

Currently I use following plugin configuration.

<plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>package.Program</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>

wanted structure of jar file:

my-jar-with-dependencies.jar
|-dependency1.jar
|-|-class1.class
|-dependency2.jar
|-|-class2.class
|-...........

and not

my-jar-with-dependencies.jar
|-class1.class
|-class2.class
|-.............

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紫瑟鸿黎 2024-12-31 01:20:06

如果我正确理解了这个问题,您想要指定何时应该运行程序集插件。在这种情况下,您应该考虑创建一个 构建配置文件 并将程序集插件配置添加到新配置文件中。

在 pom.xml 中添加:

<project>
...
 <profiles>
    <profile>
        <id>myprofile</id>
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>single</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <archive>
                            <manifest>
                                <mainClass>package.Program</mainClass>
                            </manifest>
                        </archive>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
 </profiles>
...
</project>

当您希望 maven 运行程序集插件时,您可以使用 -P 开关切换到“mvn”脚本,如下所示:

mvn -Pmyprofile clean package

If I understand the question correctly you want to specify when the assembly plugn should be run or not. I this is the case you should consider creating a build profile and add the assembly plugin configuration to the new profile.

In pom.xml add:

<project>
...
 <profiles>
    <profile>
        <id>myprofile</id>
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>single</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <archive>
                            <manifest>
                                <mainClass>package.Program</mainClass>
                            </manifest>
                        </archive>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
 </profiles>
...
</project>

When you want maven to run the assembly plugin you can then use the -P switch to the 'mvn' script like this:

mvn -Pmyprofile clean package
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文