3

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

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

问题是因为这个配置文件在默认情况下是活动的,如果我们尝试运行 amvn 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 方并发布存储库,所有这些都基于版本中 -SNAPSHOT 的存在GAV 坐标?

4

2 回答 2

2

我想到的唯一解决方案是使用属性并在部署期间添加三个执行。丑陋的事情是,在 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>
        ...
于 2015-09-03T19:55:54.720 回答
0

你不能用配置文件来做到这一点。来自 Maven 文档:

可以通过多种方式触发/激活配置文件:

  • 明确地
  • 通过 Maven 设置
  • 基于环境变量
  • 操作系统设置
  • 存在或丢失文件

所以你不能按照你想要的方式去做。但是,我们一直这样做。我们的设置是在我们的超级 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 是相同的,因为两个 repos 使用相同的凭据。

我们还使用了 nexus,其中每个 repo 都配置为快照或发布,只有这样,maven 才能知道 *-SNAPSHOT 进入快照 repo。

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

于 2015-05-01T16:02:47.117 回答