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
When creating it:
val demo by sourceSets.creating { /* configure */ }
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
How to Restrict Jfilechooser to a Directory
Convert Arraylist into 2D Array Containing Varying Lengths of Arrays
Jackson - Serialization of Entities with Birectional Relationships (Avoiding Cycles)
Peer Not Authenticated While Importing Gradle Project in Eclipse
Using Javafx in Jre 8, "Access Restriction" Error
How to Check the Bitness of My Os Using Java? (J2Se, Not Os.Arch)
How to Encode Uri Parameter Values
Abstractmethoderror Using Uribuilder on Jax-Rs
Error:Java: Invalid Source Release: 8 in Intellij. What Does It Mean
Can Spring Security Use @Preauthorize on Spring Controllers Methods
Java 7 String - Substring Complexity
How to Create a Custom Exception Type in Java
Generating a Random Number Between 1 and 10 Java
How to Check If a Url Exists or Returns 404 with Java
Java Convert Gmt/Utc to Local Time Doesn't Work as Expected
Hibernate One-To-One: Getid() Without Fetching Entire Object