代码之家  ›  专栏  ›  技术社区  ›  mglauche

如何在整个组织范围内指定maven的distributionManagement?

  •  94
  • mglauche  · 技术社区  · 14 年前

    我试图弄清楚如何组织许多(大约50+个)maven2项目,以便它们可以部署到中央nexus存储库中。当使用 mvn deploy 目标,确实需要在distributionManagement标记中指定如下目标:

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

    现在,我不希望每一个pom.xml(这50+中的一个)反复包含这个块。我的第一个想法是 settings.xml 文件,但似乎不可能(按设计)在那里定义它。 所以,第一个问题是,为什么会这样?如果可能的话,我可以在maven2发行版的settings.xml中指定它,它可以分发给所有开发人员。

    我找到的唯一可能的解决方案是创建一个组织范围的主pom项目,该项目包含这些设置,并使所有其他pom.xml通过 <parent> 标签。但这在多模块构建中看起来有点奇怪:

    - master configuration POM (pm)
    - Project 1 parent pom (p1 with module 1 and module 2 as modules)
        - Project 1 module pom (with pm as parent)
        - Project 2 module pom (with pm as parent)
    

    我发现的一个问题是maven站点生成的问题,这个设置似乎有问题(如果模块没有直接的反向引用,它们就不能正确链接)

    那么,这是一种有效的方法吗?还有其他更明显更简单的解决方案吗?

    1 回复  |  直到 8 年前
        1
  •  146
  •   Neeraj Singh    6 年前

    最好的解决方案是为组织中的所有项目创建一个简单的父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>
    

    可以构建、发布并部署到本地nexus,这样每个人都可以访问它的工件。

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

    此解决方案将允许您轻松地将其他常见的东西添加到公司的所有项目中。例如,如果您想将JUnit的使用标准化为一个特定的版本,这将是一个完美的地方。

    - pom.xml (aggregator)
        - project-parent
        - project-module1
        - project-module2
    

    使用此结构所做的是将父模块包含在聚合器中,并使用 mvn install

    我们在我的组织中使用这个精确的解决方案,它经受了时间的考验,对我们来说非常有效。

        2
  •  55
  •   hjoeren    4 年前

    您可以在poms中完全省略distributionManagement部分,并在构建服务器或settings.xml中进行设置。

    要在构建服务器上执行此操作,只需传递到 mvn 命令:

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

    https://maven.apache.org/plugins/maven-deploy-plugin/deploy-mojo.html 有关可设置哪些选项的详细信息。

    也可以在您的 settings.xml

    只需在那里创建一个已启用并包含属性的概要文件。

    示例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>
    

    确保“快照”和“版本”的凭据位于 <servers> settings.xml的部分

    属性altSnapshotDeploymentRepository和altReleaseDeploymentRepository是在maven deploy插件版本2.8中引入的。旧版本将失败并显示错误消息

    Deployment failed: repository element was not specified in the POM inside distributionManagement element or in -DaltDeploymentRepository=id::layout::url parameter
    

    要解决此问题,可以强制使用更新版本的插件:

            <build>
              <pluginManagement>
                <plugins>
                  <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.8</version>
                  </plugin>
                </plugins>
              </pluginManagement>
            </build>
    
        3
  •  1
  •   Robert Newton    4 年前

    关于 answer 从迈克尔·怀拉兹那里 alt*DeploymentRepository 在你的 settings.xml bug 在此版本中,可能会导致服务器身份验证问题。

    解决方法如下。在值中:

    releases::default::https://YOUR_NEXUS_URL/releases
    

    default 部分,使其:

    releases::https://YOUR_NEXUS_URL/releases
    

    以前的版本2.8.2没有这个bug。