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 Answers
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
-
2What do I write in my .groovy file to get this property ? Actually what I really need is getting all properties.– minasMay 16, 2016 at 17:34
-
-
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']
}
task runScript (dependsOn: 'classes', type: JavaExec) { main = 'code' classpath = sourceSets.main.runtimeClasspath }