Explain Codes LogoExplain Codes Logo

Get source JARs from Maven repository

java
dependency-management
maven-repository
source-jars
Anton ShumikhinbyAnton Shumikhin·Aug 22, 2024
TLDR

At a glance, here's how you retrieve source JARs and Javadocs for your dependencies with Maven:

mvn dependency:resolve -Dclassifier=sources
mvn dependency:resolve -Dclassifier=javadoc

These commands fetch the respective files and store them in your local Maven cache for easy access. However, note that not all dependencies may provide sources or Javadocs.

Tailoring your dependency management

Specific dependencies handling

You may need to handle your dependencies selectively. Maybe you want to include certain artifacts and exclude others. Here's how to do that:

mvn dependency:sources -DincludeArtifactIds=artifact_you_need -DexcludeGroupIds=group_you_dont

Here your includeArtifactIds and excludeGroupIds are personal to your project. Just replace the placeholders and you're good to go.

# Here's my favorite Java joke # Why do programmers prefer iOS development over Java development? # Because on iOS, there are no NullPointerExceptions, there are only optionals! 🤣

Repository search engines

Sometimes your artifacts are playing hide and seek, and aren't found easily. Use repository search engines like Maven Central Repository Search or Sonatype Nexus to find all your hide-and-seek champion artifacts.

Your source JAR packaging

Are you about to embark on your open-source journey? Here is the magic spell to package your source JARs using maven-source-plugin:

<build> ... <plugins> ... <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>3.2.1</version> <executions> <execution> <id>attach-sources</id> <goals> <goal>jar-no-fork</goal> </goals> </execution> </executions> </plugin> ... </plugins> ... </build>
# A programmer's version of the classic bar joke: # An SQL query walks into a bar, sees two tables and asks... can I join you? 😂

Packaging your source JAR along with your project's binaries helps the dev community to gaze into your brilliant mind.

Visual representation

Getting source JARs could be seen as a treasure hunt:

The Maven Repository (🏛️): It houses countless artifacts (groupId:artifactId:version).
Our Quest (🔍): To find the hidden treasure (source JARs).

Steps to winning your Quest:

1. Discover the coordinates of the artifact: `🏛️🗺️ (groupId:artifactId:version)` 2. Unearth the source JAR with `mvn dependency:sources` as your map: `🔍✨` 3. Study the divine wisdom within the scrolls: `📜 (source JAR)`
# Committing in git be like # Initial commit # Final, this time for sure # Oh come on, this is the final for real!

IDE integrations

IntelliJ IDEA

IntelliJ IDEA users can awesomely get the sources right in their IDE with this sweet command:

mvn idea:idea -DdownloadSources=true

Your sources are automatically stitched into the fabric of IntelliJ, boosting your productivity.

Eclipse

For Eclipse users, this command will get you going:

mvn eclipse:eclipse -DdownloadSources=true

An additional tweak in your POM file to embed the maven-eclipse-plugin will ensure a continuous supply of source files:

<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-eclipse-plugin</artifactId> <configuration> <downloadSources>true</downloadSources> </configuration> </plugin>
# Ever wondered why there are bugs in the code? # Because codemakers are bugs themselves. 😁