Java Maven构建和发布多个相互依赖的项目

Java Maven构建和发布多个相互依赖的项目,java,maven,release,Java,Maven,Release,我有一些现有的Maven项目 它们是: aaa(插件) bbb(罐) ccc(罐) ddd(战争) eee(战争) 项目ddd是针对一个客户的,eee是针对另一个客户的 它们位于磁盘上workspace/文件夹下的平面结构中,与svn repo内部的结构完全相同 这是项目的依赖关系层次结构: ddd (war) aaa (plugin) bbb (jar) ccc (jar) bbb (jar) eee (war) ccc (ja

我有一些现有的Maven项目

它们是:

  • aaa(插件)
  • bbb(罐)
  • ccc(罐)
  • ddd(战争)
  • eee(战争)
  • 项目
    ddd
    是针对一个客户的,
    eee
    是针对另一个客户的

    它们位于磁盘上
    workspace/
    文件夹下的平面结构中,与svn repo内部的结构完全相同

    这是项目的依赖关系层次结构:

    ddd (war)        
        aaa (plugin)
        bbb (jar)
        ccc (jar)
            bbb (jar)
    
    eee (war)
        ccc (jar)
            bbb (jar)
    
    当只有
    war
    s是
    SNAPSHOT
    s时,构建和发布没有问题

    否则,即

    ddd       1.1-SNAPSHOT (war)        
      aaa     2.0          (plugin)
      bbb     2.1-SNAPSHOT (jar)
      ccc     1.3-SNAPSHOT (jar)
        bbb   2.1-SNAPSHOT (jar)
    
    我必须做的是,为了建设:

  • bbb>mvn安装
  • ccc>mvn安装
  • ddd>mvn编译
  • 以及发布:

  • bbb>mvn发布:准备发布:执行
  • ccc>mvn版本:使用发行版
  • ccc>svn ci-m“更新的快照依赖项”
  • ccc>mvn发布:准备发布:执行
  • ddd>mvn版本:使用发行版
  • ddd>svn ci-m“更新的快照依赖项”
  • ddd>mvn发布:准备发布:执行
  • 我试过使用聚合器,但是

    • 在构建时,它编译非快照依赖项(aaa 2.0->编译aaa 2.1-SNAPSHOT)
    • 发布时,它会抱怨scm,但我不希望聚合器位于svn中
    这就是我需要的:

    • 单命令构建
      • 订单快照依赖项
      • 安装(或部署)每个快照依赖项
      • 构建(编译、打包或安装…)根工件
    • 单命令释放
      • 订单快照依赖项
      • 释放每个快照依赖项
      • 释放根伪影
    这就是我不想要的:

    • 批生成/发布脚本
    • 在svn中放置聚合器
    • 项目之间的版本共享
    • 父对象也成为聚合器
    这可能吗?

    其他最佳实践?(也许詹金斯会帮忙?

    我应该换成Gradle吗??
    更新

    我发现你们中的大多数人把家长聚合器混淆了,所以我把它从这个问题中删除了。但是,

    […]您经常会看到既是父级又是聚合器的项目。[…]然而,尽管POM项目、聚合器项目和父项目不是一个项目,不应混淆。POM项目可以从它聚合的任何模块继承,但不一定有。相反,POM项目可能会聚合不从其继承的项目


    中,您没有提到在父POM中使用“模块”标记。我建议您应该使用它,这样您就可以指定项目构建过程的队列

    要解决当前问题,请创建单独的模块/项目。在pom中,指定用于触发ant操作的概要文件。这应该能奏效

    您可以在父目录上调用mvn命令,如下所示:

    mvn clean install -Pdeploy
    

    根据您的依赖项更改SVN中的结构:

    在parent中,您必须定义模块列表,并且必须在适当的模块中定义模块之间的依赖关系

    parent (pom.xml)
     +-- aaa (plugin)
     +-- bbb (jar)
     +-- ccc (jar)
     +-- ddd (war)
     +-- eee (war)
    
    所有模块(如3.0-SNAPSHOT)的版本应相同,之后您可以从根位置简单地发布/部署等


    如果不是这样,您应该通过Jenkins单独发布所有这些项目。

    您可以切换到Gradle,但可能更容易继续使用Maven,只需稍作修改:

    • 在指定所有子模块的父级pom中添加
    • 将子POM中的父版本设置为与父版本相同,例如
      1.0.0-SNAPSHOT
    • 将其他版本包括的项目,即
      bbb
      ccc
      添加到父pom的
      部分,将其版本设置为
      ${project.version}
      ,并在
      ddd
      eee
      中删除其特定版本
    • 同样,将
      aaa
      插件添加到父pom的
      部分,将其版本设置为
      ${project.version}
      ,并从其他项目中删除特定版本

    现在,您可以一次性构建和发布所有构建,例如使用。

    现在,您的模块可能过于细粒度,但我个人认为单独构建是构建系统的方式,原因如下

    父pom的主要工作是表示下一个版本。该版本可能是一个涉及多个内部模块的功能版本,也可能是一个只更改一个模块的bug修复版本

    鉴于此,循环如下:

  • 修改父pom的版本以对应下一个“版本”
  • 作为版本的一部分,更新正在修改的模块版本
  • 更新模块以使用快照修改的父级并进行更改
  • 将父级中的模块版本更新为其发布版本,然后发布父级
  • 更新每个模块以使用已发布的父pom并发布模块
  • 这种方法的好处是,在未修改模块的情况下,您可以使用以前发布的模块版本(从您的发布回购中获得,因此这不是100%保证),而无需重建(多模块方法可能会发生),从而减少回归测试开销

    如果随着每个版本而改变的模块数量成为一个负担,那么可能是时候检查您的模块结构了,但是这种方法的好处是父pom将所有模块版本对齐
    Root layout:
    <project>
      <modules>
        <module>a</module>
        <module>ant</module>
        <module>fish</module>
        <module>fish/salmon</module>
        <module>fish/shark</module>
    </modules>
    
    <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/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.mycompany</groupId>
        <artifactId>test-release-parent</artifactId>
        <version>1-SNAPSHOT</version>
        <packaging>pom</packaging>
    
        <name>test-release-parent</name>
        <description>This is the parent pom to test release procedure</description>
        <inceptionYear>2014</inceptionYear>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <svn.repository>http://svn.mycompany.com/repo1</svn.repository>
            <web.projects>http://www2.mycompany.com/projects</web.projects>
        </properties>
    
        <scm>
            <developerConnection>scm:svn:${svn.repository}/${project.artifactId}</developerConnection>
            <url>${svn.repository}/${project.artifactId}</url>
        </scm>
    
        <distributionManagement>
            <repository>
                <id>ftp.mycompany.com</id>
                <name>mycompany Maven Repository</name>
                <url>ftp://ftp.mycompany.com/maven-repo</url>
            </repository>
        </distributionManagement>
    
        <repositories>
            <repository>
                <id>www2.mycompany.com</id>
                <name>mycompany Maven Repository</name>
                <url>http://www2.mycompany.com/maven-repo</url>
            </repository>
        </repositories>
    
        <pluginRepositories>
            <pluginRepository>
                <id>www2.mycompany.com</id>
                <name>mycompany Maven Repository</name>
                <url>http://www2.mycompany.com/maven-repo</url>
            </pluginRepository>
        </pluginRepositories>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>commons-io</groupId>
                    <artifactId>commons-io</artifactId>
                    <version>2.4</version>
                </dependency>
                <dependency>
                    <groupId>commons-codec</groupId>
                    <artifactId>commons-codec</artifactId>
                    <version>1.9</version>
                </dependency>
            </dependencies>
        </dependencyManagement>
    
        <build>
            <pluginManagement>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-scm-plugin</artifactId>
                        <version>1.9</version>
                    </plugin>
    
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>versions-maven-plugin</artifactId>
                        <version>2.1</version>
                        <configuration>
                            <excludes>
                                <exclude>javax:javaee-api:*:*</exclude>
                                <exclude>org.eclipse.persistence:*:*:*</exclude>
                                <exclude>org.hibernate:hibernate-validator:*:*</exclude>
                            </excludes>
                            <rulesUri>http://www.mycompany.com/ruleset.xml</rulesUri>
                        </configuration>
                    </plugin>
    
                    <plugin>
                        <groupId>org.codehaus.gmaven</groupId>
                        <artifactId>gmaven-plugin</artifactId>
                        <version>1.5</version>
                    </plugin>
                </plugins>
            </pluginManagement>
    
            <plugins>
                <plugin>
                    <groupId>org.codehaus.gmaven</groupId>
                    <artifactId>gmaven-plugin</artifactId>
                    <configuration>
                        <source>
                            String releaseVersion = project.version.substring(0, project.version.indexOf("-"));
    
                            String nextVersion;
                            int index = releaseVersion.lastIndexOf(".");
                            if(index == -1) nextVersion = (releaseVersion.toInteger() + 1) + "-SNAPSHOT";
                            else
                            {
                                String prefix = releaseVersion.substring(0, index);
                                String suffix = releaseVersion.substring(index + 1);
    
                                nextVersion = prefix + "." + (suffix.toInteger() + 1) + "-SNAPSHOT";
                            }
    
                            ant.exec(failonerror: "true", dir: "${basedir}", executable: "cmd")
                            {
                                arg(value: "/c")
                                arg(value: "mvn")
                                arg(value: "validate")
                                arg(value: "-Prelease-align")
                                arg(value: "-Dversion.release=" + releaseVersion)
                                arg(value: "-Dversion.next=" + nextVersion)
                            }
    
                            ant.exec(failonerror: "true", dir: "${basedir}", executable: "cmd")
                            {
                                arg(value: "/c")
                                arg(value: "mvn")
                                arg(value: "initialize")
                                arg(value: "-Prelease-prepare")
                                arg(value: "-Dversion.release=" + releaseVersion)
                                arg(value: "-Dversion.next=" + nextVersion)
                            }
    
                            ant.exec(failonerror: "true", dir: "${basedir}", executable: "cmd")
                            {
                                arg(value: "/c")
                                arg(value: "mvn")
                                arg(value: "deploy")
                                arg(value: "-Prelease-perform")
                                arg(value: "-Dversion.release=" + releaseVersion)
                                arg(value: "-Dversion.next=" + nextVersion)
                            }
                        </source>
                    </configuration>
                </plugin>
            </plugins>
    
            <extensions>
                <extension>
                    <groupId>org.apache.maven.wagon</groupId>
                    <artifactId>wagon-ftp</artifactId>
                    <version>2.6</version>
                </extension>
            </extensions>
        </build>
    
        <profiles>
            <profile>
                <id>buildall</id>
                <build>
                    <plugins>
                        <plugin>
                            <groupId>org.codehaus.gmaven</groupId>
                            <artifactId>gmaven-plugin</artifactId>
                            <executions>
                                <execution>
                                    <phase>validate</phase>
                                    <goals>
                                        <goal>execute</goal>
                                    </goals>
                                    <configuration>
                                        <source>
                                            for(d in project.dependencies)
                                            {
                                                if(d.groupId == "com.mycompany" &amp;&amp; d.version.endsWith("-SNAPSHOT"))
                                                {
                                                    println "installing " + d
                                                    ant.exec(failonerror: "true", dir: "${basedir}/../" + d.artifactId, executable: "cmd")
                                                    {
                                                        arg(value: "/c")
                                                        arg(value: "mvn")
                                                        arg(value: "install")
                                                        arg(value: "-Pbuildall")
                                                    }
                                                }
                                            }
                                        </source>
                                    </configuration>
                                </execution>
                            </executions>
                        </plugin>
                    </plugins>
                </build>
            </profile>
    
            <profile>
                <id>release-align</id>
                <build>
                    <plugins>
                        <plugin>
                            <groupId>org.codehaus.mojo</groupId>
                            <artifactId>versions-maven-plugin</artifactId>
                            <executions>
                                <execution>
                                    <id>initial-updates</id>
                                    <phase>validate</phase>
                                    <goals>
                                        <goal>update-parent</goal>
                                        <goal>use-releases</goal>
                                        <goal>commit</goal>
                                    </goals>
                                </execution>
                            </executions>
                        </plugin>
                    </plugins>
                </build>
            </profile>
    
            <profile>
                <id>release-prepare</id>
                <build>
                    <plugins>
                        <plugin>
                            <groupId>org.codehaus.gmaven</groupId>
                            <artifactId>gmaven-plugin</artifactId>
                            <executions>
                                <execution>
                                    <id>release-snapshots</id>
                                    <phase>validate</phase>
                                    <goals>
                                        <goal>execute</goal>
                                    </goals>
                                    <configuration>
                                        <source>
                                            if(project.parent != null &amp;&amp; project.parent.groupId == "com.mycompany" &amp;&amp; project.parent.version.endsWith("-SNAPSHOT"))
                                            {
                                                println "releasing " + project.parent
    
                                                String releaseVersion = project.parent.version.substring(0, project.parent.version.indexOf("-"));
    
                                                String nextVersion;
                                                int index = releaseVersion.lastIndexOf(".");
                                                if(index == -1) nextVersion = (releaseVersion.toInteger() + 1) + "-SNAPSHOT";
                                                else
                                                {
                                                    String prefix = releaseVersion.substring(0, index);
                                                    String suffix = releaseVersion.substring(index + 1);
    
                                                    nextVersion = prefix + "." + (suffix.toInteger() + 1) + "-SNAPSHOT";
                                                }
    
                                                ant.exec(failonerror: "true", dir: "${basedir}/../" + project.parent.artifactId, executable: "cmd")
                                                {
                                                    arg(value: "/c")
                                                    arg(value: "mvn")
                                                    arg(value: "validate")
                                                    arg(value: "-Prelease-align")
                                                    arg(value: "-Dversion.release=" + releaseVersion)
                                                    arg(value: "-Dversion.next=" + nextVersion)
                                                }
    
                                                ant.exec(failonerror: "true", dir: "${basedir}/../" + project.parent.artifactId, executable: "cmd")
                                                {
                                                    arg(value: "/c")
                                                    arg(value: "mvn")
                                                    arg(value: "initialize")
                                                    arg(value: "-Prelease-prepare")
                                                    arg(value: "-Dversion.release=" + releaseVersion)
                                                    arg(value: "-Dversion.next=" + nextVersion)
                                                }
    
                                                ant.exec(failonerror: "true", dir: "${basedir}/../" + project.parent.artifactId, executable: "cmd")
                                                {
                                                    arg(value: "/c")
                                                    arg(value: "mvn")
                                                    arg(value: "deploy")
                                                    arg(value: "-Prelease-perform")
                                                    arg(value: "-Dversion.release=" + releaseVersion)
                                                    arg(value: "-Dversion.next=" + nextVersion)
                                                }
                                            }
    
                                            for(d in project.dependencies)
                                            {
                                                if(d.groupId == "com.mycompany" &amp;&amp; d.version.endsWith("-SNAPSHOT"))
                                                {
                                                    println "releasing " + d
    
                                                    String releaseVersion = d.version.substring(0, d.version.indexOf("-"));
    
                                                    String nextVersion;
                                                    int index = releaseVersion.lastIndexOf(".");
                                                    if(index == -1) nextVersion = (releaseVersion.toInteger() + 1) + "-SNAPSHOT";
                                                    else
                                                    {
                                                        String prefix = releaseVersion.substring(0, index);
                                                        String suffix = releaseVersion.substring(index + 1);
    
                                                        nextVersion = prefix + "." + (suffix.toInteger() + 1) + "-SNAPSHOT";
                                                    }
    
                                                    ant.exec(failonerror: "true", dir: "${basedir}/../" + d.artifactId, executable: "cmd")
                                                    {
                                                        arg(value: "/c")
                                                        arg(value: "mvn")
                                                        arg(value: "validate")
                                                        arg(value: "-Prelease-align")
                                                        arg(value: "-Dversion.release=" + releaseVersion)
                                                        arg(value: "-Dversion.next=" + nextVersion)
                                                    }
    
                                                    ant.exec(failonerror: "true", dir: "${basedir}/../" + d.artifactId, executable: "cmd")
                                                    {
                                                        arg(value: "/c")
                                                        arg(value: "mvn")
                                                        arg(value: "initialize")
                                                        arg(value: "-Prelease-prepare")
                                                        arg(value: "-Dversion.release=" + releaseVersion)
                                                        arg(value: "-Dversion.next=" + nextVersion)
                                                    }
    
                                                    ant.exec(failonerror: "true", dir: "${basedir}/../" + d.artifactId, executable: "cmd")
                                                    {
                                                        arg(value: "/c")
                                                        arg(value: "mvn")
                                                        arg(value: "deploy")
                                                        arg(value: "-Prelease-perform")
                                                        arg(value: "-Dversion.release=" + releaseVersion)
                                                        arg(value: "-Dversion.next=" + nextVersion)
                                                    }
                                                }
                                            }
                                        </source>
                                    </configuration>
                                </execution>
                            </executions>
                        </plugin>
                        <plugin>
                            <groupId>org.codehaus.mojo</groupId>
                            <artifactId>versions-maven-plugin</artifactId>
                            <executions>
                                <execution>
                                    <id>final-updates</id>
                                    <phase>initialize</phase>
                                    <goals>
                                        <goal>update-parent</goal>
                                        <goal>use-releases</goal>
                                        <goal>set</goal>
                                        <goal>commit</goal>
                                    </goals>
                                    <configuration>
                                        <newVersion>${version.release}</newVersion>
                                    </configuration>
                                </execution>
                            </executions>
                        </plugin>
                    </plugins>
                </build>
            </profile>
    
            <profile>
                <id>release-perform</id>
                <build>
                    <plugins>
                        <plugin>
                            <groupId>org.apache.maven.plugins</groupId>
                            <artifactId>maven-deploy-plugin</artifactId>
                        </plugin>
    
                        <plugin>
                            <groupId>org.codehaus.mojo</groupId>
                            <artifactId>versions-maven-plugin</artifactId>
                            <executions>
                                <execution>
                                    <id>set-next-snapshot</id>
                                    <phase>deploy</phase>
                                    <goals>
                                        <goal>set</goal>
                                        <goal>commit</goal>
                                    </goals>
                                    <configuration>
                                        <newVersion>${version.next}</newVersion>
                                    </configuration>
                                </execution>
                            </executions>
                        </plugin>
                        <plugin>
                            <groupId>org.apache.maven.plugins</groupId>
                            <artifactId>maven-scm-plugin</artifactId>
                            <executions>
                                <execution>
                                    <id>checkin-release</id>
                                    <phase>verify</phase>
                                    <goals>
                                        <goal>checkin</goal>
                                        <goal>tag</goal>
                                    </goals>
                                </execution>
                                <execution>
                                    <id>checkin-snapshot</id>
                                    <phase>deploy</phase>
                                    <goals>
                                        <goal>checkin</goal>
                                    </goals>
                                </execution>
                            </executions>
                            <configuration>
                                <tag>${project.version}</tag>
                                <message>auto-generated by release process</message>
                            </configuration>
                        </plugin>
                    </plugins>
                </build>
            </profile>
        </profiles>
    </project>
    
    <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/xsd/maven-4.0.0.xsd">
        <parent>
            <groupId>com.mycompany</groupId>
            <artifactId>test-release-parent</artifactId>
            <version>1-SNAPSHOT</version>
        </parent>
    
        <modelVersion>4.0.0</modelVersion>
        <artifactId>test-release-project</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <packaging>jar</packaging>
    
        <name>${project.artifactId}</name>
        <description>maven project to test a release process</description>
        <inceptionYear>2014</inceptionYear>
        <url>${web.projects}/${project.artifactId}</url>
    
        <repositories>
            <repository>
                <id>www2.mycompany.com</id>
                <name>mycompany Maven Repository</name>
                <url>http://www2.mycompany.com/maven-repo</url>
            </repository>
        </repositories>
    
        <scm>
            <developerConnection>scm:svn:${svn.repository}/${project.artifactId}/trunk</developerConnection>
            <url>${svn.repository}/${project.artifactId}/trunk</url>
        </scm>
    
        <dependencies>
            <dependency>
                <groupId>commons-io</groupId>
                <artifactId>commons-io</artifactId>
            </dependency>
            <dependency>
                <groupId>commons-codec</groupId>
                <artifactId>commons-codec</artifactId>
            </dependency>
    
            <dependency>
                <groupId>com.mycompany</groupId>
                <artifactId>test-release-dependency</artifactId>
                <version>1.0.0-SNAPSHOT</version>
            </dependency>
        </dependencies>
    </project>
    
    mvn <some-phase> -Pbuildall
    
    mvn groovy:execute