maven publishing sources and javadocs

This is a really overly simplistic guide to show how to add packaging up the sources and javadoc jars to the mvn package command.

Starting with with some sort of maven project, if you don’t have one off hand, you can use the “Hello, world” project maven generates with:

mvn archetype:generate \
  -DgroupId=com.mycompany.app -DartifactId=my-app \
  -DarchetypeArtifactId=maven-archetype-quickstart \
  -DinteractiveMode=false

Then cd into my-app or whereever your project is and open up the pom.xml. There are only two plugins we need to add. It might be easier to look at a diff of what I added to the default pom.xml generated by mvn.

--- pom.xml.original    2015-03-12 11:21:04.502509345 -0700
+++ pom.xml.new 2015-03-12 11:20:44.699176122 -0700
@@ -15,4 +15,32 @@
       <scope>test</scope>
     </dependency>
   </dependencies>
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-source-plugin</artifactId>
+        <executions>
+          <execution>
+            <id>attach-sources</id>
+            <goals>
+              <goal>jar</goal>
+            </goals>
+          </execution>
+        </executions>
+      </plugin>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-javadoc-plugin</artifactId>
+        <executions>
+          <execution>
+            <id>attach-javadocs</id>
+            <goals>
+              <goal>jar</goal>
+            </goals>
+          </execution>
+        </executions>
+      </plugin>
+    </plugins>
+  </build>
 </project>

Now when you run mvn package, three artifacts get created.

target/my-app-1.0-SNAPSHOT.jar
target/my-app-1.0-SNAPSHOT-javadoc.jar
target/my-app-1.0-SNAPSHOT-sources.jar

All three can now be published to the maven repository (artifactory).