Configure Gradle to publish sources and javadoc
2017, Gradle 4.0 Edition:
apply plugin: 'maven'
apply plugin: 'maven-publish'
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
artifact sourceJar
artifact packageJavadoc
}
}
}
javadoc {
source = sourceSets.main.allJava
classpath = configurations.compileClasspath
options
{
setMemberLevel JavadocMemberLevel.PUBLIC
setAuthor true
links "https://docs.oracle.com/javase/8/docs/api/"
}
}
task sourceJar(type: Jar) {
classifier = 'sources'
from sourceSets.main.allJava
}
task packageJavadoc(type: Jar) {
from javadoc
classifier = 'javadoc'
}
Works with gradle publish
and gradle publishToMavenLocal
Solution as of Gradle 6.0
Here’s the somewhat minimal configuration you can use if you’re on Gradle 6.0 or later; note the newly introduced withSourcesJar()
and withJavadocJar()
methods:
plugins {
id 'java'
id 'maven-publish'
}
group = 'com.example'
java {
withSourcesJar()
withJavadocJar()
}
publishing {
repositories {
maven {
url = 'file:///tmp/my-repo'
}
}
publications {
myJava(MavenPublication) {
from components.java
}
}
}
Of course, you can also use the ivy-publish
plugin instead of maven-publish
.
See also the Gradle docs:
- on the two new methods
- on the
maven-publish
plugin - on the
ivy-publish
plugin
Add the following code to the build script:
task packageJavadoc(type: Jar, dependsOn: 'javadoc') {
from javadoc.destinationDir
classifier = 'javadoc'
}
task packageSources(type: Jar, dependsOn: 'classes') {
from sourceSets.main.allSource
classifier = 'sources'
}
artifacts {
archives packageJavadoc
archives packageSources
}
Tested with Gradle 1.10