Gradle SourceSets Usage in a task - gradle

I am new to the build tool Gradle and I have defined a source set to exclude few java files. Now, how can I use that source set in my JavaCompile task to avoid those excluded files being compiled ??? Here is my build.gradle and the error.build.gradle
output

Related

Why won't gradle jar build a jar that gradle build does?

In my Gradle project, I define an additional sourceSet.
sourceSets {
sample {
java {
srcDir 'sample/java'
}
compileClasspath += sourceSets.main.output + sourceSets.main.compileClasspath
}
}
I then add a task:
task sampleJar(type: Jar) {
classifier 'sample'
from sourceSets.sample.output
}
artifacts {
archives sampleJar
}
If I do > gradle build the additional jar file builds from the additional source set. However, if I do > gradle jar, it doesn't. any reason why?
When I go through the output messages, I see:
gradle build has sampleJar in the Tasks to be executed:
but
gradle jar doesn't.
But unsure as to why?
Because jar is just the task that assembles the main jar file.
build, on the other hand, is the top-level life-cycle task, which depends on assemble. And assemble is documented as
Depends on: jar, and all other tasks that create artifacts attached to the archives configuration.
Since your sampleJar pecisely creates an artifact attached to the archives configuration, assemble, and thus build depends on it.

Calling Gradle task for only one subproject

How do I configure subprojects for Gradle and run a Gradle task from the build.gradle only for the current directory I'm executing the task from?
For example, suppose I have two projects in a parent folder:
root-project
project-1
project-2
build.gradle
settings.gradle
I have project 1 and 2 included in the settings.gradle file:
include(
'project-1',
'project-2'
)
I have a task configured in the build.gradle file that I would like to call. I've nested the tasks within a subprojects{} block:
subprojects {
...
task check(type: JavaExec) {
main = MAIN_CLASS
classpath = sourceSets.main.runtimeClasspath
}
...
}
However, the build.gradle is run for all subprojects that way.
How can I define all possible subprojects but only call a Gradle task for the current project I'm executing from (i.e., avoid calling the task for all subprojects)? I would like to do this with the current task name only (e.g., gradle check).
Invoking the task in a single project with gradle check is not possible.
You have two options:
Leverage the model Gradle proposes, where tasks can be prefixed by the project name in a multi project build which removes the need to navigate to subprojects.
Which means invoking gradle :project-1:check or gradle :project-2:check depending on which task you want to run.
Define the tasks to have different name in each subprojects. See task rules on how to define tasks with dynamic names.
Which means invoking gradle check1 or gradle check2

Where is the "compileOnly" task in a gradle project?

I created a minimal build.gradle file:
apply plugin: "java"
and put a minimal set of Java files in src/main/java.
When I run:
gradle dependencies
I see various tasks with no dependencies, such as:
compileOnly - Compile only dependencies for source set 'main'.
No dependencies
However, when I try to run one of them:
gradle compileOnly
I see:
Task 'compileOnly' not found in root project '21-java'.
So where is this task and how can I see it?

How can I run Kotlin-Script (*.kts) files from within Gradle?

How can I run Kotlin-Script (*.kts) files from within Gradle?
From the command line, I can call:
kotlinc -script foo.kts
How can I do this from gradle (build.gradle)?
You have two options here to run a gradle build file named build.gradle.kts.
Change the name of your build file in settings.gradle to build.gradle.kts:
rootProject.buildFileName = 'build.gradle.kts' and run gradle <tasknames>.
Run gradle -b build.gradle.kts <tasknames>.
Note that with version gradle 4.21 (current at the time of writing this), you do not need any "buildFileName" entry, or any other entry in settings.gradle. If there is a build.gradle.kts and no build.gradle, gradle will use build.gradle.kts by default.
However having the setting for buildFileName does allow build.gradle to be ignored. Without the setting, you have to remove (or rename) build.gradle in order to run build.gradle.kts.
If you have both a build.gradle and a build.gradle.kts you can switch between the two by commenting and un-commenting the buildFileName setting.

How do I run or create a single Gradle task to run all subprojects and root in a multi-project build?

I have multiple projects configured using build.gradle for each and settings.gradle at the top level.
I want to define or use a single Gradle task that will build all of the subprojects and the root.
How do I run or create a single Gradle task to run all subprojects and root in a multi-project build?
It depends on what all your projects are.
You can call gradle(w) build from the root dir if all your subprojects extend the javaplugin.
Other project types (like the ear plugin) need to be attached to the build task manually. The way I do this is by creating the build task like: task build and in the ear project: build.dependsOn ear

Resources