如何使用Maven将本地jar作为依赖项添加到胖jar中? [英] How to add local jar to fat jar as a dependency using maven?

查看:187
本文介绍了如何使用Maven将本地jar作为依赖项添加到胖jar中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试建立一个胖子罐,以便在其他地方使用它.
我为此使用maven assembly plugin:

I'm trying to build a fat jar to use it in some other place.
I use maven assembly plugin for that:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>3.1.1</version>
    <configuration>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>

    </configuration>
    <executions>
        <execution>
            <id>assemble-all</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>

在我的依赖项中,我有一个本地jar依赖项,我不能拒绝:

Among my dependencies I have a local jar dependency, which I can't refuse:

<dependency>
    <groupId>com.example</groupId>
    <artifactId>library</artifactId>
    <version>1.0.0</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/lib/my-jar.jar</systemPath>
</dependency>

但是这个罐子包含在我用mvn package创建的最终胖罐子中. 将本地jar包含到胖jar中作为依赖项的最佳方法是什么?

But this jar is on included to the final fat jar which I create with mvn package.
What is the best way to include my local jar to fat jar as a dependency?

UPD.
有一些相关的问题,但是它们不能完全回答问题:
在构建之前将jar添加到maven本地存储库
maven-assembly-plugin不会在系统范围内添加依赖项

UPD.
There are some related questions, but they do not answer the question completely:
add jar to the maven local repository before build
maven-assembly-plugin doesn't add dependencies with system scope

推荐答案

更易于使用阴影插件:

父项目:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.greg</groupId>
  <artifactId>fat-jar</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>

  <modules>
    <module>library-jar</module>
    <module>final-jar</module>
  </modules>

</project>

图书馆项目:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <parent>
    <artifactId>fat-jar</artifactId>
    <groupId>com.greg</groupId>
    <version>1.0-SNAPSHOT</version>
  </parent>

  <artifactId>library-jar</artifactId>
  <dependencies>
    ....
  </dependencies>

  <build>
  </build>
</project>

在同一父项下具有库依赖关系的最终jar:

Final jar with dependency to library, under the same parent:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <artifactId>fat-jar</artifactId>
        <groupId>com.greg</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>final-jar</artifactId>

    <dependencies>
        <dependency>
            <groupId>com.greg</groupId>
            <artifactId>library-jar</artifactId>
            <version>${project.version}</version>
        </dependency>
          ...
    </dependencies>

    <build>
        <plugins>
            <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>
                            <transformers>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.greg.App</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

这篇关于如何使用Maven将本地jar作为依赖项添加到胖jar中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆