如何使用Maven将本地jar作为依赖项添加到胖jar中?

编程入门 行业动态 更新时间:2024-10-09 09:19:43
本文介绍了如何使用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="maven.apache/POM/4.0.0" xmlns:xsi="www.w3/2001/XMLSchema-instance" xsi:schemaLocation="maven.apache/POM/4.0.0 maven.apache/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="maven.apache/POM/4.0.0" xmlns:xsi="www.w3/2001/XMLSchema-instance" xsi:schemaLocation="maven.apache/POM/4.0.0 maven.apache/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="maven.apache/POM/4.0.0" xmlns:xsi="www.w3/2001/XMLSchema-instance" xsi:schemaLocation="maven.apache/POM/4.0.0 maven.apache/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中?

本文发布于:2023-08-04 21:39:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1299853.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   Maven   jar

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!