java多线程    Java入门    vsftp    ftp    linux配置    centos    FRP教程    HBase    Html5缓存    webp    zabbix    分布式    neo4j图数据库    

maven引入本地jar包的方法

我用第二种办法搞定的,个人推荐第二种,成本低,见效快,适合多人开发

方法1:直接安装到本地库

mvn install:install-file -Dfile=D:\dechnicLib\INetSDK.jar -DgroupId=com.dahua.netsdk -DartifactId=dahua-netsdk-jni -Dversion=1.0.0 -Dpackaging=jar -DgeneratePom=true  -Dmaven.repo.local=E:\USERDATA\maven\repository

pom 里

<dependency>
    <groupId>com.dahua.netsdk</groupId>
    <artifactId>dahua-netsdk-jni</artifactId>
    <version>1.0.0</version>
</dependency>

这个模式有个弊端,如果要发布到服务器,必须在服务器的环境里也要操作一次mvn install,如果项目组有8个小伙伴,都要折腾一遍

方法2:引用本地文件

<dependency>
            <groupId>com.inspur.msy</groupId>
            <artifactId>green-channel-core</artifactId>
            <version>1.3.6</version>
            <scope>system</scope>
            <systemPath>${pom.basedir}/lib/green-core-1.3.6.jar</systemPath>
        </dependency>

${pom.basedir} pom.xml 文件所在的那个目录

部署的时候,告诉maven别忘记本地这个jar文件

<plugin>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-maven-plugin</artifactId>
	<configuration>
		<fork>true</fork><!--热部署devTools生效-->
        <includeSystemScope>true</includeSystemScope><!--包含 本地 引入jar包-->
	</configuration>
	<executions>
        <execution>
             <goals>
                 <goal>repackage</goal>
             </goals>
         </execution>
     </executions>
</plugin>

要点来了:

2022年7月22,我打包微信官方的wxpay的包,结果用mvn install微信支付sdk的工程项目项目可以跑
用jar包就不行,但是IDEA完全不报错,后来查了一下午发现了问题 微信支付的sdk用mvn install的时候出现了一个文件wx-pay-sdk.pom 内容如下

<?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.github.wxpay</groupId>
    <artifactId>wxpay-sdk</artifactId>
    <version>3.1.0</version>
    <name>wxpay-sdk</name>
    <description>wxpay sdk</description>
    <url>https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=11_1</url>
 
    <properties>
        <project.build.sourceEncoding>utf-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>utf-8</project.reporting.outputEncoding>
    </properties>
 
    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.3</version>
        </dependency>
 
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.21</version>
        </dependency>
 
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.21</version>
        </dependency>
 
    </dependencies>
 
    <licenses>
        <license>
            <name>The BSD 3-Clause License</name>
            <url>https://opensource.org/licenses/BSD-3-Clause</url>
            <distribution>repo</distribution>
        </license>
    </licenses>
 
    <scm>
        <url></url>
        <connection></connection>
        <developerConnection></developerConnection>
    </scm>
 
    <developers>
        <developer>
            <name>wxpay</name>
            <email></email>
            <url>https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=11_1</url>
        </developer>
    </developers>
 
    <profiles>
        <profile>
            <id>release</id>
            <distributionManagement>
                <snapshotRepository>
                    <id>oss</id>
                    <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
                </snapshotRepository>
                <repository>
                    <id>oss</id>
                    <url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
                </repository>
            </distributionManagement>
            <build>
                <plugins>
                    <!-- Source -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-source-plugin</artifactId>
                        <version>3.0.1</version>
                        <executions>
                            <execution>
                                <phase>package</phase>
                                <goals>
                                    <goal>jar-no-fork</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                    <!-- Javadoc -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-javadoc-plugin</artifactId>
                        <version>2.10.4</version>
                        <executions>
                            <execution>
                                <phase>package</phase>
                                <goals>
                                    <goal>jar</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                    <!-- Gpg Signature -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-gpg-plugin</artifactId>
                        <version>1.6</version>
                        <executions>
                            <execution>
                                <id>sign-artifacts</id>
                                <phase>verify</phase>
                                <goals>
                                    <goal>sign</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
 
</project>

他标明SDK有三个依赖,我们如果直接打包拷贝到工程里,那么就必须在自己的pom.xml里加上这三个依赖。就好了

<dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.3</version>
        </dependency>
 
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.21</version>
        </dependency>
 
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.21</version>
        </dependency>

方法3:创建一个新的本地 Maven仓库

第三个方案和第一个很像,不同点在于JAR包安装到另外一个本地maven仓库中。

假设本地仓库名称为:maven-repository。位于${basedir}目录.

先将本地JAR包发布到新的本地仓库中

mvn deploy:deploy-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=jar
<repositories>
    <repository>
        <id>maven-repository</id>
        <url>file:///${project.basedir}/maven-repository</url>
    </repository>
</repositories>

方法4:用自己的远程仓库


最佳方式:使用Nexus仓库管理器
最好的方法是使用包含你自定义JAR包的Nexus仓库管理器,可将其用下载依赖的远程仓库。
这个可以单独开一篇了,不细聊


This entry was posted in JAVA and tagged , . Bookmark the permalink.
月小升QQ 2651044202, 技术交流QQ群 178491360
首发地址:月小升博客https://java-er.com/blog/maven-local-jar/
无特殊说明,文章均为月小升原创,欢迎转载,转载请注明本文地址,谢谢
您的评论是我写作的动力.

One Response to maven引入本地jar包的方法

  1. harries says:

    还是用远程仓库吧,本地的花,不然其他小伙伴都得来一遍

Leave a Reply

Your email address will not be published. Required fields are marked *

*

*