How to set property in closure in Groovy, like in Gradle? - gradle

Here is the Gradle documentation, which I don't understand:
idea {
//if you want parts of paths in resulting files (*.iml, etc.) to be replaced by variables (Files)
pathVariables GRADLE_HOME: file('~/cool-software/gradle')
module {
//if for some reason you want to add an extra sourceDirs
sourceDirs += file('some-extra-source-folder')
It sets idea.module.sourceDirs value.
How do I implement it in pure Groovy without Gradle? I want to know its underlying merchanism.

thanks #Steinar, I use following code fix:
def file(String p) {
new File(p)
}
class Idea {
class Module {
Set<File> excludeDirs = []
}
void module(Closure c) {
c.delegate = new Module()
c()
}
}
void idea(Closure c) {
c.delegate = new Idea()
c()
}
idea {
module {
excludeDirs += file("smth")
}
}

With your proposed solution #asullaherc you can't choose between calling idea { module and idea.module { ... so I would rather go for a different solution.
This is the base class for all of the closures you want to create for your case:
class SectionBase extends Closure<Object> {
SectionClosure() {
super(null)
}
Object doCall(final Closure inner) {
inner.delegate = this
inner()
}
}
And this would be an implementation ("Gradle style"):
class DependenciesSection extends SectionBase {
void compile(packageName) {
println "compile: $packageName"
}
void testCompile(packageName) {
println "testCompile: $packageName"
}
}
Now we make an instance:
def dependencies = new DependenciesSection()
And mess around with it a little:
// You can do it either this way...
dependencies {
compile 'com.my.package1'
compile 'com.my.package2'
testCompile 'com.my.package3'
}
// ...or also the other
dependencies.compile 'com.my.package4'
dependencies.testCompile 'com.my.package5'
The output would be:
compile: com.my.package1
compile: com.my.package2
testCompile: com.my.package3
compile: com.my.package4
testCompile: com.my.package5

Related

How to add arguments to a org.gradle.api.Action?

I'm writing a custom Gradle plugin that registers an extension and uses Actions for configuring it so you can use it like
myExtension {
info {
someConfig("a") {
// config for someConfig["a"]
}
someConfig("b") {
// config for someConfig["b"]
}
}
}
Based on how much someConfig you define, the plugin created new Gradle tasks dynamically. I've seen some scenarios in which it'd useful to configure all someConfig with the same values, so instead of writing
myExtension {
info {
someConfig("a") {
someValue = x
}
someConfig("b") {
someValue = x
}
}
}
I was thinking in providing something like
myExtension {
info {
someConfig("a")
someConfig("b")
allConfigs { configName ->
someValue = x
}
}
}
When trying to code it, I've used an objectFactory for creating instances of Action so I can configure the extension values
val allMyConfigs = mutableMapOf<String, SomeConfig>()
fun someConfig(configName: String, init: Action<in EnvironmentInfo>) {
val myConfig = allMyConfigs.computeIfAbsent(env) {
objectFactory.newInstance(SomeConfig::class.java, env)
}
init.execute(environmentInfo)
}
but since Action is a (SomeConfig) -> Unit (i.e. a function receiving a SomeConfig instance) I'm unable to pass the config name (an String) as param
Is there a way to create an Action, so it's compatible with both build.gradle and build.gradle.kts files, that receives an extra parameter and maintain the SomeConfig as delegate?

Declare Gradle buildSrc plugin using Kotlin DSL

I'm trying to figure out how to convert this configuration to the Kotlin DSL, but I can't find much in the way of examples:
gradlePlugin {
plugins {
javaConventionsPlugin {
id = "build.java-conventions"
implementationClass = "buildlogic.plugins.JavaConventionsPlugin"
}
}
}
What would this declaration look like using Kotlin?
It is documented in the guide: https://docs.gradle.org/current/userguide/java_gradle_plugin.html#sec:gradle_plugin_dev_usage
The way you have also works. Any of the following would also work:
gradlePlugin {
plugins {
register("javaConventionsPlugin") {
id = "build.java-conventions"
implementationClass = "buildlogic.plugins.JavaConventionsPlugin"
}
}
}
gradlePlugin {
plugins {
create("javaConventionsPlugin") {
id = "build.java-conventions"
implementationClass = "buildlogic.plugins.JavaConventionsPlugin"
}
}
}
The former uses Gradle's lazy configuration.
This is what I've found so far, not sure if there's a more fluent way to do it:
gradlePlugin {
val javaConventionsPlugion = plugins.register("javaConventionsPlugin")
javaConventionsPlugion.configure {
id = "build.java-conventions"
implementationClass = "buildlogic.plugins.JavaConventionsPlugin"
}
}

How to re-run only failed JUnit test classes using Gradle?

Inspired by this neat TestNG task, and this SO question I thought I'd whip up something quick for re-running of only failed JUnit tests from Gradle.
But after searching around for awhile, I couldn't find anything analogous which was quite as convenient.
I came up with the following, which seems to work pretty well and adds a <testTaskName>Rerun task for each task of type Test in my project.
import static groovy.io.FileType.FILES
import java.nio.file.Files
import java.nio.file.Paths
// And add a task for each test task to rerun just the failing tests
subprojects {
afterEvaluate { subproject ->
// Need to store tasks in static temp collection, else new tasks will be picked up by live collection leading to StackOverflow
def testTasks = subproject.tasks.withType(Test)
testTasks.each { testTask ->
task "${testTask.name}Rerun"(type: Test) {
group = 'Verification'
description = "Re-run ONLY the failing tests from the previous run of ${testTask.name}."
// Depend on anything the existing test task depended on
dependsOn testTask.dependsOn
// Copy runtime setup from existing test task
testClassesDirs = testTask.testClassesDirs
classpath = testTask.classpath
// Check the output directory for failing tests
File textXMLDir = subproject.file(testTask.reports.junitXml.destination)
logger.info("Scanning: $textXMLDir for failed tests.")
// Find all failed classes
Set<String> allFailedClasses = [] as Set
if (textXMLDir.exists()) {
textXMLDir.eachFileRecurse(FILES) { f ->
// See: http://marxsoftware.blogspot.com/2015/02/determining-file-types-in-java.html
String fileType
try {
fileType = Files.probeContentType(f.toPath())
} catch (IOException e) {
logger.debug("Exception when probing content type of: $f.")
logger.debug(e)
// Couldn't determine this to be an XML file. That's fine, skip this one.
return
}
logger.debug("Filetype of: $f is $fileType.")
if (['text/xml', 'application/xml'].contains(fileType)) {
logger.debug("Found testsuite file: $f.")
def testSuite = new XmlSlurper().parse(f)
def failedTestCases = testSuite.testcase.findAll { testCase ->
testCase.children().find { it.name() == 'failure' }
}
if (!failedTestCases.isEmpty()) {
logger.info("Found failures in file: $f.")
failedTestCases.each { failedTestCase ->
def className = failedTestCase['#classname']
logger.info("Failure: $className")
allFailedClasses << className.toString()
}
}
}
}
}
if (!allFailedClasses.isEmpty()) {
// Re-run all tests in any class with any failures
allFailedClasses.each { c ->
def testPath = c.replaceAll('\\.', '/') + '.class'
include testPath
}
doFirst {
logger.warn('Re-running the following tests:')
allFailedClasses.each { c ->
logger.warn(c)
}
}
}
outputs.upToDateWhen { false } // Always attempt to re-run failing tests
// Only re-run if there were any failing tests, else just print warning
onlyIf {
def shouldRun = !allFailedClasses.isEmpty()
if (!shouldRun) {
logger.warn("No failed tests found for previous run of task: ${subproject.path}:${testTask.name}.")
}
return shouldRun
}
}
}
}
}
Is there any easier way to do this from Gradle? Is there any way to get JUnit to output a consolidated list of failures somehow so I don't have to slurp the XML reports?
I'm using JUnit 4.12 and Gradle 4.5.
Here is one way to do it. The full file will be listed at the end, and is available here.
Part one is to write a small file (called failures) for every failed test:
test {
// `failures` is defined elsewhere, see below
afterTest { desc, result ->
if ("FAILURE" == result.resultType as String) {
failures.withWriterAppend {
it.write("${desc.className},${desc.name}\n")
}
}
}
}
In part two, we use a test filter (doc here) to restrict the tests to any that are present in the failures file:
def failures = new File("${projectDir}/failures.log")
def failedTests = []
if (failures.exists()) {
failures.eachLine { line ->
def tokens = line.split(",")
failedTests << tokens[0]
}
}
failures.delete()
test {
filter {
failedTests.each {
includeTestsMatching "${it}"
}
}
// ...
}
The full file is:
apply plugin: 'java'
repositories {
jcenter()
}
dependencies {
testCompile('junit:junit:4.12')
}
def failures = new File("${projectDir}/failures.log")
def failedTests = []
if (failures.exists()) {
failures.eachLine { line ->
def tokens = line.split(",")
failedTests << tokens[0]
}
}
failures.delete()
test {
filter {
failedTests.each {
includeTestsMatching "${it}"
}
}
afterTest { desc, result ->
if ("FAILURE" == result.resultType as String) {
failures.withWriterAppend {
it.write("${desc.className},${desc.name}\n")
}
}
}
}
The Test Retry Gradle plugin is designed to do exactly this. It will rerun each failed test a certain number of times, with the option of failing the build if too many failures have occurred overall.
plugins {
id 'org.gradle.test-retry' version '1.2.1'
}
test {
retry {
maxRetries = 3
maxFailures = 20 // Optional attribute
}
}

How to add new sourceset with gradle kotlin-dsl

I want to add a sourceset src/gen/java. With groovy this is rather easy and already described in https://discuss.gradle.org/t/how-to-use-gradle-with-generated-sources/9401/5
sourceSets {
gen {
java.srcDir "src/gen/java"
}
}
But I stuck with the kotlin-dsl to add a new one. All I've got is:
java {
sourceSets {
}
}
Can anyone help here to
The answer of #s1m0nw1 is correct to add a new sourceset.
But to just add a new source-folder in an existing sourceset, this can be used:
java.sourceSets["main"].java {
srcDir("src/gen/java")
}
You should try the following:
java.sourceSets.create("src/gen/java")
Hope it's what you need!
Worked for me on Gradle 4.10.2:
sourceSets.getByName("main") {
java.srcDir("src/main/java")
java.srcDir("src/main/kotlin")
}
sourceSets.getByName("test") {
java.srcDir("src/test/java")
java.srcDir("src/test/kotlin")
}
The codes above can also be used in subprojects block.
Worked for me on Gradle 4.10.2:
sourceSets.create("integrationTest") {
java.srcDir("src/integrationTest/java")
java.srcDir("build/generated/source/apt/integrationTest")
resources.srcDir("src/integrationTest/resources")
}
kotlin-dsl
sourceSets {
this.getByName("androidTest"){
//Adds the given source directory to this set.
this.java.srcDir("src/mock/java")
}
this.getByName("test"){
this.java.srcDir("src/mock/java")
}
}
I wanted to add a source set with the name "test-integration" and the source directory src/test-integration/kotlin. I was able to accomplish that by combining the two pre-existing answers:
java.sourceSets.create("test-integration").java {
srcDir("src/test-integration/kotlin")
}
As of Gradle 7.5:
sourceSets {
main {
java.sourceSets {
create("gen"){
java.srcDir("src/gen/java")
}
}
}
}
This is what I had before:
main.kotlin.srcDirs = main.java.srcDirs = ['src']
test.kotlin.srcDirs = test.java.srcDirs = ['test']
main.resources.srcDirs = ['resources']
test.resources.srcDirs = ['testresources']
Above now translates to:
sourceSets {
main {
java {
srcDirs("src")
}
resources {
srcDirs("resources")
}
}
test {
java {
srcDirs("test")
}
resources {
srcDirs("testresources")
}
}}

How do I get gradle incremental native c++ compilation to handle generated headers?

I have a header that is generated at build time in a task that I make the compile task depend on, trouble is that the compilation task doesn't recognize when the header is changed in the incremental compilation. So even if the task is run it will not compile the source file.
I would have expected that the generated header should show up as a "Discovered include" and cause the source files to be rebuilt but not so.
Below is the best I have come up with but it does not work.
apply plugin: 'cpp'
model {
binaries {
all {
cppCompiler.args "-I$buildDir/gen"
}
}
components {
test(NativeExecutableSpec) {
sources {
cpp {
source {
srcDir "."
include "*.cpp"
}
}
}
}
}
}
task generateHeader(type: Copy) {
into "$buildDir/gen"
from(rootProject.file('template.h')) {
rename(/template/, 'generated')
expand([text: 'foo'])
}
}
tasks.all { task ->
def match = task.name =~ /^compile.*Cpp$/
if (match) {
task.dependsOn generateHeader
task.inputs.files project.fileTree(dir: "$buildDir/gen").matching {
include '*.h'
}
}
}
Sample project: https://github.com/thejk/gradle-incremental-cpp-generated-header
Got help on the gradle forum, long story short: create a C(pp)SourceSet with generatedBy tasks.generateHeader and modify generateHeader to export headerDir and sourceDir properties. Link the source set to the real one with lib.
sources {
headers(CppSourceSet) {
generatedBy tasks.generateHeader
}
cpp {
source {
srcDir "."
include "*.cpp"
}
lib sources.headers
}
}
task generateHeader(type: HeaderGenerator) {
template rootProject.file('template.h')
headerDir project.file("$buildDir/gen")
}
class HeaderGenerator extends DefaultTask {
#InputFile
File template
#OutputDirectory
File headerDir
#OutputDirectory
#Optional
File sourceDir
#TaskAction
void generate() {
project.copy {
into headerDir
from(template) {
rename(/template/, 'generated')
expand([text: 'foo'])
}
}
}
}

Resources