How to add output of task to srcDir of SourceSet - gradle

How can I add the output of a task to a SourceSet. My goal is that the task will implicitly be executed before compileGenJava-task.
sourceSets {
gen {
java {
srcDir "${buildDir}/generated-sources/markup2pojo" // equals output directory of generateSources
}
}
[...]

see https://docs.gradle.org/current/userguide/java_plugin.html#sec:changing_java_project_layout
if you have task that generates code before compile ; you can add the generated folder path ex: build/gensrc
sourceSets {
main {
java {
srcDirs = ['src/java', 'build/gensrc']
}
resources {
srcDirs = ['src/resources']
}
}
}

Related

Idea Gradle integration add classes as a test source tree, but don't include in test sourceSet

I would like to mark the classes of my systest sourceSet as unit test classes. I tried to mark them with the following code:
sourceSets {
main {
groovy {
srcDirs = [
'src/main/masks'
}
resources {
srcDirs += 'src/main/journaltemplates'
}
}
/* This brings up systest in the test resources */
test.java.srcDir 'src/systest/java'
test.resources.srcDir 'src/systest/resources'
systest {
java {
srcDirs = ['src/systest/java']
}
resources {
srcDirs = ['src/systest/resources']
}
}
}
With this solution the sourceset got marked as unit test class, but was additionally added to the test sourceSet which is not desired. I want to keep the classes in the systest sourceSet and specify that the systest sourceSet, is a unit test sourceSet. I want the same behaviour for the systest sourceSet as for the test sourceSet, but they should be distinct sourceSets.
The second solution i tried was using the idea plugin for gradle and modify the module setting, as seen in this SO post:
idea {
module {
testSourceDirs += file('src/systest')
}
}
The problem with this solution is that the systest sources are added to the test sourceSet too.
Hopefully this is clear enough, otherwise please comment. Thank you.
There is a dedicated Gradle feature called Declarative Test Suite that supports this case:
testing {
suites {
val test by getting(JvmTestSuite::class) {
useJUnitJupiter()
}
register("integrationTest", JvmTestSuite::class) {
dependencies {
implementation(project())
}
targets {
all {
testTask.configure {
shouldRunAfter(test)
}
}
}
}
}
}
More:
https://docs.gradle.org/current/userguide/java_testing.html#sec:configuring_java_integration_tests
Please try this configuration:
apply plugin: "idea"
sourceSets {
systest {
java {
compileClasspath = test.output + main.output
runtimeClasspath = output + compileClasspath
}
}
}
idea {
module {
testSourceDirs = sourceSets.systest.allSource.srcDirs
}
}

error occur after setting class outputdir in build.gradle

I want to setting a new dir for compiled java class file when use gradle.
But it never work.
apply plugin: 'java'
//apply plugin: 'war'
sourceSets {
main {
java {
srcDirs = ['src/']
outputDir = 'WebContent/WEB-INF/classes/'
}
resources {
srcDirs = [ 'src/' ]
}
}
}
For gradle > 4. Note for type of the value for outputDir property. It should be File. Not String like in your question's code:
sourceSets {
main {
output.resourcesDir = file('out/res')
java.outputDir = file('out/bin')
}
}
see
https://docs.gradle.org/current/javadoc/org/gradle/api/file/SourceDirectorySet.html#setOutputDir(java.io.File)
and
https://docs.gradle.org/current/javadoc/org/gradle/api/tasks/SourceSetOutput.html
Before version 4 gradle was using single output directory for different jvm languages.
So, for gradle < 4:
sourceSets {
main {
output.resourcesDir = file('out/res')
output.classesDir = file('out/bin')
}
}
see https://docs.gradle.org/3.3/javadoc/org/gradle/api/tasks/SourceSetOutput.html.

How to access list of values from gradle.properties to build.gradle

I have the following gradle.properties
version=2.1
paths=['/home/desk/hie', '/home/mydesk1/hai1', '/home/mydesk2/hai2']
sources=['/src/path/impl', '/src/path/src']
I need to access these paths and sources form gradle.properties to build.gradle
sourceSets {
main {
java {
srcDir=${paths}
}
}
}
but ${paths} is not working.
Can you someone help to get out of this issue and how to use those list in build.gradle file
Consider the following, which converts the paths String to an Iterable (that is, a List):
apply plugin: 'java'
def pathsList = Eval.me(project.ext.paths)
sourceSets {
main {
java {
srcDirs = pathsList
}
}
}
You can share the value using gradle object:
// settings.gradle
gradle.ext {
paths = ['/home/desk/hie', '/home/mydesk1/hai1', '/home/mydesk2/hai2']
}
// build.gradle
sourceSets {
main {
java {
srcDir = paths
}
}
}

Read Strings from a file and putting them into an array in build.gradle file

I have a file that contains list of paths paths.list
current/path/to/hai
current/path/to/hai2
path/to/hai3
path/to/hai4
I want to read this file and place these paths in srcDirs list
sourceSets {
main {
java {
srcDir = ['current/path/to/hai','current/path/to/hai2','path/to/hai3','path/to/hai4']
}
}
}
I have several paths to be added in the list
How to read these paths from list so that they get reflected in scrDir list.
apply plugin: 'java'
def srcDirs = file('paths.list').text.readLines()
srcDirs.each { srcDirectory ->
sourceSets {
main {
java {
srcDir srcDirectory
}
}
}
}

Get SourceSet full path string or path directory

I would like to know if there is a property that returns a SourceSet full path string or file directory, something like ${project.projectDir}/src/${sourceSet.name}.
apply plugin: 'java'
sourceSets {
foo {
java {
srcDir 'example/dir/java'
}
}
}
// For example, this could return pathToProject/src/example/dir
println sourceSets.foo.srcDir
Try this:
productFlavors {
admin { }
customer { }
}
sourceSets {
main {
java.srcDirs = ['src/main']
//other typical sourceSets stuff
}
admin.java.srcDirs = ['src/admin']
customer.java.srcDirs = ['src/customer']
}

Resources