105

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.

3 Answers 3

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

13
  • 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, 2011 at 15:18
  • 1
    maven-source-plugin:jar attaches to the package phase by default, so you could leave off <phase>verify</phase> 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, 2011 at 15:22
  • 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. Jan 19, 2011 at 12:50
  • 1
    using random id creates new execution I thought you would want to "remap" the default one execution
    – Hurda
    Mar 22, 2013 at 12:25
  • 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 1, 2014 at 17:48
68

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.

3
  • 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 8, 2016 at 8:03
  • 2
    FWIW these options also work with package: mvn clean javadoc:jar source:jar package
    – ecoe
    Jan 28, 2018 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 5, 2020 at 7: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

1

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.