Hey All,
For some post scripts on Jira, I have a set of groovy scripts that I have deployed on the server where jira is located. These groovy scripts are run on the execution of certain transitions. Several locations and other values are hardcoded into the script, but realistically the values should be configuration values. Is there a way to create a configuration file that these groovy scripts will get their values from rather than having to hardcode these values into my scripts and change when I move from development to production?
For example, I have something along the lines of the following:
def url = new URL("http://some_dev_url:29000/")
and in production I will need the following
def url = new URL("http://some_prod_url:19000/")
but I would love to have something like this in both dev and productions
def url = new URL("http://" + config.url + ":" + config.port)
Thanks for the help!
How about externalizing the configuration values to property files and then simply:
Properties properties = new Properties()
File propertiesFile = new File('test.properties')propertiesFile.withInputStream { properties.load(it)
}
def runtimeString = 'a'
assert properties."$runtimeString" == '1'
assert properties.b == '2'
Or you could JVM variables to eliminate the file, but that is more painful from other aspects.
This worked. I had to do some further googling and used what I found here (http://www.baeldung.com/java-properties), but I got it. Thanks
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Tyler,
You can utilise GStrings to cast a variable into a string like so:
def url = new URL("http://${configUrl}:${configPort}".toString())
As for using code from a remote script file check out this article.
Hope this helps.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Yes, you can do that Ivan. I more or less typed pseudo-code and I think you missed the point of the question.
I could very easily do something like
def configUrl = "Blah"
def configPort = 19000
def url = new URL("http://${configUrl}:${configPort}".toString())
But I am still forced to define my variables withing the file which isn't great design. I would prefer to do something along the lines of
def configUrl = configFile.url
def configPort = configFile.port
def url = new URL("http://${configUrl}:${configPort}".toString())
I am mainly a python developer so sorry if my code is rather pythonic.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I see. Well I suppose you could store a file in your Jira home directory, which will contain something like:
www.someurl.com:12345
And then call that file in your script:
def scanner = new Scanner(new File("path to your file"))
String urlFromFile
while (scanner.hasNextLine()){
urlFromFile = scanner.nextLine()
}
scanner.close()
def url = new URL("http://${urlFromFile}".toString())
Naturally, file contents can be more complex but that'll require a bit of parsing to be done within your script.
Sorry if this is not the most elegant solution (I'm not a full fledged developer) but it gets the job done.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
It would work I suppose, but I was hoping for a better way to load a configuration value from a file. Preferably would load from xml or json.
I appreciate the help Ivan.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.