关于 maven 打包直接运行的 fat jar (uber jar) 时需要包含本地文件系统第三方 jar 文件的问题

关于maven打包fat jar (uber jar) 时需要包含本地文件系统第三方jar文件的问题,今天折腾了一整天。最后还是用了spring boot来做。下面是几篇关于打包的有参考价值的文章,以及我的解决方法。

 

参考文章:

1.  将 Spring boot 项目打成可执行Jar包,及相关注意事项(main-class、缺少 xsd、重复打包依赖)  https://blog.csdn.net/rainbow702/article/details/55096506

 

2.  Java 打包 FatJar 方法小结  https://yq.aliyun.com/articles/631100

   (此文说明了Unshaded,Shaded 和 jar of jars 的区别和方法,其中提到了 jar of jars 的工具有 Onejar Maven Plugin 和 下面的 spring boot plugin)

  

3.  maven打包可运行的fat-jar的简单方法  https://www.cnblogs.com/pekkle/p/9680769.html   

     (此处两种方法均无法包含本地jar,下面就是关于如何在第二种方法中加入本地jar的选项。对于assembly,或者maven shade插件,我都还搞不清楚如何把本地jar包进去,或者包进去后如何被查找到。有知道的同学麻烦留言告知,不胜感激!)

 

4. (20191101 新增) maven-shade-plugin介绍及使用    https://blog.csdn.net/yangguosb/article/details/80619481

 

我采用的方法:

最后的pom.xml文件,其中的gson是测试maven repo的依赖,mxGraph是测试本地jar文件的依赖 :

<?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.pekkle</groupId>
    <artifactId>test-maven</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>${artifactId}</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>

        <!--这个是maven repo中的dependency-->
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.5</version>
        </dependency>

        <!--这个是本地文件系统中的dependency,groupId和artifactId和version其实都是可以自己随便编一个的-->
        <!--systemPath就是指向本地jar文件,${basedir}指向pom.xml文件所在的目录-->
        <dependency>
            <groupId>com.jgraph</groupId>
            <artifactId>abctest</artifactId>
            <version>3.9.11</version>
            <scope>system</scope>
            <systemPath>${basedir}/src/lib/mxgraph-all.jar</systemPath>
        </dependency>

    </dependencies>

    <build>
        <finalName>${artifactId}</finalName>

        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <!--自己写的应用的主类-->
                    <mainClass>com.pekkle.App</mainClass>
                    <!--能包含本地文件系统的jar包的关键-->
                    <includeSystemScope>true</includeSystemScope>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <!--必须repackage才会生成最终的fat jar-->
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

 

生成的jar最后直接运行: java -jar ./target/test-maven.jar  , 没有其他依赖。

 

总结:

本地文件的scope “system” 是被反对使用的,因为这样的文件是游离在maven repo之外。虽然可以通过安装到内网的Nexus仓库,或者安装到本机的repo仓库来提供依赖坐标,但是对于偶尔需要用到第三方jar,又需要整合到mvn package过程的需求来说,似乎还是需要这样一条路径。

 

posted @ 2018-12-04 22:19  Pekkle  阅读(1577)  评论(0编辑  收藏  举报