Maven- 组织内部项目统一配置 DistributionManagement

本贴最后更新于 1507 天前,其中的信息可能已经物是人非

需求

假设公司内部有非常多 Maven 项目,需要 deploy 到一个内部 maven 私有仓库中。
如果希望 maven deploy 命令可以成功执行,一般需要在 pom.xml 中添加:

<distributionManagement>
   <repository>
      <id>nexus-site</id>
      <url>http://central_nexus/server</url>
   </repository>
</distributionManagement>

但需要 deploy 的项目很多的情况下,我们肯定不希望在每个项目的 pom 文件中都重复添加这个配置。

方案一

为所有项目增加一个公共的 parent pom 项目。那么只需要在这个项目的 pom 文件中添加:

<?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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>your.company</groupId>
    <artifactId>company-parent</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <distributionManagement>
        <repository>
            <id>nexus-site</id>
            <url>http://central_nexus/server</url>
        </repository>
    </distributionManagement>

</project>

然后使其他项目的 parent 项目变成这个项目:

<parent>
  <groupId>your.company</groupId>
  <artifactId>company-parent</artifactId>
  <version>1.0.0</version>
</parent>

方案二

方案一存在两个问题:

  • 如果代码泄露或将代码开源,会使该内部私有仓库的地址被暴露
  • 私有仓库这种环境配置信息最好和代码分离。类似通过配置中心,将数据库地址等配置和代码分离。

我们完全可以将这个配置放到 maven 中。
可以通过 mvn 命令的启动参数来实现:

-DaltSnapshotDeploymentRepository=snapshots::default::https://YOUR_NEXUS_URL/snapshots
-DaltReleaseDeploymentRepository=releases::default::https://YOUR_NEXUS_URL/releases

更好的方法是将其配在 settings.xml 中:

<settings>
[...]
  <profiles>
    <profile>
      <id>nexus</id>
      <properties>
        <altSnapshotDeploymentRepository>snapshots::default::https://YOUR_NEXUS_URL/snapshots</altSnapshotDeploymentRepository>
        <altReleaseDeploymentRepository>releases::default::https://YOUR_NEXUS_URL/releases</altReleaseDeploymentRepository>
      </properties>
    </profile>
  </profiles>

  <activeProfiles>
    <activeProfile>nexus</activeProfile>
  </activeProfiles>

</settings>

不要忘记也在 <;server>;<;/server>; 之间加上 snapshotsreleases 的账号。

参考资料

java - How to specify maven’s distributionManagement organisation wide? - Stack Overflow

  • Maven

    Maven 是基于项目对象模型(POM)、通过一小段描述信息来管理项目的构建、报告和文档的软件项目管理工具。

    185 引用 • 318 回帖 • 353 关注

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...