How do I configure Gradle to publish sources and javadoc jars to a repository?
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
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
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:
Paolo Fulgoni's answer used to work for until I bumped up my Gradle version to 3.1. To get the packageJavadoc
Task to work with Gradle 3.1 I found I had to make a slight tweak to it as follows:
task packageJavadoc(type: Jar) {
from javadoc
classifier = 'javadoc'
}