简体   繁体   中英

Groovy closure return value independent of code structure

I have some code in build.gradle

test {
    doFirst {
        def profile = System.getenv("...")
        if (profile == "dev") {
            println "1: if start"
            // ...
            println "2: if end"
        }
    }
}

and last line ("2: if end") executing anyway, even if profile not "dev"

Looks like groovy don't care about code structure: it simple return last line as result of closure

because if I modify code to:

test {
    doFirst {
        def profile = System.getenv("...")
        if (profile == "dev") {
            println "1: if start"
            // ...
            println "2: if end"
        }
        println "3: after if"
    }
}

Then, this way, if profile not "dev", then all ok - after checking statement groovy execute line with "3: after if"
Is this bug or feature? :)

是的,正如所评论的,这仅在调试器中-groovy可以正常工作

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM