How to Build Sources Jar with Gradle

How to build sources JAR with Gradle?

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
}

Building a jar with sources included with Gradle using the Kotlin DSL?

You should probably tweak the jar task, not the Java Plugin extension (the top-level java clause). Try this:

tasks {
withType<Jar> {
from(sourceSets["main"].allSource)
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
}
}

I tested it quickly in my project and it seems to work, but due to some quirks I need that duplicatesStrategy. It should probably work without that for you.

How to create source JAR with Gradle from project files (*.java, *.js, *,xml, etc.)?

Use task javadocJar and sourceJar

task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}

task sourcesJar(type: Jar, dependsOn: classes) {
classifier = 'sources'
from sourceSets.main.allSource
}

artifacts {
archives javadocJar
archives sourcesJar
}

Build source jar with Gradle Kotlin DSL?

The following will work:

val sourcesJar by creating(Jar::class) {
dependsOn(JavaPlugin.CLASSES_TASK_NAME)
classifier = "sources"
from(sourceSets["main"].allSource)
}

val javadocJar by creating(Jar::class) {
dependsOn(JavaPlugin.JAVADOC_TASK_NAME)
classifier = "javadoc"
from(tasks["javadoc"])
}

artifacts {
add("archives", sourcesJar)
add("archives", javadocJar)
}

A complete build.gradle.kts would look like this:

plugins {
kotlin("jvm") version "1.2.71"
}

repositories {
mavenCentral()
}

dependencies {
}

tasks {
val sourcesJar by creating(Jar::class) {
dependsOn(JavaPlugin.CLASSES_TASK_NAME)
classifier = "sources"
from(sourceSets["main"].allSource)
}

val javadocJar by creating(Jar::class) {
dependsOn(JavaPlugin.JAVADOC_TASK_NAME)
classifier = "javadoc"
from(tasks["javadoc"])
}

artifacts {
add("archives", sourcesJar)
add("archives", javadocJar)
}
}

Creating a single source jar for a multi-module gradle project

We have found a solution - deploy the sub-modules as independent sources jar and include them on an individual basis.

For example, in each sub-module:

sourceSets {
main.kotlin.srcDirs += 'src/main/kotlin'
main.java.srcDirs += 'src/main/java'
}

task sourcesJar(type: Jar) {
from sourceSets.main.kotlin
from sourceSets.main.java
archiveClassifier = 'sources'
}

publishing {
publications {
mavenJava(MavenPublication) {
groupId = rootProject.group
artifactId = "$rootProject.name-$project.name"
version = rootProject.project.version
from components.java
artifact sourcesJar
}
}

Gradle generate sources.jar for only public interfaces

You could try adding something like this, instead of your include:

from 'src/main/java'
eachFile { currentFile ->
String contents = new File(currentFile.getSourcePath()).text
if(!contents.contains("public class")) {
currentFile.exclude()
}
}

I'm not entirely sure if that works, but it should set you on the right path to where you want to go.

Since Gradle does not actually do any code analysis, you can't just simply say "only include files that have classes that are public". Instead, you have to either write a custom plugin that will only include public classes, or do something like what I provided. It includes everything from the source directory, but runs a little bit of code on each file. First, it gets the contents of the file, then it checks if that file contains public class. If not, the file does't have a public class, and should be excluded.

Hope this helps! Feel free to ask any more questions if you have any.

How to create a JAR file from gradle that contains the .java fields (source code) as well

Try adding this to your build.gradle.kts and see if it works:

tasks.named<ProcessResources>("processResources") {
from("src/main/java")
}

When your uber-JAR builds, it should contain the source files. Though this will also add the source files to any JAR. If that is not what you want, then you will need to configure something similar in the uber-JAR setup.



Related Topics



Leave a reply



Submit