104

I want to deploy sources and javadocs with my snapshots. This means that I want to automize the following command:

mvn clean source:jar javadoc:jar deploy

Just to execute:

mvn clean deploy

I don't want to have javadoc/sources generation executed during the install phase (i.e. local builds).

I know that source/javadoc plugins can be synchronized with the execution of the release plugin but I can't figure out how to wire it to the snapshots releases.

Henryk Konsek
  • 9,016
  • 5
  • 32
  • 41

3 Answers3

88
<build>
  <plugins> 
    <plugin>
      <artifactId>maven-source-plugin</artifactId>
      <executions>
        <execution>
          <id>attach-sources</id>
          <phase>deploy</phase>
          <goals><goal>jar-no-fork</goal></goals> 
        </execution>
      </executions>
    </plugin>
    <plugin> 
      <artifactId>maven-javadoc-plugin</artifactId> 
      <executions> 
        <execution> 
          <id>attach-javadocs</id>
          <phase>deploy</phase>
          <goals><goal>jar</goal></goals> 
        </execution> 
      </executions> 
    </plugin>
    <plugin> 
      <!-- explicitly define maven-deploy-plugin after other to force exec order -->
      <artifactId>maven-deploy-plugin</artifactId> 
      <executions> 
        <execution> 
          <id>deploy</id>
          <phase>deploy</phase>
          <goals><goal>deploy</goal></goals> 
        </execution> 
      </executions> 
    </plugin>
  </plugins> 
</build>

See Sonatype's OSS parent POM for a complete example.

simon04
  • 3,054
  • 29
  • 25
sfussenegger
  • 35,575
  • 15
  • 95
  • 119
  • 2
    I'm using this setup, and it works quite well. However I had two small issues: one, generated sources are not included in the "jar" goal, you'll need "jar-no-fork". Two, there is a bug in the release plugin that will cause to generate the release sources twice (and therefore deploeyed twice, which will lead to problems with repository managers) – mglauche Jan 18 '11 at 15:18
  • 1
    `maven-source-plugin:jar` attaches to the `package` phase by default, so you could leave off `verify` and accomplish the same thing. Besides I'm not sure why you'd attach this to verify anyway as that phase is intended for "package the project and run integration tests". – matt b Jan 18 '11 at 15:22
  • @mglauche @matt thanks for your comments. I've just made the appropriate changes. – sfussenegger Jan 18 '11 at 15:26
  • Sorry guys, but maven-deploy-plugin (as opposed to the release-plugin) doesn't support element in . So the configuration in your answer doesn't work for deploy (only for release). – Henryk Konsek Jan 19 '11 at 11:29
  • @Henryk I've proposed a completely different approach now. Simply try to bind both plugins to the deploy phase, no profile needed. Untested but *should* work. The problem is that Maven doesn't guarantee any order of plugin execution. Therefore, the deploy plugin could execute before sources and javadoc jars have been built. – sfussenegger Jan 19 '11 at 12:39
  • 1
    @Henryk Ok, I've explicitly added maven-deploy-plugin as well as maven seems (or tries) to guarantee execution in the oder the plugins are defined in the POM. It might require some experimenting though. – sfussenegger Jan 19 '11 at 12:50
  • It works! @sfussenegger Kudos for you :) I tried to bind doc/src to deploy phase before but without your phase sequence trick it didn't work. Thanks again. – Henryk Konsek Jan 20 '11 at 09:48
  • you have wrong execution id of maven-deploy-plugin it should be default-deploy – Hurda Mar 22 '13 at 10:04
  • @Hurda "default-" execution IDs are a feature introduced in Maven 2.2.0. I haven't used it yet, but it's certainly not necessary. Using some random unique ID works just fine. – sfussenegger Mar 22 '13 at 10:49
  • here's the related documentation: http://maven.apache.org/guides/mini/guide-default-execution-ids.html – sfussenegger Mar 22 '13 at 10:50
  • 1
    using random id creates new execution I thought you would want to "remap" the default one execution – Hurda Mar 22 '13 at 12:25
  • with newer maven version 3.0.5+, you just need to add the plugins without specifying a phase. – Zarathustra Jul 28 '14 at 12:59
  • 3
    This isn't working for me with Maven 3.1.1. Deploy plugin happens before javadoc and source despite the order they appear in the pom. Anyone else running into this? – Chris H. Aug 01 '14 at 17:48
67

The article referred to by Dan also mentions another approach that works without modifying poms AND won't go away anytime soon:

mvn clean javadoc:jar source:jar install

Which works fine with Maven 3+, along with...

mvn clean javadoc:jar source:jar deploy

Which I have tested from Jenkins deploying to Nexus.

This approach was nice because I only had to modify some Jenkins jobs and didn't need to mess with my poms.

Dave
  • 21,524
  • 28
  • 141
  • 221
  • 5
    Thanks for a pom-free solution! Note that **javadoc:jar** and/or **source:jar** must appear before **install** or **deploy**, or the extra jars won't be "attached" to the deployment. – seanf Jul 08 '16 at 08:03
  • 2
    FWIW these options also work with `package`: `mvn clean javadoc:jar source:jar package` – ecoe Jan 28 '18 at 20:33
  • This is the good answer, since it tells you the exact command to build and upload sources and javadoc to your repo. – joninx May 05 '20 at 07:43
50

Just to add an alternative that doesn't require you to muck with plugin configuration:

mvn -DperformRelease=true [goals]

Credit goes to mcbeelen from http://sea36.blogspot.com/2009/02/attaching-javadocs-and-sources-to-maven.html?showComment=1314177874102#c6853460758692768998

Dan
  • 1,955
  • 17
  • 21
  • 5
    I'd like to mention that this feature might go away in a future release of Maven (possibly Maven-4?). Check out the comment here in the Maven Super POM's profile section: http://maven.apache.org/ref/3.1.1/maven-model-builder/super-pom.html – Dan Nov 14 '13 at 02:42