简体   繁体   English

Maven复制依赖关系+阴影-类路径管理

[英]Maven copy-dependencies + shade - classpath management

To package a maven project with its dependencies, among many solutions, one may use maven-dependency-plugin with its goal copy-dependencies to get the dependencies in a folder besides, or one may use maven-shade-plugin to get all the code in a single jar. 为了将具有相关性的maven项目打包,在许多解决方案中,可以使用具有目标copy-dependencies maven-dependency-plugin来获取文件夹中的相关性,或者可以使用maven-shade-plugin来获取所有代码。在一个罐子里。

I actually do both: I choose to have external dependencies (eg apache commons) as external libs, and my own dependencies (I have a multi-module maven parent project) shaded into a unique jar. 实际上,我两者都做:我选择将外部依赖项(例如apache commons)作为外部库,并将我自己的依赖项(我有一个多模块maven父项目)隐藏到一个唯一的jar中。

And it works, except for the classpath. 它起作用,除了类路径。 I copy-dependencies with option excludeGroupIds to exclude my own maven group id. 我使用选项excludeGroupIds copy-dependencies项以排除自己的Maven组ID。 I shade with option to include only my own maven group id. shade与选项,只包括我自己的Maven组ID。 Before that, I jar with option to add classpath to the manifest. 在此之前,我会选择将classpath添加到清单的选项。 All set, it works. 全部设置成功。 But my classpath also contains my own dependencies that were actually shaded in the final jar. 但是我的类路径还包含我自己的依赖关系,而这些依赖关系实际上已在最终的jar中被遮蔽。

It is no big deal, because the result works even with this erroneous classpath. 没什么大不了的,因为即使使用此错误的类路径,结果也可以工作。 But I wonder if there is a simple means to have the correct classpath, in order not to expose my internal structure to my users. 但是我想知道是否有一种简单的方法来拥有正确的类路径,以免向用户公开我的内部结构。

Here is a basic example demonstrating the problem: 这是演示问题的基本示例:

<groupId>com.foo.bar</groupId>
<artifactId>com.foo.bar.launcher</artifactId>
<dependencies>
    <dependency>
        <groupId>com.foo.bar</groupId>
        <artifactId>com.foo.bar.utils</artifactId>
        <version>0.0.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.8.1</version>
    </dependency>
</dependencies>
<plugin>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <classpathPrefix>lib/</classpathPrefix>
            </manifest>
        </archive>
    </configuration>
</plugin>
<plugin>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>copy-dependencies</id>
            <phase>package</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <outputDirectory>${project.build.directory}/lib</outputDirectory>
                <excludeGroupIds>com.foo.bar</excludeGroupIds>
            </configuration>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <executions>
        <execution>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <artifactSet>
                    <includes>
                        <include>com.foo.bar:*</include>
                    </includes>
                </artifactSet>
            </configuration>
        </execution>
    </executions>
</plugin>

The resulting manifest contains this: Class-Path: lib/com.foo.bar.utils-0.0.1.jar lib/commons-lang3-3.8.1.jar while the com.foo.bar.utils one does not exist. 结果清单包含以下内容: Class-Path: lib/com.foo.bar.utils-0.0.1.jar lib/commons-lang3-3.8.1.jar而com.foo.bar.utils不存在。

If you look into the following mvnrepository link maven shade plugin depends upon maven dependency tree . 如果您查看以下mvnrepository链接,则maven shade插件将取决于maven dependency tree As per the above pom.xml maven dependency plugin, you have excluded com.foo.bar dependency. 按照上面的pom.xml maven依赖插件,您已经排除了com.foo.bar依赖。 You can omit the maven-dependency-plugin to create fat jar. 您可以省略maven-dependency-plugin来创建胖子。 It is not mandatory to use in case of shade plugin. 在阴影插件的情况下,并非必须使用。

You can use the following command to check and copy all the dependencies used in the project. 您可以使用以下命令来检查并复制项目中使用的所有依赖项。

mvn dependency:copy-dependencies

https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-shade-plugin/3.2.1 https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-shade-plugin/3.2.1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM