Explain Codes LogoExplain Codes Logo

Maven – Always download sources and javadocs

java
maven-plugin
dependency-management
java-documentation
Anton ShumikhinbyAnton Shumikhin·Nov 22, 2024
TLDR

To enable automatic downloads of sources and JavaDocs in Maven, modify your settings.xml by adding this profile:

<profiles> <profile> <id>enhanced-dependency-resolution</id> <activation><activeByDefault>true</activeByDefault></activation> <properties> <maven.dependency.sources>true</maven.dependency.sources> <maven.dependency.javadocs>true</maven.dependency.javadocs> </properties> </profile> </profiles>

This code snippet prompts Maven to always fetch sources and JavaDocs, enhancing your local development environment by providing better insight and accessibility to your dependencies' documentation.

Using Maven Dependency Plugin

When central settings.xml usage is not preferred, or you aim for a project-specific setting, you can leverage the maven-dependency-plugin. Specify a separate Maven profile within your pom.xml to delegate the sources and JavaDocs downloading task without impacting the overall build time.

<profiles> <profile> <id>download-sources</id> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <goals> <goal>sources</goal> <goal>resolve</goal> </goals> <configuration> <classifier>javadoc</classifier> </configuration> </execution> </executions> </plugin> </plugins> </build> </profile> </profiles>

Triggering this profile in your IDE or from the Maven command line will ensure that Javadocs and sources are downloaded upon request.

Sync JVM Docs with IDE

Integrate your IDE to automatically download JavaDoc/sources when working on a project. IDEs like Eclipse and IntelliJ IDEA offer functionalities where the Maven dependencies synchronize with the corresponding sources and JavaDocs automatically during project import or update.

Freed up the Disk Space

Considering that fetching all sources and JavaDocs can inflate disk usage, it's crucial to configure maven-dependency-plugin with care. This can be achieved by:

  • Downloading sources/javadocs for significant dependencies only
  • Persistently deleting older sources from your local repository
  • Utilize Maven dependency analysis tools to weed out unused artifacts

Maven Command Shortcuts

For those who prefer the command line, here's a list of streamlined commands:

mvn clean install -Pdownload-sources // Clean the house, install new stuff, fetch some reading material mvn dependency:sources dependency:resolve -Dclassifier=javadoc // Get all the gossips (JavaDocs) and the juicy stories (sources) mvn -Dmaven.test.skip=true // "Tests? In this economy? Just build already!"

These commands ensure a blend of automation and adaptability, allowing you to tailor the Maven lifecycle according to your project needs.

Scaling configurations with Organizational Policies

For organizations or large projects, consider embedding these configurations within an organization-level pom.xml. It stimulates a standardized way of retrieving source and JavaDoc, freeing individual developers from configuring their environments.