一、maven打包本地jar包到本地仓库
使用场景
- maven依赖需要对本地自己写的jar包进行引用;
- 远程仓库不存在某jar包,手工加载后,打入仓库
执行指令
在控制台执行:
mvn install:install-file -Dfile=jar包的位置 -DgroupId=groupId -DartifactId=artifactId -Dversion=version -Dpackaging=jar
其中,groupId,artifactId,version三个字段值可以自定义赋值。但要保证跟pom.xml中对应。
打包命令执行成功后,maven执行reimport,检查pom.xml是否还有报错。
示例:
mvn install:install-file -Dfile=D:\setup\fastdfs-client-java-1.27-RELEASE.jar -DgroupId=org.csource -DartifactId=fastdfs-client-java -Dversion=1.27-SNAPSHOT -Dpackaging=jar
错误
Q:报错:[ERROR] The goal you specified requires a project to execute but there is no POM in this directory
A:
解决方案一:将相关参数加上引号
mvn install:install-file "-Dfile=D:\setup\fastdfs-client-java-1.27-RELEASE.jar" "-DgroupId=org.csource" "-DartifactId=fastdfs-client-java" "-Dversion=1.27-SNAPSHOT" "-Dpackaging=jar"
大神指出该问题主要是使用PowerShell执行install命令时的错误,且可能跟maven版本有关。
详见:
解决方案二:检查一下install命令是否全是英文字符
章节一参考:
https://www.cnblogs.com/panchanggui/p/10696458.html
二、pom.xml指定本地路径
<dependency>
<groupId>your.organization</groupId>
<artifactId>your-sdk-util</artifactId>
<version>1.1.1.0</version>
<scope>system</scope>
<systemPath>${pom.basedir}/lib/your-sdk-ver.jar</systemPath>
</dependency>
其中,${pom.basedir}表示项目根路径。groupId,artifactId,version自定义设置,但是scope必须设置成system。
注意
这样设置后pom.xml不会报错,但是如果需要使用maven进行打包时,scope为system的依赖包不会被maven打包进项目。
需要为pom.xml的打包插件配置includeSystemScope属性。
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
(未实际验证,参考:https://blog.csdn.net/beeworkshop/article/details/104727363)