How to build sources JAR with Gradle?
Creating a source JAR in Gradle can be done by adding a sourcesJar
task, as shown below:
Next, link it to your Gradle build with:
In the above snippet, Gradle is configured to assemble your .java
files into a sources
JAR, ready to be integrated into your project's build artifacts.
Advanced Gradle 6.0+ methods
In Gradle version 6.0 and above, the java-library
plugin provides a simple method to get the job done faster than a coder can grab his next cup of coffee!
This helps automate the tasks for generating Javadoc and sources JARs. Use the following to apply it:
Detailed configurations
Setting task dependencies
Your source JAR may depend on other tasks. Here's how to wait in line with dependsOn
:
With the sourcesJar
task above, compileJava
and processResources
won't cut in line!
Excluding specific files
Not all .java
files need a VIP pass to your source JAR. To keep some out:
Setting up classpath for Javadoc
To ensure your Javadoc
task has all necessary permissions:
Testing the built source JAR
After assembling your sources JAR
, test it in your IDE (like IntelliJ IDEA) to ensure it's ready for the debugging feast:
Special project configurations
To handle surprise ingredients in the project, check your project configurations before generating the source JARs:
Troubleshooting
Problems popping up? Double-check the classpath and archiveName, and make sure you've correctly chucked in the source directories:
Was this article helpful?