pom.xml plugin摘出来放到profile及推送到私服、需认证推送

1.关闭私服推送时的 GPG Sign

你当前 pom.xml 使用 maven-gpg-pluginverify 阶段为构件签名,这属于 OSS 发布的标准操作。如果推送到公司私服/内部 Nexus,签名不是必须,甚至会导致推送失败。

1.要关闭签名,需要去掉或禁用(一定要做):

1
2
3
4
5
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
...
</plugin>

推荐的不删除插件,而是禁用配置方式

2.新增profile来控制

pom.xml 末尾新增一个 profile,通过激活开关来控制是否签名:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<profiles>
<profile>
<id>release-sign</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
</plugin>
</plugins>
</build>
</profile>
</profiles>

使用发布签名时才执行:

1
mvn clean deploy -P release-sign

不加 -P 参数,默认不会签名,就能推送到私服。

2.关闭 OSS 推送任务(如不使用 OSS)

如果你只推送到私服,不需要 nexus-staging-maven-plugin

1
2
3
4
5
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
...
</plugin>

可以同样封装到 release-sign 的 profile 中,避免默认执行。

3.成果推送到私服

1. 修改 pom.xml,指向私服,不要推 OSS

例如你的私服地址是:

http://nexus.xxx.internal/repository/maven-releases/

则把 distributionManagement 改成私服:

1
2
3
4
5
6
7
8
9
10
<distributionManagement>
<repository>
<id>xxx-nexus</id>
<url>http://nexus.xxx.internal/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>xxx-nexus</id>
<url>http://nexus.xxx.internal/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>

xxx-nexus 将作为 settings.xml 认证的 key。


2. settings.xml 里添加私服账号密码认证(CI必配)

编辑 ~/.m2/settings.xml 或 CI 里的 settings:

1
2
3
4
5
6
7
<servers>
<server>
<id>xxx-nexus</id>
<username>admin</username>
<password>your_password</password>
</server>
</servers>

id 必须与 pom.xml<id>xxx-nexus</id> 一致。


4. 以后需要发布到 OSS 时怎么办?

使用 profile:

1
mvn clean deploy -P release-sign

5. 流程图

flowchart LR
A[mvn deploy] --> B{profile=release-sign?}

B -- Yes --> C[签名 + 推 OSS]
C --> D[Sonatype Staging]

B -- No --> E[不签名 + 推私服]
E --> F[Nexus Releases/Snapshots]

6.pom.xml指定路径构建

1.指定 pom 文件构建

当项目中存在多个 pom.xml 或你使用自定义名称,例如 featureprobe-server-starter.xml,可以用 -f 参数指定构建的 pom:

1
mvn clean install -f path/to/pom.xml

例如:

1
mvn clean deploy -f ./sdk/featureprobe-sdk-server/pom.xml

2.指定模块构建(多模块 parent 场景)

如果是多模块项目,可以直接指定模块路径:

1
mvn -pl module-name clean install

只构建当前模块及依赖模块:

1
mvn -pl module-name -am clean install

3.结合 profile + 指定 pom + deploy 示例

1
mvn -f ./sdk/featureprobe-sdk-server/pom.xml clean deploy -Dmaven.test.skip=true

发布 OSS:

1
mvn -f ./sdk/featureprobe-sdk-server/pom.xml clean deploy -P release-sign -Dmaven.test.skip=true

总结

场景 命令
指定 pom 构建 mvn -f path/to/pom.xml clean install
指定 pom 发布到私服 mvn -f pom.xml clean deploy
指定 pom 发布到 OSS mvn -f pom.xml clean deploy -P release-sign
多模块只构建某模块 mvn -pl module -am clean install

继续 CI 集成时可以自动注入 settings.xml 和 profile,达成构建统一与安全性提升。

7.执行构建推送

用私服推送更简洁:

1
mvn clean deploy -Dmaven.test.skip=true

执行 deploy 时:

  • 不会触发 GPG 签名
  • 不会走 Sonatype Staging
  • 默认推送私服成功

推进构建流畅,比如在公司 CI 中默认关闭签名,仅在发布 OSS 时启用 profile,这是未来可维护性的最佳实践。