Is it possible to dynamically create source sets? - gradle

I am wondering if it is possible to dynamically create a source set with gradle. The directory hierarchy of my current project looks as follows:
dbfit-junit
module
foo
bar
Each of this module folders (foo and bar) should have its own source set assigned. The reason is that I want to dynamically create tasks like dbFitTestFoo and dbFitTestBar. My current approach looks like this:
ext.dbFitModulesDir = "dbfit-junit/module"
ext.dbFitTestSpecs = ["java", "groovy", "scala", "resources"]
ext.dbFitModules = []
file(dbFitModulesDir).eachDir{ module ->
dbFitModules << module.name
}
/** this needs to be done dynamically for each "module" **/
sourceSets {
integrationTest { sourceSet ->
dbFitModules.each{ module ->
dbFitTestSpecs.each { spec ->
if (!sourceSet.hasProperty(spec)) {
return
}
sourceSet."$spec".srcDir file("$dbFitModulesDir/$module/$spec")
}
}
}
}
dbFitModules.each{ module ->
task "dbFitTest${module.capitalize()}"(type: Test) {
group = "Verification"
description = "Run dbFit tests for $module"
doLast {
testClassesDir = sourceSets.integrationTest.output.classesDir
classpath = sourceSets.integrationTest.runtimeClasspath
}
}
}
The creation of the tasks works smoothly, the only thing that is still missing is the dynamic creation and assignment of the sourcesets.
Thanks for any hints!

Yes, you can create source sets dynamically. Here's one example:
dbFitModules.each { module ->
sourceSets.create("${module}Test") {
...
}
}

Related

Configuration inheritance and resolution

I have some trouble with gradle and I don't know why this doesn't work:
Project A with 2 sub-project B and C
B and C have a configuration named masterConfiguration and superConfiguration (who extend masterConfiguration)
I add some dependencies in both.
When I do this:
configurations.superConfigurations.resolvedConfiguration.files
All is fine, and I have all files from superConfiguration and masterConfiguration.
Now, the problem.
I create a configuration (projectAConfiguration) in the project A (the rootProject).
This configuration extends superConfiguration from B and C.
I add no new dependencies in this one.
If I do this:
configurations.projectAConfiguration.resolvedConfiguration.files
I have nothing. I don't understand why?
settings.gradle =>
include 'B'
include 'C'
build.gradle =>
configurations {
projectAConfiguration
}
def rootConfiguration = configurations.projectAConfiguration
subprojects {
configurations {
masterConfiguration
superConfiguration {
extendsFrom masterConfiguration
}
}
rootConfiguration.extendsFrom configurations.superConfiguration
dependencies {
masterConfiguration 'group:artifactid:version'
superConfiguration 'anotherGroup:anotherArtifactid:version'
}
//ALL IS OK
println configurations.superConfiguration.resolvedConfiguration.files
}
//NOT OK
println configurations.projectAConfiguration.resolvedConfiguration.files
I have solve my problem with this solution :
Add task on all subproject.
task resolveMyConf {
doLast {
it.ext.confResolve = it.configurations.superConfiguration..resolvedConfiguration.files
}
}
and in the project A
task resolveAllConf {
suprojects.each {
dependsOn it.resolveMyConf
}
doLast {
//and now, we can collect all task result
}
}
I don't know if it's good, maybe there is a better solution. But it works.
You can get the configuration of a sub-project just by referencing the project in the same way as you would declare a dependency to it. You don't need to create another configuration that extends it.
For example (Groovy DSL):
// Root project A
task printConfigurationB {
doLast {
println project("B").configurations.superConfiguration.resolve()
}
}

Re-using properties of custom gradle task

I'm defining gradle task like this:
task assembleAppPackage() {
File distDir = file("${projectDir}/dist")
File binDir = file("${distDir}/bin")
File configDir = file("${distDir}/config")
File libDir = file("${distDir}/lib")
doLast {
...using distDir , binDir, etc...
}
}
Somewhere later I want to add some functionality to assembleAppPackage, so I expect something like this to work:
assembleAppPackage {
doLast {
copy {
from "${projectDir}/bin"
into binDir #binDir from original task definition
}
}
}
And gradle claims there is no binDir in scope:
Could not get unknown property 'binDir' for object of type org.gradle.api.internal.file.copy.CopySpecWrapper_Decorated.
How to define task properties which could be later accessed on task extensions? Is it only possible with defining task class?
You can use extra properties for this I think:
task assembleAppPackage() {
ext.distDir = file("${projectDir}/dist")
ext.binDir = file("${distDir}/bin")
ext.configDir = file("${distDir}/config")
ext.libDir = file("${distDir}/lib")
doLast {
...using distDir , binDir, etc...
}
}
(rest of your code unchanged)
On most gradle entities you can use that concept: Set a property using "ext." or
ext {
name1 = value1
name2 = value2
}
See https://docs.gradle.org/current/dsl/org.gradle.api.plugins.ExtraPropertiesExtension.html or google for "gradle extra properties"

refer to Gradle task which is defined later in build

Our build has some generic logic to create tasks from configuration data. There are also several Gradle files on whose execution order I do not want to depend.
Now I need to add a task dependency to a task without knowing if that task will be defined before my part of the build script runs (at configuration time) or after my part.
[Edit: as #canillas writes I can say myTask.dependsOn "otherTask" and "lazily" depend on yet undefined "otherTask", but I need it the other way around.]
Since I can't write tasks["otherTask"].dependsOn myNewTask before "otherTask" is defined, I made the following helper function:
void safelyDoWithTask(String taskName, Closure func) {
def task = tasks.findByName(taskName)
if (task != null) {
func(task)
} else {
project.tasks.whenTaskAdded { task_ ->
if (taskName.equals(task_.getName())) {
func(task_)
}
}
}
}
safelyDoWithTask('otherTask', { it.dependsOn myNewTask })
Now I wonder if there is more canonical way to achieve this?
Consider the following:
// section 1
def dynamicDependencies = [:]
dynamicDependencies['otherTask'] = 'myNewTask'
// section 2
afterEvaluate { project ->
// taskA dependsOn taskB
dynamicDependencies.each { taskA, taskB ->
def task = project.tasks.findByName(taskA)
if (task) { task.dependsOn "${taskB}" }
}
}
// section 3
task myNewTask() {
doLast {
println 'myNewTask !'
}
}
task otherTask() {
doLast {
println 'otherTask !'
}
}
The gist is:
section 1 defines our dependency info in a custom map
section 2 uses the afterEvaluate hook to process the map
because the above is decoupled from the task definitions, we can simply put them in section 3 (or wherever)

How to export dependencies to different locations with Gradle?

I wanted to avoid creating different configurations because I would like to have many different behaviors associated with my dependencies, not just this one.
So thanks to 'ext' properties I am able to associate my own data to the dependencies:
runtime 'org.apache.logging.log4j:log4j-api:2.5'
runtime 'org.apache.lucene:lucene-core:5.4.1'
runtime('hsqldb:hsqldb:1.8.0.10') { ext.exportTo = 'jdbc' }
Now I want to exploit it to copy the dependencies into 'libs/' by default, and 'libs/${exportTo}/' if exportTo is specified.
task copyRuntimeLibs(type: Copy) {
into "build/dependencies"
from configurations.runtime
}
So I'm trying to create a dynamically created configuration for those special dependencies, and a dynamically created task to do the job, which I link to the main task 'copyRuntimeLibs':
configurations.runtime.allDependencies.withType(ExternalModuleDependency).each { d ->
if (d.hasProperty("exportTo")) {
def key = "${d.group}:${d.name}:${d.version}"
def configname = "config-${key}"
def c = project.configurations.create(configname)
c.getDependencies().add(d)
def task = task "task-${key}"(type: Copy) {
into "build/dependencies/${d.ext.exportTo}"
from c
}
project.tasks.add(task)
copyRuntimeLibs.dependsOn task
}
}
So the dynamic tasks is executed, however nothing is copied. When I add a 'eachFiles' to debug, it doesn't go there.
Any clue?
task copyRuntimeLibs
copyRuntimeLibs << {
configurations.runtime.allDependencies.withType(ExternalModuleDependency).each { d ->
def exportFolder = 'build/dependencies'
def exportSub = ''
if (d.hasProperty("exportTo")) {
exportSub = d.ext.exportTo
}
def key = "${d.group}:${d.name}:${d.version}"
def c = project.configurations.create("config-${key}")
c.getDependencies().add(d)
copy {
from c
into "${exportFolder}/${exportSub}/"
}
}
}

How to keep Java code and Junit tests together building with Gradle

I have a project in which the main source and the test cases for that source are kept in the same package/directory. Each test class is the name of the class which it is testing with "Test" appended on the end. So if I have a Foo.java there will be a FooTest.java right next to it.
My question is, how do I build this project with Gradle? I'd still like to keep the class files separate, i.e. a folder for main classes and a folder for test classes.
This should do the trick:
sourceSets {
main {
java {
srcDirs = ["some/path"]
exclude "**/*Test.java"
}
}
test {
java {
srcDirs = ["some/path"]
include "**/*Test.java"
}
}
}
For reference, here is the code I used to try to get around the Eclipse plugin's classpath issue. Using this in combination with Peter's answer above seems to work.
// The following ensures that Eclipse uses only one src directory
eclipse {
classpath {
file {
//closure executed after .classpath content is loaded from existing file
//and after gradle build information is merged
whenMerged { classpath ->
classpath.entries.removeAll { entry -> entry.kind == 'src'}
def srcEntry = new org.gradle.plugins.ide.eclipse.model.SourceFolder('src', null)
srcEntry.dir = file("$projectDir/src")
classpath.entries.add( srcEntry )
}
}
}
}
this work for me:
eclipse {
classpath {
file {
withXml {
process(it.asNode())
}
}
}
}
def process(node) {
if (node.attribute('path') == 'src/test/java' || node.attribute('path') == 'src/test/resources')
node.attributes().put('output', "build/test-classes")
else
node.children().each {
process(it)
}}

Resources