How to just override sourceSets in a different gradle file - java

How to just override sourceSets between gradle files without copying over all definitions. I want to define all groovy functions to build Bar Application in build-bar.gradle but just inherit everything from build-bar.gradle and override just the sourceSets in build-baz.gradle file. How can we do this?
Bar Application contains this
build-bar.gradle
sourceSets {
main {
java {
exclude 'com/foo/BarApplication.java'
}
}
}
Baz Application contains this
build-baz.gradle
sourceSets {
main {
java {
exclude 'com/foo/BazApplication.java'
}
}
}

Related

Kotlin Gradle Plugin: How to access `Project` extensions such as `sourceSets`?

In a regular build script you can easily use extensions on Project like Project.sourceSets, for example build.gradle.kts:
sourceSets {
main {
...
}
}
But when I am developing a Gradle plugin in my buildSrc module, I cannot access these. For example:
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.*
class ExamplePlugin : Plugin<Project> {
override fun apply(target: Project) {
target.sourceSets { // error because `sourceSets` can't be resolved.
}
}
}
This is happening despite including the kotlin-gradle-plugin module in my buildSrc dependencies:
plugins {
`kotlin-dsl`
}
repositories {
mavenCentral()
}
dependencies {
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.31")
}
So, how can I access these extensions from within my Gradle plugin?
class ExamplePlugin : Plugin<Project> {
override fun apply(target: Project) {
target.configure<JavaPluginExtension> {
sourceSets {
println(names)
}
}
}
}
See additional notes here: https://docs.gradle.org/current/userguide/kotlin_dsl.html#project_extensions_and_conventions
Basically for plugins, or other times when the plugins applied are not known, the accessors (sourceSets, configurations, etc) of extensions added by other plugins will need to go through a method call which sort of 'retrieves' that scope or object. Further down the link there is also an example of how to get tasks created by other plugins:
val test by target.tasks.existing(Test::class)
test.configure { useJUnitPlatform() }
// or
val test by target.tasks.existing(Test::class) {
useJUnitPlatform()
}
note that if the 'sourceSet' object does not exist on the project (because the java plugin was not applied), an exception will be thrown .
tested with gradle version 7.2, kotlin-dsl version 2.1.6

why new sourceset resources not in buildDir in gradle

I have declared
sourceSets {
micro {
resources.srcDir file('src/micro/resources')
}
}
but
micro/resources
not copied to ${buildDir}/resources/micro.
I want to add micro sourceSet to classpath. Thereby I can run from eclipse or from gradleConsole.
What am I missing here?
It looks like you might not be using the correct syntax: How do I add a new sourceset to Gradle?
Maybe you could try like this:
sourceSets {
micro{
resources {
srcDir 'src/micro/resources'
}
}
}

Dagger2 not working in plain Java IntelliJ Gradle project

I'm trying to get a simple non-Android Java project working with Dagger2 in IntelliJ (2016.3.3, not AndroidStudio) and Gradle (3.1), but nothing seems to get generated at all, I get no DaggerXXX classes output. Not sure if it's the Dagger annotation processing that doesn't work, the source set configuration in IntelliJ that's wrong, or something else. When building the project I get the following output:
build
|_classes
|_main
|_com
| |_<test classes correctly built>
|_generated
|_<empty, but should contain generated Dagger classes?>
What I've done is:
Configured the project to use annotation processing in the IntelliJ settings.
Used the following config:
buildscript {
repositories {
mavenCentral()
jcenter()
maven {
url("https://plugins.gradle.org/m2/")
}
}
dependencies {
classpath "net.ltgt.gradle:gradle-apt-plugin:0.9"
}
}
apply plugin: 'net.ltgt.apt'
apply plugin: 'java'
apply plugin: 'idea'
repositories {
mavenCentral()
}
dependencies {
compile 'com.google.dagger:dagger:2.8'
apt 'com.google.dagger:dagger-compiler:2.8'
}
sourceSets {
main {
java {
srcDirs 'src/main/java'
}
}
}
Test program:
public class Test {
#Inject
TestPojo testPojo;
public static void main(String[] args){
<no generated Dagger component available for building>
}
}
#Component(modules = Module.class)
public interface Component {
Pojo get();
}
#Module
public class Module {
#Provides
public Pojo get(){
return new Pojo();
}
}
public class Pojo {}
Update:
It's working if I build the project through the command line and not through the IntelliJ gui. That points to the issue being within the IntelliJ environment or its Gradle integration somehow, rather than in the build script itself.

Gradle - Groovy and Java class dependency - Compile

My project has both Java (N files) and Groovy code (1 file only). Java compile depends upon this single Groovy file's class file for Java compilation (compileJava task to succeed).
When I don't use src/java as one of the srcDir within main>groovy> sourceSet section, then I get an error saying class/symbol not found which is in the groovy file/class. In ANT, it's easy that we are calling compile-groovy target first, before calling compile-java target but the same in Gradle is what I'm trying to find.
I read some posts and found that if I make main>java section NULL and specify srcDir for main>java which is src/java inside main>groovy sourceSet section, then it compiles fine.
My ?s:
1. Is there any other way to do? for ex, the following should work:
compileJava {
dependsOn compileGroovy
}
though, this goes to an infinte loop.
OR
what about using doFirst for compileJava task:
compileJava {
doFirst {
compileGroovy
}
}
this doesn't work either.
build.gradle This works, but compileJava in one sense becomes useless here even though the source code has N no. of java files in the src/java or src/java-test etc tree. I know this build script is working but logically it might bring some confusion to the developer if s/he is not familiar why sourceSet for Groovy MUST have "src/java" as its srcDir value.
apply plugin: 'java'
apply plugin: 'groovy'
sourceSets {
main {
groovy {
srcDir 'src/groovy'
srcDir 'src/java'
}
java {
//The following needs to be commented out OR Gradle will always pick compileJava before compileGroovy
//srcDir 'src/java'
//srcDir 'src/java-test'
}
}
test {
groovy {
srcDir 'test/groovy'
}
java {
srcDir 'test/java'
}
resources {
srcDir 'test/resources'
srcDir 'conf'
}
}
integrationTest {
groovy {
srcDir 'src/groovy-test'
}
java {
srcDir 'src/java-test'
}
resources {
srcDir 'test/resources'
srcDir 'conf'
}
}
}
Other links:
How to make Gradle compile Groovy tests before Java tests
The Groovy (base) plugin makes GroovyCompile tasks depend on the corresponding JavaCompile tasks because it's more common to call from Groovy into Java than the other way around. If you need it the other way around (or both ways), joint compilation is a good solution. Here is a somewhat improved (over your version) joint compilation setup:
sourceSets {
main {
groovy {
// override the default locations, rather than adding additional ones
srcDirs = ['src/groovy', 'src/java']
}
java {
srcDirs = [] // don't compile Java code twice
}
}
}
If you prefer separate compilation with Java->Groovy dependencies only, something like the following should work:
// since you aren't using the default locations
sourceSets {
main {
groovy {
srcDirs = ['src/groovy']
}
java {
srcDirs = ['src/java']
}
}
}
// remove GroovyCompile->JavaCompile task dependencies
tasks.withType(GroovyCompile) {
dependsOn = []
}
// add JavaCompile->GroovyCompile task dependencies
tasks.withType(JavaCompile) { task ->
dependsOn task.name.replace("Java", "Groovy")
}
Because a JavaCompile task and its corresponding GroovyCompile task write to the same output directory, Java compilation will now have the compiled Groovy code on its compile class path.
PS: Calling a task from another task is not supported, and bad things can happen if you try. Instead, you should always work with task relationships (dependsOn, finalizedBy, mustRunAfter, shouldRunAfter).

buildSrc: Could not get unknown property for object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler

I am trying to reorganize this Android (Java based) library to use the buildSrc folder to define all versions and dependencies as described in this article.
I already set this successfully up for several times for Kotlin bases projects. This time the project is pure Java.
In the buildSrc folder I created the following buildSrc/src/main/java/org/ligi/snackengage/Dependencies.java file:
package org.ligi.snackengage;
public class Dependencies {
public static class Android { /* ... */ }
public static class GradlePlugins {
public static final String ANDROID = "com.android.tools.build:gradle:3.6.3";
// ...
}
public static class Libs { /* ... */ }
}
Then I refer to the definitions in the project root build.gradle among others:
import org.ligi.snackengage.Dependencies.GradlePlugins
apply plugin: "com.github.ben-manes.versions"
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath GradlePlugins.ANDROID
classpath GradlePlugins.MAVEN
classpath GradlePlugins.VERSIONS
}
}
allprojects {
repositories {
google()
jcenter()
}
}
Here is the work in progress branch. When I build the project then the following error occurs:
* Where:
Build file 'SnackEngage/build.gradle' line: 12
* What went wrong:
A problem occurred evaluating root project 'SnackEngage'.
> Could not get unknown property 'GradlePlugins' for object of type
org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
Here is the build log.
You have defined GradlePlugins class as an inner static class of Dependencies, so you need to use Dependencies.GradlePlugins to access it from your build script.
Change your dependencies block as follows:
import org.ligi.snackengage.Dependencies // do not import org.ligi.snackengage.Dependencies.GradlePlugins
buildscript {
// ...
dependencies {
classpath Dependencies.GradlePlugins.ANDROID
classpath Dependencies.GradlePlugins.MAVEN
classpath Dependencies.GradlePlugins.VERSIONS
}
}
EDIT you could also use a static import, as follows:
import static org.ligi.snackengage.Dependencies.*
buildscript {
// ...
dependencies {
classpath GradlePlugins.ANDROID
classpath GradlePlugins.MAVEN
classpath GradlePlugins.VERSIONS
}
}
You need to define variable GradlePlugins with def (in Gradle) or public class GradlePlugins (in Java), before attempting to access it. Kotlin class GradlePlugins should also work.
dependencies {
classpath GradlePlugins.ANDROID
classpath GradlePlugins.MAVEN
classpath GradlePlugins.VERSIONS
}
And I think the buildSrc directory belongs into the module directory, as the Gradle manual shows.

Categories

Resources