1

Let's say I want to run Groovy code with Groovy plugin and pass properties to some groovy files. I know you can import properties from Maven build with project.properties. Just didn't figure out how to do it with Gradle build.

2
  • how do you want to run Groovy code? By simply writing it in the build file? Or by running the code in a file? Or during tests?
    – Renato
    May 16, 2016 at 16:55
  • task runScript (dependsOn: 'classes', type: JavaExec) { main = 'code' classpath = sourceSets.main.runtimeClasspath }
    – minas
    May 16, 2016 at 17:00

2 Answers 2

1

Try this:

task runScript(type: JavaExec) {
    main 'Main'
    systemProperty( 'my-prop', 'hello' )
    classpath = sourceSets.main.runtimeClasspath
}

In the groovy script (Main.groovy):

println 'my-prop is ' + System.getProperty( 'my-prop' )

// all properties
println System.properties
3
  • 2
    What do I write in my .groovy file to get this property ? Actually what I really need is getting all properties.
    – minas
    May 16, 2016 at 17:34
  • System.getProperty( 'my-prop' )
    – Renato
    May 16, 2016 at 17:37
  • @minas Edited the answer with more details.
    – Renato
    May 16, 2016 at 17:40
0

You may try to define properties you need in gradle.properties file at the root of your project

myUserProperty=value1
systemProp.system=someValue

and then use it in build.gradle script

task printProperties << {
    println myUserProperty
    println System.properties['system']
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.