java - 自动部署到sonatype的oss maven仓库

标签 java maven maven-plugin nexus sonatype

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

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

那么,我的 pom 中需要出现什么简介(假设是一个标准的简单 java 项目),包括 sonatype 存储库的 url,我找到的所有文档似乎都是坚持 localhost:8081 是它,并且需要 maven 咒语让它做一个发布(最好通过 mvn release plugin),让它签署 Artifact ,并拥有它将生成的 Artifact 部署到 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 parent pom并将其扁平化为我的pom文件。

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

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

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 巫术使 mvn release:perform 也执行暂存版本,将不胜感激。

最佳答案

为了方便 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!"

待测试...

关于java - 自动部署到sonatype的oss maven仓库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21902873/

相关文章:

linux - 在 Windows 上开发并构建 - 使用 Vagrant 在 Linux 上部署?

java - 如何在Java中解析JSON对象:

java - java中new对象的{}是什么?

java - 如何打印不带括号([)和逗号(,)的arraylist值以及java中新行中的每个值?

java - 如何让maven项目从自己的目录而不是工作目录中识别文件?

java - 无法执行 HTTP 请求

java - 从插件设置 Maven 属性

java - 为什么使用 cxf-xjc-plugin 从 xsd 生成代码 (java) 在 jdk 11 (amazon) 上不起作用,但在 java 8 上却起作用

Maven原型(prototype)问题

java - 在 Java 中查找 char 数组的大小