使用不同的profile将jar包发布到不同的仓库
我自己有一个私有maven仓库用于存储自己平时写的一些组件,有些组件我想开源到中央仓库,于是就遇到了将jar包发布到不同仓库的问题,这个问题可以通过使用不同的profile配置来解决
首先修改distributionManagement节点
<distributionManagement>
<snapshotRepository>
<id>${server.id}</id>
<url>${snapshot.url}</url>
</snapshotRepository>
<repository>
<id>${server.id}</id>
<url>${release.url}</url>
</repository>
</distributionManagement>
将原先写死的server id和url改为使用变量,这些变量后面会在profile里定义好,接着定义profile,profile可以写在settings.xml里,也可以写在pom文件里,如果不想暴露自己的私有仓库地址或者其他敏感信息,则profile建议写在settings.xml里。
<profiles>
<profile>
<id>local</id>
<properties>
<release.url>http://192.168.50.4/repository/maven-releases/</release.url>
<snapshot.url>http://192.168.50.4/repository/maven-snapshots/</snapshot.url>
<server.id>local</server.id>
</properties>
<repositories>
<repository>
<id>local-repo</id>
<name>local Packages</name>
<url>http://192.168.50.4/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
</repositories>
</profile>
</profiles>
发布到中央仓库需要用到两个额外的插件,而发布到私有仓库则不需要这两个插件,所以这两个额外插件的配置也可以放到中央仓库的profile里面
<profiles>
<profile>
<id>ossrh</id>
<properties>
<release.url>https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/</release.url>
<snapshot.url>https://s01.oss.sonatype.org/content/repositories/snapshots</snapshot.url>
<server.id>ossrh</server.id>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
<configuration>
<keyname>${gpg.keyname}</keyname>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.7</version>
<extensions>true</extensions>
<configuration>
<serverId>ossrh</serverId>
<nexusUrl>https://s01.oss.sonatype.org/</nexusUrl>
<autoReleaseAfterClose>true</autoReleaseAfterClose>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
公共插件则可以直接放到pom文件里
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.2.1</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9.1</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<additionalparam>-Xdoclint:none</additionalparam>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
这样我们定义了两个profile,deploy时通过-P参数指定profile就能发布到不同的仓库了
#发布到本地私有仓库
mvn clean deploy -Plocal
#发布到中央仓库
mvn clean deploy -Possrh