According to Gradle documentation: sourceCompatibility is "Java version compatibility to use when compiling Java source." targetCompatibility is "Java version to generate classes for."
1.1 What is a Gradle SourceSet ? A SourceSet is a collection of java source files and additional resource files that are compiled and assembled together to be executed. The main idea of sourcesets is to group files with a common meaning for the project, with no need of separate them in another project.
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
}
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
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 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:
java
and maven-publish
pluginstask 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)
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With