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
Jformattedtextfield:Input Time Duration Value
Java 8 Stream's .Min() and .Max(): Why Does This Compile
Java: Array of Primitive Data Types Does Not Autobox
How to Have a Textfield Inside a Label
Tool for Analyzing Large Java Heap Dumps
Environment Variable to Control Java.Io.Tmpdir
Differencebetween the Float and Integer Data Type When the Size Is the Same
Issues with Swingworker and Jprogressbar
Named Placeholders in String Formatting
How to Scale Threads According to CPU Cores
How to Find All the Methods That Call a Given Method in Java
How to Convert String to Date Without Knowing the Format
How to Flatten 2D Array to 1D Array
Selenium Ie Webdriver Only Works While Debugging
How to Programmatically Set the Sslcontext of a Jax-Ws Client