如何在不必指定配置文件的情况下将多个maven工件部署到多个nexus存储库(包括快照存储库)?

时间:2015-04-30 18:50:39

标签: maven nexus maven-deploy-plugin

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

<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版本设置了一个配置文件,该版本只会部署到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而不指定快照配置文件,并让构建自动部署到快照存储库或第三方和发布存储库,所有这些都基于前提-GNAPSHOT在GAV坐标的版本中?

2 个答案:

答案 0 :(得分: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>
        ...

答案 1 :(得分:0)

您不能使用个人资料。来自maven doc:

  

可以通过多种方式触发/激活个人资料:

     
      
  • 显式
  •   
  • 通过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会转到快照仓库。

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