I am working with an open source project that is built with Gradle. I would like to generate a (project)-sources.jar file that I can load into my IDE (IntelliJ IDEA) and debug through the project. I know how to load the file if I can generate it.
I have looked through the available Gradle tasks for the project and there isn't one that generates a sources jar file.
What is the easiest way to generate a sources jar file for this project?
Adding the source into the jar file that contains the compiled class files would be fine as well.
task sourcesJar(type: Jar, dependsOn: classes) {
classifier = 'sources'
from sourceSets.main.allSource
}
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}
artifacts {
archives sourcesJar
archives javadocJar
}
Solution as of Gradle 6.0
Assuming that you use the java/java-library plugin with Gradle 6.0 or later, you can get a sourcesJar task using the following configuration:
java {
withSourcesJar()
// and/or analogously use “withJavadocJar()” to get a “javadocJar” task
}
If you additionally use the maven-publish/ivy-publish plugin (recommended nowadays), then this will also publish a *-sources.jar artifact along with your main Java publication.
See also the Gradle docs.
If you're using Android:
task sourcesJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier = 'sources'
}
task javadoc(type: Javadoc) {
source = android.sourceSets.main.java.srcDirs
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
}
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}
artifacts {
archives javadocJar
archives sourcesJar
}
from here
If you wish to add the sources to the compiled classes JAR file, which you also said would be acceptable, you can do that easily enough. Just add the following to your build file. You can see that, in theory, it is quite like the solution for putting sources into a separate JAR:
jar {
from sourceSets.main.allSource
}
The difference is that you are adding it to the main JAR file by saying "jar" in lieu of sourcesJar.
this should work
assemble.dependsOn 'propertyJar'
task propertyJar(type: Jar) {
archiveName = "myJarName.jar"
from('src/main/resources') {
include '**'
}}
When using:
Gradle: 5+
java and maven-publish plugins
task sourceJar(type: Jar) {
from sourceSets.main.allJava
archiveClassifier = "sources"
}
publishing {
publications {
maven(MavenPublication) {
from components.java
artifact sourceJar
}
}
}
https://docs.gradle.org/current/dsl/org.gradle.api.publish.maven.MavenPublication.html
The Kotlin DSL equivalent would be:
tasks {
val sourcesJar by creating(Jar::class) {
dependsOn(JavaPlugin.CLASSES_TASK_NAME)
classifier = "sources"
from(java.sourceSets["main"].allSource)
}
val javadocJar by creating(Jar::class) {
dependsOn(JavaPlugin.JAVADOC_TASK_NAME)
classifier = "javadoc"
from(java.docsDir)
}
artifacts {
add("archives", sourcesJar)
add("archives", javadocJar)
}
}
This is how I included Dokka (view it online) and sources JARs for my Android Kotlin library using Kotlin DSL (build.gradle.kts):
plugins {
// ...
id("org.jetbrains.dokka") version "1.4.32"
id("maven-publish")
}
lateinit var sourcesArtifact: PublishArtifact
lateinit var javadocArtifact: PublishArtifact
tasks {
val sourcesJar by creating(Jar::class) {
archiveClassifier.set("sources")
from(android.sourceSets["main"].java.srcDirs)
}
val dokkaHtml by getting(org.jetbrains.dokka.gradle.DokkaTask::class)
val javadocJar by creating(Jar::class) {
dependsOn(dokkaHtml)
archiveClassifier.set("javadoc")
from(dokkaHtml.outputDirectory)
}
artifacts {
sourcesArtifact = archives(sourcesJar)
javadocArtifact = archives(javadocJar)
}
}
publishing {
// ...
publications {
create<MavenPublication>("MyPublication") {
from(components["release"])
artifact(sourcesArtifact)
artifact(javadocArtifact)
// ...
}
}
}
Android:
task androidSourcesJar(type: Jar) {
getArchiveClassifier().set('sources')
from android.sourceSets.main.java.srcDirs//full sources
}
Java:
task sourcesJar(type: Jar, dependsOn: classes) {
getArchiveClassifier().set('sources')
from sourceSets.main.allSource
}
Related
Basically what I'm trying to do is publish a jar file to GitHub Packages with a certain name. What I have now is:
shadowJar {
archiveFileName = "Some-Name-${parent.version}.${extension}"
}
publishing {
...
publications {
shadow(MavenPublication) { publication ->
project.shadow.component(publication)
artifactId = 'me.project'
groupId = 'some-project'
version = 1.1.0
}
}
}
But from this I get some-project-1.1.0-all.jar, I would like to get some-project-1.1.0.jar but cant seem to find the way how. Changing the archiveFileName in the shadowJar task doesn't seem to affect the publishing jar only the build jar.
I believe you need to change the archiveClassifier of the shadowJar task. By default, this is configured as all.
Point 5: https://github.com/johnrengelman/shadow/blob/master/src/docs/getting-started/README.md#default-javagroovy-tasks
https://github.com/johnrengelman/shadow/blob/7.0.0/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowJavaPlugin.groovy#L66
Something like:
tasks.shadowJar {
archiveClassifier = ""
}
The shadowJar task extends of the Jar task type. By default, with the Java plugin, archiveClassifier is configured as an empty String "". The Shadow plugin reconfigures its shadowJar task with all.
I have inherited a codebase which I suspect was originally built with Gradle 4 (but I don't know for sure). I am using Gradle 5.5.1 and when I run gradle I get errors to do with publication to a Maven repo:
* What went wrong:
A problem occurred evaluating root project 'common'.
> Could not find method sourcesJar() for arguments [build_d1u03z05r8d12r3e8b5qq1fxm$_run_closure3$_closure13$_closure15$_closure16#190bc2b8] on object of type org.gradle.api.publish.maven.internal.publication.DefaultMavenPublication.
Add sourcesJar task to custom Gradle plugin looks like a similar problem but it is a different error and their solution doesn't work anyway.
The relevant parts of my build.gradle are:
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
artifact sourcesJar {
classifier "sources"
}
artifact testJar {
classifier "tests"
}
}
}
repositories {
maven {
url 'http://repo.url'
credentials {
username "$username"
password "$password"
}
}
}
}
task sourcesJar(type: Jar) {
from sourceSets.main.allSource
classifier = 'sources'
}
task testJar(type: Jar) {
from sourceSets.test.output
classifier = 'tests'
}
Okay I think I figured it out: https://docs.gradle.org/5.5.1/userguide/publishing_maven.html#publishing_maven:deferred_configuration says that a publishing block was executed after the rest of the project in Gradle 4, but not in Gradle 5.
So, changing
artifact sourcesJar {
classifier "sources"
}
artifact testJar {
classifier "tests"
}
to
afterEvaluate {
artifact sourcesJar {
classifier "sources"
}
artifact testJar {
classifier "tests"
}
}
got me a little further. With that change I then got this error:
* What went wrong:
A problem occurred configuring root project 'common'.
> Cannot create a Publication named 'sourcesJar' because this container does not support creating elements by name alone. Please specify which subtype of Publication to create. Known subtypes are: MavenPublication
https://discuss.gradle.org/t/cannot-create-a-publication-named-x/3726 and Gradle: Using 'maven-publish' plugin in custom standalone plugin seem to suggest that a prefix of project. should fix it.
So changing it to:
afterEvaluate {
artifact project.sourcesJar {
classifier "sources"
}
artifact project.testJar {
classifier "tests"
}
}
seems to work, though I'm a little iffy on the project. prefix.
In my case I had to add the following that Gradle was aware about the artifacts:
java {
withJavadocJar()
withSourcesJar()
}
Then I was able to use it this way:
publishing.publications {
mavenJava(MavenPublication) {
from components.java
}
}
Javadoc as well as Sources were published. There seems to be no need to add the afterEvaluate block.
In this block:
artifact sourcesJar {
classifier "sources"
}
artifact testJar {
classifier "tests"
}
remove the closures...just make it look like this:
artifact sourcesJar
artifact testJar
I am currently building a jar using Gradle. I also generate source and javadoc jars. So far everything is by the book.
Now I want to create an additional jar, that has different content (few classes ignored). This jar should be published using a classifier.
Snippets from build.gradle:
apply plugin: 'java'
apply plugin: 'jacoco'
apply plugin: 'osgi'
apply plugin: 'maven'
apply plugin: 'signing'
configurations {
published
}
task sourcesJar(type: Jar, dependsOn: classes) {
classifier = 'sources'
from sourceSets.main.allSource
}
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}
artifacts {
archives jar
archives sourcesJar
archives javadocJar
published jar
published sourcesJar
published javadocJar
}
I am not able to create an additional jar that is going to have everything: manifest updated by OSGI plugin, POM file updated, and signed. I am able to produce an additional JAR, but that jar is just a simple archive, and not signed/updated by plugins etc.
How to create an additional jar that is going to be named with classifier but treated as the regular jar (signed, uploaded to maven central, etc...)?
My try:
configure (java8Modules()) {
configurations {
java8
}
task code8Jar(type: Jar, dependsOn: classes) {
classifier = 'java8'
from sourceSets.main.output.classesDirs
}
artifacts {
java8 code8Jar
published code8Jar
}
}
gives me a jar, but it is not signed, and manifest is not modified.
If you're looking for sample gradle protobuf project look here.
I'm having hard time with gradle and protobuf,
i want to create a simple gradle project that will take any proto files from default src/main/proto, src/test/proto and compile them to src/main/java, src/test/java accordingly, then pack that into a jar and publish to local repo.
Unfortunately i'm new to gradle and cant figure out how the original project is composed.
Here is my unfinished build.gradle file
apply plugin: 'java'
apply plugin: "com.google.protobuf"
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.7.0'
}
}
repositories {
mavenCentral()
}
dependencies {
compile 'com.google.protobuf:protobuf-java:3.0.0-beta-1'
}
sourceSets {
main {
proto {
srcDir 'src/main/proto'
}
java {
srcDir 'src/main/java'
}
}
test {
proto {
srcDir 'src/test/proto'
}
proto {
srcDir 'src/test/java'
}
}
}
protobuf {
// Configure the protoc executable
protoc {
// Download from repositories
artifact = 'com.google.protobuf:protoc:3.0.0-alpha-3'
}
generateProtoTasks {
// all() returns the collection of all protoc tasks
all().each { task ->
// Here you can configure the task
}
// In addition to all(), you may get the task collection by various
// criteria:
// (Java only) returns tasks for a sourceSet
ofSourceSet('main')
}
}
After runing jar task we have this :
as you can see gradle builds both test and main protos to the same classes directory (red arrows), in the jar i can see both generated classes included (while tests should be skipped).
but the main problem is that I want to make compile proto files directly to appropriate source directories (blue arrows), after that ordinary build will do the correct thing... After all we need those classes in src to use them in business logic...
So we only need one task that compiles proto to appropriate src directory... nothing more.
src/main/proto to src/main/java
src/test/proto to src/test/java
The current project as it is is located here. Please help to configure this, i'm pretty sure lot of people will need it later...
If I don't misunderstand your question it's quite simple to solve. If you don't want to distinguish between your own and the generated sources you just have to add set the generatedFileBaseDir like this generateProtoTasks.generatedFilesBaseDir = 'src'
So the entire build file looks like:
// ...
protobuf {
// Configure the protoc executable
protoc {
// Download from repositories
artifact = 'com.google.protobuf:protoc:3.0.0-alpha-3'
}
generateProtoTasks.generatedFilesBaseDir = 'src' // <- that line
generateProtoTasks {
// all() returns the collection of all protoc tasks
all().each { task ->
// Here you can configure the task
}
Than your folder looks like:
src/main/java/com/vach/tryout/AddressBookProtos.java
src/main/java/com/vach/tryout/protobuf/Main.java
BUT:
That might not be the best idea to mix generate with handcrafted source code. So my suggestion would be to generate the source code into an own directory like generatedSources and add this directory to the java sourceSet. The build file would look like this:
sourceSets {
main {
proto {
srcDir 'src/main/proto'
}
java {
// include self written and generated code
srcDirs 'src/main/java', 'generated-sources/main/java'
}
}
// remove the test configuration - at least in your example you don't have a special test proto file
}
protobuf {
// Configure the protoc executable
protoc {
// Download from repositories
artifact = 'com.google.protobuf:protoc:3.0.0-alpha-3'
}
generateProtoTasks.generatedFilesBaseDir = 'generated-sources'
generateProtoTasks {
// all() returns the collection of all protoc tasks
all().each { task ->
// Here you can configure the task
}
// In addition to all(), you may get the task collection by various
// criteria:
// (Java only) returns tasks for a sourceSet
ofSourceSet('main')
}
}
Your directory will look like this
src/main/proto/dtos.proto
src/main/java/com/vach/tryout/protobuf/Main.java
generated-sources/main/java/com/vach/tryout/AddressBookProtos.java
A nice side effect is that you can ignore this generated-sources dir in your git configuration. That's always a good idea not to publish generated source code.
How do I configure Gradle to publish sources and javadoc jars to a repository?
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
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
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'
}