Using the Maven Source Plugin

When using an ordinary jar library, IDEs like Eclipse’s JDE don’t have enough information to display inline javadocs for referenced classes. You have to manually direct Eclipse to the library’s source code to take advantage of inline documentation. Maven’s source plugin provides a convenient solution to this problem. When building the library, the plugin creates a source jar containing the package’s source code. This source package can be installed or deployed into your repository. Maven’s eclipse plugin then adds the source package to the generated .classpath file.

This is how it works. Before installing or deploying your library, run the source plugin:

  mvn source:jar

Your next install or deploy call will take care of the rest. In the dependent project (the one using your library), run the eclipse plugin:

  mvn eclipse:eclipse

After refreshing the project in eclipse, you should now have access to inline documentation.

If you don’t want to run source:jar each time, you can attach the call to the build lifecycle’s package phase:

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-source-plugin</artifactId>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>jar</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

After running mvn package, there should be a source package in your target directory.

Advertisements
This entry was posted in java and tagged , , . Bookmark the permalink.

9 Responses to Using the Maven Source Plugin

  1. Oleksandr says:

    BTW, you can ommit default groupId for plugins: org.apache.maven.plugins

  2. Santhosh says:

    Thank you. Had been looking for this :) Works like a charm.

  3. Zaid Fattuhi says:

    Hi,

    This will generate sources in separate jar. If somebody wantsto attach the source code with the original jar (not in a separate jar with -source classifier) then add the following to pom.xml

    ..

    src/main/java

    src/main/resources

    Regards,
    Zaid

  4. Michal Chmielarz says:

    Hi,

    Zaid – could you explain a bit more what and where in pom.xml file should I put to pack sources in original jar?

  5. Angelo Neuschitzer says:

    To build a single .jar with sources do not use the maven-source-plugin. Use the following entry in the pom.xml:

    ${basedir}/src/main/resources

    ${basedir}/src/main/java

    (If you want to use that for gwt / gxt you also have to construct a .gwt.xml in your src/main/resources… that you have to include in the target .gwt.xml)

    Good Luck

    Angelo

  6. Angelo Neuschitzer says:

    Ah, I see, the tags vanish here.
    I posted the code here:
    http://pastebin.com/f3164f918

    Hope it helps.

    Angelo

  7. Jitesh says:

    Hi Angelo,

    So I went with the approach that you mentioned, what is happening is, the source is getting attached but then I dont see the package in the eclipse package explorer(of the project that is using the library).

    Thanks,
    Jitesh

  8. mafr says:

    Jitesh: Please try a “mvn eclipse:clean eclipse:eclipse” to generate new .classpath files (they reference the source packages). Then refresh your Eclipse project.

  9. Mukesh Harshwal says:

    I want to develop a maven plugin having two goals.Please help be for this and if possible then send me sample code.
    Thanks in advance.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s