How to Add a New Sourceset to Gradle

How do I add a new sourceset to Gradle?

Update for 2021:

A lot has changed in 8ish years. Gradle continues to be a great tool. Now there's a whole section in the docs dedicated to configuring Integration Tests. I recommend you read the docs now.

Original Answer:

This took me a while to figure out and the online resources weren't great. So I wanted to document my solution.

This is a simple gradle build script that has an intTest source set in addition to the main and test source sets:

apply plugin: "java"

sourceSets {
// Note that just declaring this sourceset creates two configurations.
intTest {
java {
compileClasspath += main.output
runtimeClasspath += main.output
}
}
}

configurations {
intTestCompile.extendsFrom testCompile
intTestRuntime.extendsFrom testRuntime
}

task intTest(type:Test){
description = "Run integration tests (located in src/intTest/...)."
testClassesDir = project.sourceSets.intTest.output.classesDir
classpath = project.sourceSets.intTest.runtimeClasspath
}

How to add new sourceset with gradle kotlin-dsl

You should try the following:

java.sourceSets.create("src/gen/java")

Hope it's what you need!

Using a Custom SourceSet in Gradle

The sourceSets property (an extension property for Project) returns a SourceSetContainer which extends NamedDomainObjectContainer<SourceSet>. The latter interface defines get(String) (an operator extension function) which is what allows you to use the ["..."] syntax. That method returns T directly, meaning you should just be using:

from(sourceSets["demo"].output)

The reason you have to use get() when using sourceSets.main is because you're getting a NamedDomainObjectProvider<SourceSet> which wraps the source set.

You can also get the custom source set via delegation

  1. When creating it:

    val demo by sourceSets.creating { /* configure */ }
  2. Or some time after creating it:

    val demo by sourceSets

Gradle: custom source set as dependency for the main and test ones

sourceSets {
main {
compileClasspath += generated.output
runtimeClasspath += generated.output
}
}

Same for the test source set.

Gradle how to build extra source sourceSets?

Use gradle task --all to list all task.

You may see:

compileExtraJava - Compiles extra Java source.

So, the command to build extra is

 gradle compileExtraJava

Add another java source directory to gradle script

I agree with @JB Nizet about respecting standard conventions. If you still insist on being an Anarchist though:

You already have src declared in your sourceset, why not add src1 and src2 as well? You can add them to the same sourceset, or define a sourceset per module if you want.

sourceSets {
main {
java {
srcDirs 'src'
srcDirs 'src1'
srcDirs 'src2'
}
}
}

To reference files outside the project, see this answer.

How to define custom source set without defining it's path explicitly in Gradle?

Gradle default tasks will not build non-default source sets unless there is an explicit dependency on them in the main chain. In order to compile your demo classes you'd have to call gradle demoClasses as per cricket_007's answer - or better yet, just do gradle build demo which will also put the generated classes in the target jar.

All you need is this :

sourceSets {
demo
}

... and the build demo task will indeed create any missing directory as expected, unless you have some weird permission scheme in effect.

Now I have looked at your git project and it seems your demo source set depends on the main classes. You need to make this dependency clear (i.e. add them to the demo classpaths) to avoid compilation errors :

sourceSets {
main
demo {
compileClasspath += main.output
runtimeClasspath += main.output
}
}


Related Topics



Leave a reply



Submit