Java 如何指定 maven 的分布管理组织范围?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3298135/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 21:51:26  来源:igfitidea点击:

How to specify maven's distributionManagement organisation wide?

javamavendeploymentmaven-2nexus

提问by mglauche

I'm trying to figure out how to organize many (around 50+) maven2 projects, so that they can deploy into a central nexus repository. When using the mvn deploygoal, one does need to specify the target in the distributionManagement tag like this:

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

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

Now, i don't want every single pom.xml (of those 50+) to contain this block over and over again. My first though would be the settings.xmlfile, but it seems it is not possible (by design) to define it there. So, the first question would be, why is that the case ? If it would be possible i could specify it in the settings.xml in the maven2 distribution, which could be distributed to all developers.

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

The only possible solution i've found was to create an organisation-wide master-pom project, that does contain these settings, and make all other pom.xml depend on this master-pom via <parent>tag. But this looks kind of strange in multi-module builds:

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

- 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)

Usually i read in all documentation that the module poms should use the parent pom, not some different one. But after reading the maven website about Inheritance v. Aggregation it is written that it is indeed possible.

通常我在所有文档中都读到模块 poms 应该使用父 pom,而不是一些不同的。但是在阅读了关于继承诉聚合的 maven 网站后,有人写道这确实是可能的。

One problem i found was with the maven site generation, which does seem to have problems with this setup (modules does not get linked correctly if they have no direct back-reference)

我发现的一个问题是 Maven 站点生成,它似乎在此设置中存在问题(如果模块没有直接反向引用,则无法正确链接)

So, is this a valid approach ? Any other, more obvious, simpler solution to the problem ?

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

采纳答案by Jesse Webb

The best solution for this is to create a simple parent pom file project (with packaging 'pom') generically for all projects from your organization.

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

This can be built, released, and deployed to your local nexus so everyone has access to its artifact.

这可以构建、发布和部署到您的本地连接,因此每个人都可以访问其工件。

Now for all projects which you wish to use it, simply include this section:

现在对于您希望使用它的所有项目,只需包含此部分:

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

This solution will allow you to easily add other common things to all your company's projects. For instance if you wanted to standardize your JUnit usage to a specific version, this would be the perfect place for that.

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

If you have projects that use multi-module structures that have their own parent, Maven also supports chaining inheritance so it is perfectly acceptable to make your project's parent pom file refer to your company's parent pom and have the project's child modules not even aware of your company's parent.

如果您的项目使用具有自己父级的多模块结构,Maven 还支持链式继承,因此完全可以让项目的父 pom 文件引用您公司的父 pom 并且项目的子模块甚至不知道您的公司的母公司。

I see from your example project structure that you are attempting to put your parent project at the same level as your aggregator pom. If your project needs its own parent, the best approach I have found is to include the parent at the same level as the rest of the modules and have your aggregator pom.xml file at the root of where all your modules' directories exist.

我从您的示例项目结构中看到,您正试图将父项目与聚合器 pom 放在同一级别。如果您的项目需要它自己的父级,我发现的最佳方法是将父级包含在与其余模块相同的级别,并将聚合器 pom.xml 文件放在所有模块目录所在的根目录下。

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

What you do with this structure is include your parent module in the aggregator and build everything with a mvn installfrom the root directory.

你用这个结构做的是将你的父模块包含在聚合器中,并使用mvn install根目录中的a 构建所有内容。

We use this exact solution at my organization and it has stood the test of time and worked quite well for us.

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

回答by Michael Wyraz

There's no need for a parent POM.

不需要父 POM。

You can omit the distributionManagement part entirely in your poms and set it either on your build server or in settings.xml.

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

To do it on the build server, just pass to the mvncommand:

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

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

See https://maven.apache.org/plugins/maven-deploy-plugin/deploy-mojo.htmlfor details which options can be set.

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

It's also possible to set this in your settings.xml.

也可以在您的settings.xml.

Just create a profile there which is enabled and contains the property.

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

Example 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>

Make sure that credentials for "snapshots" and "releases" are in the <servers>section of your settings.xml

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

The properties altSnapshotDeploymentRepository and altReleaseDeploymentRepository are introduced with maven-deploy-plugin version 2.8. Older versions will fail with the error message

maven-deploy-plugin 2.8 版引入了属性 altSnapshotDeploymentRepository 和 altReleaseDeploymentRepository。旧版本将失败并显示错误消息

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

To fix this, you can enforce a newer version of the plug-in:

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

        <build>
          <pluginManagement>
            <plugins>
              <plugin>
                <artifactId>maven-deploy-plugin</artifactId>
                <version>2.8</version>
              </plugin>
            </plugins>
          </pluginManagement>
        </build>