How to download sources for a jar with Maven?

How to download sources for a jar with Maven?

When working with Maven-managed projects, it’s often useful to have access to the source code for your dependencies. This can be helpful for debugging, understanding library internals, or contributing back to open-source projects. Fortunately, Maven provides several ways to download and manage the source code for your project’s dependencies.

The maven dependency plugin should be used with the dependency:sources goal:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>3.1.1</version>
    <executions>
      <execution>
        <id>download-sources</id>
        <goals>
          <goal>sources</goal>
        </goals>
        <configuration>
        </configuration>
      </execution>
    </executions>
  </plugin>
Java

This can also be run from the command line as:

mvn dependency:sources -Dsilent=true
Java

Executing mvn dependency:sources will force maven to download all sources of all jars in the project, if the sources are available (are uploaded in the repository where the artifact is hosted). If you want to download javadoc the command is mvn dependency:resolve -Dclassifier=javadoc

Deprecated:It’s also possible to create a profile in your settings.xml file and include the following properties:

<properties>
  <downloadSources>true</downloadSources>
  <downloadJavadocs>true</downloadJavadocs>
</properties>
Java

The key points are:

1. Use the maven-dependency-plugin to download sources on demand
2. Configure the maven-source-plugin to automatically attach sources during the build
3. Use the maven-eclipse-plugin to download sources when generating Eclipse project files

In Eclipse IDE

In Eclipse IDE, it’s better to use the maven eclipse plugin:

$ mvn eclipse:eclipse -DdownloadSources=true

$ mvn eclipse:eclipse -DdownloadSources=true -DdownloadJavadocs=false

Or

pom.xml


    <build>
	<plugins>
	  <plugin>
		<groupId>org.apache.maven.plugins</groupId>
		<artifactId>maven-eclipse-plugin</artifactId>
		<configuration>
			<downloadSources>true</downloadSources>
			<downloadJavadocs>false</downloadJavadocs>
		</configuration>
	  </plugin>
	</plugins>
   </build>
Java

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *