#Inject value on Gradle build

1 messages · Page 1 of 1 (latest)

storm wave
#

Is it possible to inject a value at compile time on a Gradle build task?
For instance, say I have this static variable called name and I pass name as an argument to the Gradle build task. I then want it to inject that value in to the static field.

oblique heraldBOT
#

<@&987246554085740594> please have a look, thanks.

oblique heraldBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

toxic pollen
#

Inject into actual project code?

#

Don't think so unless you're okay with manually parsing your file and replacing text

topaz dove
#

I'm not aware of a tool to directly inject values into source
But you could use java.util.Properties to generate and save name during compile time and then load during runtime

something like this

// ./gradlew someTask -Dname="John"
tasks.named('someTask') {
    doLast {
        def properties = new Properties()
        properties.setProperty('name', getProperty('name'))
        layout.buildDirectory.file("x.properties").withWriter { writer -> properties.write(writer, '') }
    }
}
austere hound
#

android has their R.* convention that works similarly

#

but if you don't want to add a separate kind of build step or load at runtime you could build an annotation processor which generates a source file holding the constant

storm wave