最近做项目,需要把项目编译成jar发布到maven私服上,方便别的应用加载使用。查了些资料,终于搞定,mark一下
步骤一、编译服务器或自己的开发电脑,配置settings.xml。
1、centos找到/etc/maven下的settings.xml。window电脑找到maven的安装目录下conf下的settings.xml。
2、找到servers节点,配置下面一段脚本
<servers>
<!-- server
| Specifies the authentication information to use when connecting to a particular server, identified by
| a unique name within the system (referred to by the 'id' attribute below).
|
| NOTE: You should either specify username/password OR privateKey/passphrase, since these pairings are
| used together.
|
-->
<server>
<id>nexus-xxx</id>
<username>admin</username>
<password>xxxxxx</password>
</server>
<!-- Another sample, using keys to authenticate.
<server>
<id>siteServer</id>
<privateKey>/path/to/private/key</privateKey>
<passphrase>optional; leave empty if not used.</passphrase>
</server>
-->
</servers>
步骤二、配置项目pom.xml文件
<?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.xxx.test</groupId>
<artifactId>model-jar</artifactId>
<version>1.0</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<!--下面两个配置不会将源包上传-->
<!--<maven.javadoc.skip>true</maven.javadoc.skip>
<maven.source.skip>true</maven.source.skip>-->
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<attach>true</attach>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<distributionManagement>
<!--稳定版本的仓库地址,必须是允许上传的私服地址-->
<repository>
<!--id和maven setting文件server id保持一致则不需要配置用户名密码,否则需要在pom中配置用户名密码-->
<id>nexus-xxx</id>
<url>http://maven.xxx.com/repository/xxx/</url>
</repository>
</distributionManagement>
</project>
步骤三、编译
使用下面脚本编译
mvn clean package deploy
发布到maven服务器上版本如下
另外,该项目通过jenkins编译,需要传参给pom文件修改版本号,jenkins界面配置参数化编译,这里不再赘述。
1、编译命令,传入参数
mvn clean package deploy -Dver=${VERSION_CODE}
2、pom文件接收参数
在上面pom文件中修改下面两处
<groupId>com.xxx.test</groupId>
<artifactId>model-jar</artifactId>
<!--1 在上面的文件中修改-->
<version>${ver}</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<!--下面两个配置不会将源包上传-->
<!--<maven.javadoc.skip>true</maven.javadoc.skip>
<maven.source.skip>true</maven.source.skip>-->
<!--2 在上面的文件中添加下面一行-->
<ver>0.1</ver>
<maven-jar-plugin.version>3.8.1</maven-jar-plugin.version>
</properties>