我如何将多个 Maven 工件部署到多个 Nexus 存储库,包括快照存储库而无需指定配置文件?

How could I deploy multiple maven artifacts to multiple nexus repositories including snapshot repositories without having to specify a profile?

我们有一个网关客户端项目,它是多模块 Maven 项目的一部分。 gateway-client pom.xml 配置为创建两个主要工件:gateway-client.jar 和 gateway-services-client.jar 并将它们部署到两个独立的 Nexus 存储库:Releases repo 和 3rd Party回购分别。这是通过默认情况下处于活动状态的配置文件完成的:

<profile>
    <!-- ====================================================================== -->
    <!-- default Profile -->
    <!-- This is the default profile which will run by default.  This profile -->
    <!-- produces two client artifacts: gateway-client and gateway-services-client -->
    <!-- for the releases and thirdparty repositories respectively. -->
    <!-- ====================================================================== -->
    <id>default</id>
    <activation>
        <activeByDefault>true</activeByDefault>
    </activation>
    <!-- ====================================================================== -->
    <!-- default Profile Build plugins -->
    <!-- ====================================================================== -->
    <build>
        <plugins>
            <!-- ====================================================================== -->
            <!-- default Profile Maven deploy plugin -->
            <!-- ====================================================================== -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-deploy-plugin</artifactId>
                <configuration>
                    <skip>true</skip>
                </configuration>
                <executions>
                    <execution>
                        <id>deploy-thirdparty-jar</id>
                        <phase>deploy</phase>
                        <goals>
                            <goal>deploy-file</goal>
                        </goals>
                        <configuration>
                            <url>${nexus.url}/content/repositories/thirdparty</url>
                            <repositoryId>thirdparty</repositoryId>
                            <file>${project.build.directory}/${project.build.finalName}.${project.packaging}</file>
                            <groupId>${project.groupId}</groupId>
                            <artifactId>gateway-services-client</artifactId>
                            <version>${project.version}</version>
                            <packaging>jar</packaging>
                            <generatePom>true</generatePom>
                        </configuration>
                    </execution>
                    <execution>
                        <id>deploy-release-jar</id>
                        <phase>deploy</phase>
                        <goals>
                            <goal>deploy-file</goal>
                        </goals>
                        <configuration>
                            <url>${nexus.url}/content/repositories/releases</url>
                            <repositoryId>releases</repositoryId>
                            <file>${project.build.directory}/${project.build.finalName}.${project.packaging}</file>
                            <groupId>${project.groupId}</groupId>
                            <artifactId>gateway-client</artifactId>
                            <version>${project.version}</version>
                            <packaging>jar</packaging>
                            <generatePom>true</generatePom>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</profile>

问题在于,由于此配置文件默认处于活动状态,如果我们尝试 运行 mvn deploy 并且 GAV 坐标的版本是 -SNAPSHOT,构建无意中仍会尝试部署到 Nexus 3rd Party 并发布 repos 并失败,因为它当然不会接受 -SNAPSHOT 工件版本。为了解决这个问题,我专门为 -SNAPSHOT 版本设置了一个配置文件,它只会部署到快照存储库:

<profile>
    <!-- ====================================================================== -->
    <!-- snapshot Profile -->
    <!-- Activating this profile will automatically deactivate the default profile. -->
    <!-- The purpose of this profile is to produce a a gateway-services-client and gateway-client -->
    <!-- snapshot artifacts and deploy them to the snapshots Nexus repository where they can -->
    <!-- act as the latest development dependencies for other projects -->
    <!-- ====================================================================== -->
    <id>snapshot</id>
    <activation>
        <activeByDefault>false</activeByDefault>
    </activation>
    <!-- ====================================================================== -->
    <!-- snapshot profile Build plugins -->
    <!-- ====================================================================== -->
    <build>
        <plugins>
            <!-- ====================================================================== -->
            <!-- snapshot profile Maven deploy plugin -->
            <!-- ====================================================================== -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-deploy-plugin</artifactId>
                <configuration>
                    <skip>true</skip>
                </configuration>
                <executions>
                    <execution>
                        <id>deploy-thirdparty-snapshot-jar</id>
                        <phase>deploy</phase>
                        <goals>
                            <goal>deploy-file</goal>
                        </goals>
                        <configuration>
                            <url>${nexus.url}/content/repositories/snapshots</url>
                            <repositoryId>snapshots</repositoryId>
                            <file>${project.build.directory}/${project.build.finalName}.${project.packaging}</file>
                            <groupId>${project.groupId}</groupId>
                            <artifactId>gateway-services-client</artifactId>
                            <version>${project.version}</version>
                            <packaging>jar</packaging>
                            <generatePom>true</generatePom>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</profile>

这样做的问题是您必须在执行 Maven 命令时指定配置文件:mvn deploy -P 'snapshot'。我的问题是我能做什么,我所要做的就是 运行 mvn deploy 而不指定快照配置文件,并让构建自动部署到快照存储库或第 3 方和发布存储库所有基于在 GAV 坐标版本中是否存在 -SNAPSHOT?

你不能用配置文件来做。来自 Maven 文档:

A profile can be triggered/activated in several ways:

  • Explicitly
  • Through Maven settings
  • Based on environment variables
  • OS settings
  • Present or missing files

所以你不能按照你想要的方式去做。然而,我们一直这样做。我们的设置是在我们的 super-pom

中使用以下内容
 <distributionManagement>
   <repository>
     <id>deploymentRepo</id><!-- key in settings.xml -->
     <name>Releases</name>
     <uniqueVersion>false</uniqueVersion>
     <url>${repos.release}</url>
     <layout>default</layout>
   </repository>
   <snapshotRepository>
     <id>deploymentRepo</id>
     <name>Snapshots</name>
     <uniqueVersion>true</uniqueVersion>
     <url>${repos.snapshot}</url>
     <layout>default</layout>
   </snapshotRepository>
 </distributionManagement>

请注意 ID 相同,因为两个存储库使用相同的凭据。

我们还在使用 nexus,其中每个存储库都配置为快照或发布,仅此而已,maven 能够知道 *-SNAPSHOT 转到快照存储库。

换句话说,只要同时给出两个选项,不要将它们放在互斥的配置文件中,maven 会知道以哪种方式发送它们。如果没有,请尝试回购管理器

我想到的唯一解决方案是使用属性并在部署期间添加三个执行。丑陋的是,在 SNAPSHOT 的情况下,您的工件将被部署到同一个存储库两次。

您可以执行以下操作:

<plugin>
    <groupId>org.codehaus.groovy.maven</groupId>
    <artifactId>gmaven-plugin</artifactId>
    <version>1.0</version>
    <executions>
        <execution>
            <id>eval-repo</id>
            <phase>initialize</phase>
            <goals>
                <goal>execute</goal>
            </goals>
            <configuration>
                <source>
                    if (project.version.endsWith("-SNAPSHOT")){
                    project.properties.repoId = "snapshots";
                    project.properties.repoUrl = "snapshots url";
                    project.properties.thirdPartyRepoId =   "snapshots";
                    project.properties.thirdPartyRepoUrl = "snapshots url";                             
                    }
                    else {
                    project.properties.repoId = "releases";
                    project.properties.repoUrl = "releases url";
                    project.properties.thirdPartyRepoId =   "thirdparty";
                    project.properties.thirdPartyRepoUrl = "thirdparty url";                                    
                    }
                </source>
            </configuration>
        </execution>
    </executions>
</plugin>

然后添加三个执行,配置如下:

    <configuration>
        <artifactId>gateway-client</artifactId>
        <url>${repoUrl}</url>
        <repositoryId>${repoId}</repositoryId>
        ...

    <configuration>
        <artifactId>gateway-services-client</artifactId>
        <url>${repoUrl}</url>
        <repositoryId>${repoId}</repositoryId>
        ...

    <configuration>
        <artifactId>gateway-services-client</artifactId>
        <url>${thirdPartyRepoId}</url>
        <repositoryId>${thirdPartyRepoUrl}</repositoryId>
        ...