【问题标题】:How to use Maven to only sign three jars and push them to Maven Central?如何使用 Maven 只签署三个 jar 并将它们推送到 Maven Central?
【发布时间】:2014-09-06 10:21:30
【问题描述】:

更新:见followup question


我有一个 Java 库,它的构建过程完全用 Ant 编写。项目的沙箱(源码目录,我在里面编辑代码)是

R:\jeffy\programming\sandbox\xbnjava\

它的构建(输出)目录是

R:\jeffy\programming\build\xbnjava-0.1.1\

这就是我的目标:

  1. ant cleanpublish:从头开始构建项目,包括创建这三个jar

    • mylibrary-0.1.1.jar
    • mylibrary-0.1.1-sources.jar
    • mylibrary-0.1.1-javadoc.jar

    放入

    R:\jeffy\programming\build\xbnjava-0.1.1\download
    

    这部分已经完成并且运行良好。这三个罐子是 Maven Central 的 as required(向上滚动一点点查看)。

  2. 在 GitHub 中检查版本并将构建目录上传到我的 Web 服务器。这一步我也完成了。

  3. mvn deploy唯一需要做的就是签署三个 jar 并将它们推送到 Maven Central。这是我目前存在的祸根。


这些是我到目前为止所采取的步骤:我已经

  1. Set up my project 在 sonatype,
  2. Created my public keygpg4winrelated documentation 在 sonatype 上)
  3. Downloadedinstalled Maven
  4. 设置我希望correct settings.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
       <servers>
          <server>
             <id>sonatype-nexus-snapshots</id>
             <username>MY_SONATYPE_USERNAME</username>
             <password>MY_SONATYPE_PASSWORD</password>
          </server>
          <server>
             <id>sonatype-nexus-staging</id>
             <username>MY_SONATYPE_USERNAME</username>
             <password>MY_SONATYPE_PASSWORD</password>
          </server>
       </servers>
       <pluginGroups></pluginGroups>
       <proxies></proxies>
       <mirrors></mirrors>
       <profiles></profiles>
    </settings>
    

(此文件存储在R:\jeffy\programming\sandbox\settings.xml,因为它被我的每个项目使用。)

  1. 至少设置我的pom.xml 的开头,基于the one in ez-vcard(参考the author's 有帮助的blog post)。我还使用了 Maven 的 introduction to the 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/maven-v4_0_0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.github.xbn</groupId>
   <artifactId>xbnjava</artifactId>
   <packaging>jar</packaging>
   <version>0.1.2-SNAPSHOT</version>
   <name>XBN-Java</name>
   <url>https://github.com/aliteralmind/xbnjava</url>
   <inceptionYear>2014</inceptionYear>
   <organization>
      <name>Jeff Epstein</name>
   </organization>
   <description>XBN-Java is a collection of generically-useful backend (server side, non-GUI) programming utilities, featuring RegexReplacer and FilteredLineIterator. XBN-Java is the foundation of Codelet (http://codelet.aliteralmind.com).</description>

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

   <licenses>
      <license>
         <name>Lesser General Public License (LGPL) version 3.0</name>
         <url>https://www.gnu.org/licenses/lgpl-3.0.txt</url>
      </license>
      <license>
         <name>Apache Software License (ASL) version 2.0</name>
         <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
      </license>
   </licenses>

   <developers>
      <developer>
         <name>Jeff Epstein</name>
         <email>aliteralmind-github@yahoo.com</email>
         <roles>
            <role>Lead Developer</role>
         </roles>
      </developer>
   </developers>

   <issueManagement>
      <system>GitHub Issue Tracker</system>
      <url>https://github.com/aliteralmind/xbnjava/issues</url>
   </issueManagement>

   <scm>
      <connection>scm:git:git@github.com:aliteralmind/xbnjava.git</connection>
      <url>scm:git:git@github.com:aliteralmind/xbnjava.git</url>
      <developerConnection>scm:git:git@github.com:aliteralmind/xbnjava.git</developerConnection>
   </scm>

   <properties>
      <java.version>1.7</java.version>
   </properties>

其余的,我不太确定:

  <profiles>
     <!--
     This profile will sign the JAR file, sources file, and javadocs file using the GPG key on the local machine.
     See: https://docs.sonatype.org/display/Repository/How+To+Generate+PGP+Signatures+With+Maven
     -->
     <profile>
        <id>release-sign-artifacts</id>
        <activation>
           <property>
              <name>release</name>
              <value>true</value>
           </property>
        </activation>
        <build>
           <plugins>
              <plugin>
                 <groupId>org.apache.maven.plugins</groupId>
                 <artifactId>maven-gpg-plugin</artifactId>
                 <version>1.4</version>
                 <executions>
                    <execution>
                       <id>sign-artifacts</id>
                       <phase>package</phase>
                       <goals>
                          <goal>sign</goal>
                       </goals>
                    </execution>
                 </executions>
              </plugin>
           </plugins>
        </build>
     </profile>
  </profiles>
</project>

我需要做什么才能完成POM,它的路径是

R:\jeffy\programming\sandbox\xbnjava\pom.xml

所以它签名并推送这三个 jar 文件,位于

R:\jeffy\programming\build\xbnjava-0.1.1\download\

到 Maven 中心?

我真的很感激一些关于从这里去哪里的建议。我已经在 Maven 文档中游泳了三天,我迷失了和沮丧。这是我在过去几年中使用 Maven 的第三次尝试,但每次都非常糟糕。

(我最初尝试fulfill Maven requirements with Ant tasks,但我决定将我的 Ant 构建和 Maven 签名的罐子和推送到 Maven-Central 部分完全分开。这是为了学习新的东西,但是还因为在 Ant 中实现 Maven 似乎比纯粹的 Maven 更不标准。)

谢谢你帮助我。

【问题讨论】:

    标签: java maven ant


    【解决方案1】:

    如果您在 Maven 之外构建工件,则需要在 .pom 文件中引用它们

    首先,将项目的packaging 设置为pom,这样Maven 就不会尝试编译任何东西。

    然后使用build-helper-maven-plugin 将您的工件文件附加到项目中。这是一个例子:

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>1.8</version>
            <executions>
                <execution>
                    <id>attach-artifacts</id>
                    <phase>package</phase>
                    <goals>
                        <goal>attach-artifact</goal>
                    </goals>
                    <configuration>
                        <artifacts>
                            <artifact>
                                <file>build/xbnjava-0.1.1/download/mylibrary-0.1.1.jar</file>
                                <type>jar</type>
                            </artifact>
                            <artifact>
                                <file>build/xbnjava-0.1.1/download/mylibrary-0.1.1-javadoc.jar</file>
                                <type>jar</type>
                                <classifier>javadoc</classifier>
                            </artifact>
                            <artifact>
                                <file>build/xbnjava-0.1.1/download/mylibrary-0.1.1-sources.jar</file>
                                <type>jar</type>
                                <classifier>sources</classifier>
                            </artifact>
                        </artifacts>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    

    最后,将distributionManagement 部分添加到您的.pom 文件中,指定为here

    您可以在github 参考我自己的 Maven 项目设置,该设置是为了在 Maven 中心上传手动构建的 .jar 文件而创建的。

    【讨论】:

    • 这看起来很有希望。我唯一不明白的是它如何知道 jars 存在于哪个目录中。
    • 它只是输出目录的路径吗?我已经更新了我的答案。
    • 好的,我明白了。 POM 位于C:\jeffy\programming\sandbox\xbnjava\ ,即项目根目录。它怎么知道build/xbnjava-0.1.1/download/mylibrary-0.1.1-sources.jar 位于C:\jeffy\programming?通过预先挂起../?
    • 我的答案中的路径可能是错误的,因为这取决于 pom.xml 文件的位置。我假设 pom.xml 在 xbnjava 目录中,在构建目录旁边。您应该将路径相对于 pom.xml
    • 感谢您的帮助@AlexeyGavrilov。你的帮助让我走得更远,所以谢谢你。我将发布另一个问题,因为我不想用后续跟进和诊断信息(和咆哮)淹没您。它只是没有发生。
    猜你喜欢
    • 2021-06-08
    • 1970-01-01
    • 2018-06-28
    • 2017-06-08
    • 2016-07-03
    • 1970-01-01
    • 2019-07-05
    • 1970-01-01
    相关资源
    最近更新 更多