关于maven3:聊聊springboot项目引用第三平台私有jar踩到的坑

前言

最近和敌人闲聊,他说他遇到一个问题,他援用了第三方公司公有API包,他在本地我的项目启动没问题,打包运行却找不到这个API包,于是我就问他怎么援用这个jar。

他工程项目第三jar寄存的地位相似如下

在pom做如下援用

   <dependency>
            <groupId>org.example</groupId>
            <artifactId>demo-api</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/demo-api.jar</systemPath>
        </dependency>

pom打包插件用springboot自带的插件


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

看到这个插件,大略就晓得问题所在了,springboot默认的打包插件是不会把systemscope的jar打进springboot我的项目的BOOT-INF/lib/。

注: springboot我的项目默认会援用BOOT-INF/lib/外面的jar

于是我就跟敌人说,不要用systemscope了,间接搭建maven私仓,而后把第三方jar上传到私仓中,pom做如下援用

   <dependency>
            <groupId>org.example</groupId>
            <artifactId>demo-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

敌人给回复是公司没有私仓,我一脸懵逼,我就问他应该不至于吧,再次确认,失去他同样的回复后。后边就提供了下边的几种计划,让他参考抉择

springboot如何援用没有公布到私仓的第三jar

整体思路:因为springboot提供的打包插件,默认是会把位于BOOT-INF/lib/外面的jar编译成class文件而后供我的项目援用。因而咱们只需确保BOOT-INF/lib/外面含有咱们要援用的第三方jar即可

计划一:pom指定jar范畴为system+springboot插件退出includeSystemScope标签的属性为true

示例

   <dependency>
            <groupId>org.example</groupId>
            <artifactId>demo-api</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/demo-api.jar</systemPath>
        </dependency>
 <build>

        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <includeSystemScope>true</includeSystemScope>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

计划二:pom指定jar范畴为system+resources标签引入要蕴含的jar

   <dependency>
            <groupId>org.example</groupId>
            <artifactId>demo-api</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/demo-api.jar</systemPath>
        </dependency>
 <build>
        <resources>
            <resource>
                <directory>${project.basedir}/lib</directory>
                <targetPath>BOOT-INF/lib/</targetPath>
                <includes>
                    <include>**/*.jar</include>
                </includes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

计划三:间接将第三方jar打进要公布的本地仓库


上图是maven官网的仓库流程图,其实对maven比拟相熟的敌人应该会晓得,maven会先从本地仓库找jar,本地仓库找不到jar,就会再从私仓(如果有搭建私仓)外面找,私仓没有再从地方仓库找,而后找到的jar再寄存到本地仓库。

因而咱们执行如下命令就能够将第三方jar间接打进本地仓库

mvn install:install-file -DgroupId=org.example -DartifactId=demo-api -Dversion=1.0-SNAPSHOT -Dfile=F:\boot-thirdparty\lib\demo-api.jar -Dpackaging=jar

我的项目的pom间接向如下引入第三方jar即可

   <dependency>
            <groupId>org.example</groupId>
            <artifactId>demo-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

计划四:搭建maven私仓,将第三方jar上传到maven私仓

注: 搭建私仓不在本文阐述范畴,就讲下如何将第三方jar上传到私仓

a、 先在maven的settings.xml的servers标签配置如下内容

<server>
        <id>nexus</id>
        <username>admin</username>
        <password>admin123</password>
    </server>

b、 执行发布命令行,如下

mvn deploy:deploy-file -DgroupId=org.example -DartifactId=demo-api -Dversion=1.0-SNAPSHOT -Dpackaging=jar -Dfile=F:\boot-thirdparty\lib\demo-api.jar -Durl=你的私仓地址 -DrepositoryId=和settings.xml配置server标签id统一,如上的nexus 

或者也能够利用maven私仓自带的可视化界面进行上传

总结

下面几种计划,集体是比拟偏向计划三和计划四,因为原本就是用maven来治理jar了,在我的项目中还要额定把jar引进来,而后再批改插件,看着就感觉有点变扭

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理