idea上传jar包到maven私服
原创
©著作权归作者所有:来自51CTO博客作者MonkeyKing_sun的原创作品,请联系作者获取转载授权,否则将追究法律责任
项目开发会涉及到开发一些公用的jar包,开发完jar包就能直接上传到maven私服上,这样其他工程有需要jar包只需要升级相应的依赖即可。
1、第一步搭建nexus3的maven私服仓库,具体搭建过程可以查看相应的网上教程。
安装教程
这里强调一点:为了下载jar包比较快,这个里中心仓库建议修改为阿里云的仓库。配置的地方如下图

2、配置maven的setting.xml
关键信息的配置
<localRepository>E:\maven\mavengit</localRepository>
<server>
<id>maven-snapshots</id>
<username>admin</username>
<password>XXXXX</password>
</server>
<server>
<id>maven-releases</id>
<username>admin</username>
<password>XXXXXX</password>
</server>
</servers>
<mirrors>
<!--内部maven-->
<mirror>
<id>central</id>
<mirrorOf>*</mirrorOf>
<name>central Repository</name>
<url>http://你的maven私服的网络地址/repository/nuget-group/</url>
</mirror>
<!--阿里云仓库-->
<mirror>
<id>aliyunmaven</id>
<mirrorOf>central</mirrorOf>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
</mirror>
<!--中央仓库1-->
<mirror>
<id>repo1</id>
<mirrorOf>central</mirrorOf>
<name>central maven</name>
<url>http://repo1.maven.org/maven2/</url>
</mirror>
<!--中央仓库2-->
<mirror>
<id>repo2</id>
<mirrorOf>central</mirrorOf>
<name>central maven</name>
<url>http://repo2.maven.org/maven2/</url>
</mirror>
</mirrors>
3、然后修改工程的pom.xml文件,文件修改如下:
编写的jar包的基本配置信息:
<groupId>com.neusoft.shg3.swamp.framework</groupId>
<artifactId>HTableTemplate</artifactId>
<version>2.2-RELEASE</version>
<packaging>jar</packaging>
发布到maven私服上去的配置信息:
<distributionManagement>
<repository>
<!--此名称要和.m2/settings.xml中设置的ID一致 -->
<id>maven-releases</id>
<name>Nexus Release Repository</name>
<url>http://你的maven私服的网络地址/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<!--此名称要和.m2/settings.xml中设置的ID一致 -->
<id>maven-snapshots</id>
<name>Nexus snapshots Repository</name>
<url>http://你的maven私服的网络地址/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
<build>
<plugins>
<!--发布代码Jar插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.7</version>
</plugin>
<!--发布源码插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
4、使用idea的mvn deploy命令,将代码发布到maven私服中去。

5、登录到maven私服,查看已经发布的jar包,找到具体的maven依赖关系。

注意事项:
1、同一个版本不可以提交多次,修改了需要提交,请修改版本号。
如果强制提交,idea会报400错误
Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy (default-deploy) on project HTableTemplate: Failed to deploy artifacts: Could not transfer artifact com.neusoft.shg3.swamp.framework:HTableTemplate:jar:2.2-RELEASE from/to maven-releases (http://maven.geexek.com.cn/repository/maven-releases/): Transfer failed for
2、如果idea出现400错误或者401错误,请检查setting.xml或者pom.xml中的id和URL是否配置的是一致的,是不是和maven私服的仓库名字是一致的。
如果出现401错误,也可以参考这篇文章
https://www.jianshu.com/p/411ea297990b