【问题标题】:automate deployment to sonatype's oss maven repository自动部署到 sonatype 的 oss maven 存储库
【发布时间】:2014-03-21 02:31:00
【问题描述】:

我有几个 github java 项目。其中一个我已经手动部署到 sonatype 的存储库,以便它在 maven Central 中发布。

从某种意义上说,这是一个有点痛苦的过程,因为它似乎涉及到太多需要跳过的圈子和大量的手动工作,我希望将其自动化。 所以我实际上停止了这样做,因为它的工作量太大了。有大量文档表明这是可能的,并且相当多的文档表明它以某种方式涉及使用 nexus-staging-maven-plugin 做某事。 不幸的是,所有这些文档都(以典型的 maven 风格)跳过了基本细节,这些细节使我能够以一种直接的方式找出允许我自动将发布版本发布到 sonatype 存储库所需的最少步骤(即没有我手动批准事物)。

那么,我的 pom 中需要存在的简介是什么(假设是一个其他沼泽标准简单的 Java 项目),包括 sonatype 存储库的 url,我发现的所有文档似乎坚持 localhost:8081 是它,以及使其发布所需的 Maven 咒语(最好通过 mvn 发布插件),让它签署工件,并拥有它将生成的工件部署到 sonatype、批准并准备好同步到 maven Central 等。

所以,我有点想在 ruby​​ 世界中寻找“gem push”的 Maven 替代品,它可以在一个方便的班轮中完成工作。这是一个简单的案例,给定一个我批准的 jar 文件,我如何让它以最少的麻烦最终出现在 maven Central 中。

我非常感谢一些已经设置好的 pom 文件示例,我可以复制和调整。

编辑:

这是我的工作 pom 文件:

<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.jillesvangurp</groupId>
    <artifactId>jsonj</artifactId>
    <version>1.34-SNAPSHOT</version>

    <name>JsonJ</name>
    <description>A framework for working with json in Java the "proper" way. No mappings or model classes, it's all just lovely json, but in Java.</description>
    <url>https://github.com/jillesvangurp/jsonj</url>

    <licenses>
        <license>
            <name>MIT license</name>
            <url>https://github.com/jillesvangurp/jsonj/blob/master/LICENSE</url>
            <distribution>repo</distribution>
        </license>
    </licenses>

    <scm>
        <url>git://git@github.com:jillesvangurp/jsonj.git</url>
        <connection>scm:git:git@github.com:jillesvangurp/jsonj.git</connection>
        <developerConnection>scm:git:git@github.com:jillesvangurp/jsonj.git</developerConnection>
    </scm>

    <repositories>
        <repository>
            <id>sonatype-nexus-snapshots</id>
            <name>Sonatype Nexus Snapshots</name>
            <url>https://oss.sonatype.org/content/repositories/snapshots</url>
            <releases>
                <enabled>false</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>

    <distributionManagement>
        <snapshotRepository>
            <id>sonatype-nexus-snapshots</id>
            <name>Sonatype Nexus Snapshots</name>
            <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
        </snapshotRepository>
        <repository>
            <id>sonatype-nexus-staging</id>
            <name>Nexus Release Repository</name>
            <url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
        </repository>
    </distributionManagement>

    <developers>
        <developer>
            <id>jillesvangurp</id>
            <name>Jilles van Gurp</name>
            <url>http://www.jillesvangurp.com</url>
            <timezone>gmt+1</timezone>
            <roles>
                <role>Main Developer</role>
            </roles>
        </developer>
    </developers>

    <organization>
        <name>www.jillesvangurp.com</name>
        <url>http://jillesvangurp.com</url>
    </organization>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <configuration>
                    <verbose>true</verbose>
                    <fork>true</fork>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>2.8.1</version>
                <executions>
                    <execution>
                        <id>documentation</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>2.2</version>
                <executions>
                    <execution>
                        <id>gathersource</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.sonatype.plugins</groupId>
                <artifactId>nexus-staging-maven-plugin</artifactId>
                <version>1.6</version>
                <extensions>true</extensions>                
                <configuration>
                    <!-- The Base URL of Nexus instance where we want to stage -->
                    <nexusUrl>https://oss.sonatype.org/</nexusUrl>
                    <serverId>sonatype-nexus-staging</serverId>
                </configuration>
            </plugin>            
        </plugins>
        <extensions>
            <extension>
            <artifactId>wagon-webdav-jackrabbit</artifactId>
            <groupId>org.apache.maven.wagon</groupId>
            <version>2.2</version>
            </extension>
        </extensions>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-release-plugin</artifactId>
                    <version>2.1</version>
                    <configuration>
                        <mavenExecutorId>forked-path</mavenExecutorId>
                        <useReleaseProfile>false</useReleaseProfile>
                        <arguments>-Psonatype-oss-release</arguments>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>        
    </build>
    <profiles>
        <profile>
            <id>sonatype-oss-release</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-source-plugin</artifactId>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-javadoc-plugin</artifactId>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-gpg-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>sign-artifacts</id>
                                <phase>verify</phase>
                                <goals>
                                    <goal>sign</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>        
    </profiles>

    <dependencies>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.8.7</version>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>junit</groupId>
                    <artifactId>junit</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-all</artifactId>
            <version>1.3</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.googlecode.json-simple</groupId>
            <artifactId>json-simple</artifactId>
            <version>1.1.1</version>
            <exclusions>
                <exclusion>
                    <artifactId>junit</artifactId>
                    <groupId>junit</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>xom</groupId>
            <artifactId>xom</artifactId>
            <version>1.2.5</version>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
        </dependency>
        <dependency>
            <groupId>com.jillesvangurp</groupId>
            <artifactId>efficientstring</artifactId>
            <version>1.11</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.2.3</version>
        </dependency>
    </dependencies>
</project>

下面的评论 (@aurelien-thieriot) 让我走上了正轨,但它本身还不够。

最后我把 sonatype 父 pom 压扁成 my pom 文件。

这让我可以正常使用 mvn 发布插件。它将工件上传到 sonatype 临时存储库。然后要发布工件,我实际上需要暂存存储库 ID。 您可以从 https://oss.sonatype.org/index.html#stagingRepositories 的存储库视图中找到它。

在我的例子中,命令行变成了:

mvn nexus-staging:release -Ddescription="Release 1.33" -DstagingRepositoryId=comjillesvangurp-1002

如果没有正确的 id,它就无法弄清楚并且仍然失败:Sonatype Maven Staging Plugin Issue

所以 95% 是自动化的,但我仍然需要每次都弄清楚 stagingRepositoryId

编辑:

mvn release:perform 实际上会告诉您暂存存储库的 ID。 我想您可以编写一个脚本,从输出中提取此 id,然后将其传递给下一步。如果有人知道一些 mvn voodoo 让 mvn release:perform 也进行临时发布,那将不胜感激。

【问题讨论】:

    标签: java maven maven-plugin nexus sonatype


    【解决方案1】:

    为了方便 Maven 项目,Sonatype 提供了一个父 POM,您可以将所有基本配置添加到您的项目中:

    https://docs.sonatype.org/display/Repository/Sonatype+OSS+Maven+Repository+Usage+Guide#SonatypeOSSMavenRepositoryUsageGuide-Changesto%7B%7Bpom.xml%7D%7D

    重要的一点是:

      <parent>
        <groupId>org.sonatype.oss</groupId>
        <artifactId>oss-parent</artifactId>
        <version>7</version>
      </parent>
    

    以及源代码仓库详情:

      <scm>
        <connection>scm:svn:http://foo.googlecode.com/svn/trunk/</connection>
        <developerConnection>scm:svn:https://foo.googlecode.com/svn/trunk/</developerConnection>
        <url>http://foo.googlecode.com/svn/trunk/</url>
      </scm>
    

    您还需要在您的计算机上安装 GPG(需要对包进行签名)并且我们的 settings.xml 正确填写了您的凭据:

      <servers>
        <server>
          <id>sonatype-nexus-snapshots</id>
          <username>your-jira-id</username>
          <password>your-jira-pwd</password>
        </server>
        <server>
          <id>sonatype-nexus-staging</id>
          <username>your-jira-id</username>
          <password>your-jira-pwd</password>
        </server>
      </servers>
    

    之后,你应该可以使用两步发布了:

    $ mvn release:prepare
    
    $ mvn release:perform
    

    不幸的是,我不知道有任何方法可以自动化流程的手动审批部分(在 oss.sonatype.org 中)。但这应该已经为您节省了一些时间。

    如上所示,文档可能有点复杂,但非常完整,为您提供了各种场景所需的所有知识。

    编辑:

    事实上,我认为我错了,其中有一部分是关于自动审批流程。很有趣。

    对于这部分,你是对的,细节非常有限。不过,我希望配置的第一部分已经对您有所帮助。我需要进一步研究这个暂存的东西(或者也许其他人已经做过了!)

    再次编辑:

    我需要实际尝试一下,但听起来如下:

            <plugins>
                <plugin>
                    <groupId>org.sonatype.plugins</groupId>
                    <artifactId>nexus-staging-maven-plugin</artifactId>
                    <version>1.6</version>
                    <extensions>true</extensions>
                    <configuration>
                        <!-- The Base URL of Nexus instance where we want to stage -->
                        <nexusUrl>https://oss.sonatype.org/service/local/staging/deploy/maven2/</nexusUrl>
                        <serverId>sonatype-nexus-staging</serverId>
                    </configuration>
                </plugin>
            </plugins>
    

    根据文档,部署应替换为正确的暂存工作流程(包括关闭),并且会留下最新的步骤:

    $ mvn nexus-staging:release -Ddescription="Yippie!"
    

    待测试...

    【讨论】:

    • 哇。所以有用吗?如果你能告诉我们最后一部分是否有效,我会很高兴:)
    • 这样我们就可以对 Sonatype 人抱怨一下,看看他们/某人是否可以将 staging-maven-plugin 添加到官方父 pom 中
    • 我当前的暂存设置存在一些问题;等待他们的批准。一旦我让它工作(或不工作),我会报告。计划在这里添加所有信息,因为我预计很多人可能正在寻找一种明确的方式来做到这一点。
    【解决方案2】:

    所以 95% 是自动化的,但我仍然需要每次都弄清楚 stagingRepositoryId。

    您可以使用mvn nexus-staging:rc-list

    具体来说,通过执行mvn release:rc-list 并使用grep 或其他任何方式通过某种形式的组ID 或您知道stagingRepositoryId 的其他子字符串过滤输出,您可以确定完整的@987654325 @值

    例如,我的项目的组 ID 是 nu.validator,而我的 stagingRepositoryId 值都采用 nuvalidator-NNNN 的形式,其中 NNNN 部分是从 1000 开始的数字,我的第一个版本和每次我释放时系统都会增加 1;所以nuvalidator-1000nuvalidator-1001等等。

    所以在我用于构建的 python 脚本中,我只是这样做:

    output = subprocess.check_output("mvn nexus-staging:rc-list  -DnexusUrl=https://oss.sonatype.org/ -DserverId=ossrh")
    for line in output.split('\n'):
        if "nuvalidator" in line:
            stagingRepositoryId = "nuvalidator-" + line[8:23]
            ...
    

    这是因为mvn nexus-staging:rc-list 输出中返回的相关行格式为:

    ...
    [INFO] central_bundles-3514 OPEN     Implicitly created (auto staging).
    [INFO] central_bundles-3515 OPEN     Implicitly created (auto staging).
    [INFO] central_bundles-3521 OPEN     Implicitly created (auto staging).
    [INFO] nuvalidator-1008     OPEN     Implicitly created (auto staging).
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-19
      • 2019-02-08
      • 2016-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-18
      相关资源
      最近更新 更多