How to use Maven to only sign three jars and push

2019-03-02 03:15发布

问题:

UPDATE: See followup question


I have a Java library whose build process is entirely written in Ant. The project's sandbox (the source directory, in which I edit the code) is

R:\jeffy\programming\sandbox\xbnjava\

Its build (output) directory is

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

This is what my goal is:

  1. ant cleanpublish: Builds the project from scratch, including creating these three jars

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

    which are placed into

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

    This part is done and works well. These three jars are as required by Maven Central (scroll up a tiny bit to see it).

  2. Check the version into GitHub and upload the build directory to my web server. I have also completed this step.

  3. mvn deploy: The only thing this needs to do is sign the three jars and push them to Maven Central. This is the current bane of my existence.


These are the steps I've taken so far: I have

  1. Set up my project at sonatype,
  2. Created my public key with gpg4win (related documentation on sonatype)
  3. Downloaded and installed Maven
  4. Set up what I hope is a 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>
    

(This file is stored in R:\jeffy\programming\sandbox\settings.xml, as it's used by every one of my projects.)

  1. Set up at least the beginning of my pom.xml, based on the one in ez-vcard (as referenced in the author's helpful blog post). I also used Maven's introduction to the POM as a guide.

These parts, at the top, I believe I understand:

<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>

The rest of it, I'm not so sure:

  <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>

What do I need to do to complete the POM, whose path is

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

so it signs and pushes these three jar files, as located in

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

to Maven Central?

I would really appreciate some advice on where to go from here. I have been swimming in nothing but Maven documentation for three days now, and I'm lost and frustrated. This is my third attempt over the past few years with Maven, and each time it's gone very badly.

(I originally attempted to fulfill Maven requirements with Ant tasks, but I decided to instead completely separate my Ant build and the Maven sign-the-jars-and-push-to-Maven-Central portion. This is to learn something new, but also because it seems like implementing Maven within Ant is less of a standard than is just pure Maven.)

Thank you for helping me.

回答1:

If you are building your artifacts outside of Maven you need to reference them in your .pom file

At first, set the packaging of your project to pom so Maven will not attempt to compile anything.

Then use the build-helper-maven-plugin to attach your artifact files to the project. Here is an example:

    <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>

Finally, add the distributionManagement section to your .pom file as specified here.

You can refer to my own Maven project setup at github which was created to upload manually built .jar files on Maven central.



标签: java maven ant