Hi ereryone,
I'm using Adaptavist ScriptRunner 5.3.6 for JIRA Server. I'm trying to setup up a couple of scripts, which need some configuration data, like JDBC URLs, usernames and passwords, which I don't want to hardwire into the scripts.
Where shall I store these data?
There are a two options imho:
import javax.naming.InitialContext
def ic = new InitialContext()
// javax.naming.NoInitialContextException: Cannot instantiate class: org.apache.naming.java.javaURLContextFactory
Also, how would I tell JNDI the usernames etc.?
Properties properties = new Properties()
File propertiesFile = new File('/absolute/path/to/script/root/test.properties')
propertiesFile.withInputStream {
properties.load(it)
}
log.info properties['test.message'] // works
But I don't like the absolute path. Where should I locate the .properties file? I would guess in my <script_root>, but just using the test.properties without the path won't find the file.
Is there a way to get the script root's absolute path? Using System.getenv() or System.getProperties() does not yield the application_data path.
Thanks for your help!
Arnold
If you want the script root's absolute path then try something like
import com.onresolve.scriptrunner.runner.ScriptRunner
import com.onresolve.scriptrunner.runner.ScriptRunnerImpl
def scriptRoots = ScriptRunnerImpl.getPluginComponent(ScriptRunner).getRootsForDisplay()?.split(", ")?.toList()
Keep in mind that may have been more than one script roots configured.
Hey Thanos,
this works fine for me. That way I can rely on a known file structure beneath (one of) my script root(s).
Thank you!
Arnold
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You can also store your properties in sal. You can find more info here:
https://developer.atlassian.com/server/framework/atlassian-sdk/storing-plugin-settings/
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
That looks interesting. I think this will be the orthodox solution for a grown-up plugin. Thanks for the link!
Arnold
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Ok thank you. Alexey, I had a look at SAL an I'll explore that, when and if we need a grown-up plugin. For my scripting I'll go with a .properties file.
This is how I've done it:
1. Placed myproject.properties Under <script_root> :
db.driverName = oracle.jdbc.OracleDriver
db.url = jdbc:oracle:thin:@//bla:123/blubb
db.user = myuser
db.password = secret
2. Created an accessor class in <script_root>/mycompany/MyCompanyProperties.groovy:
package mycompany
import com.onresolve.scriptrunner.runner.ScriptRunner
import com.onresolve.scriptrunner.runner.ScriptRunnerImpl
import java.nio.file.Files
import java.io.File
import groovy.util.logging.Log4j
@Singleton
@Log4j
public class MyCompanyProperties {
final String configFileName = 'myproject.properties'
public Properties getProperties() {
def scriptRoots = ScriptRunnerImpl.getPluginComponent(ScriptRunner).getRootsForDisplay()?.split(", ")?.toList()
//def scriptRoots = ['a', 'b']
File propertiesFile = null
for (root in scriptRoots) {
propertiesFile = new File("$root/$configFileName")
if (Files.isReadable(propertiesFile.toPath())) {
log.info "Found ${propertiesFile.toPath()}"
break
}
}
// TODO: what if propertiesFile == null?
Properties properties = new Properties()
propertiesFile.withInputStream {
properties.load(it)
}
properties
}
}
3. Use that class each time to access any configuration data in my scripts:
import mycompany.MyCompanyProperties
def props = MyCompanyProperties.instance.properties
def driverName = props['db.driverName'];
def url = props['db.url'];
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi, Arnold,
Have you been successfully able to roll this out?
We are using Data center version and didn't find the option to upload the file with ".properties" files. Only ".groovy" files are allowed to upload for the configuration.
We are trying to figure out how are you uploading ".properties" files? Our requirements are close to yours.
Any help will be appreciated.
Thanks
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello Ankit,
we're using the Server variant of JIRA (now discontinued to my knowledge). So there I could simply ssh into the server and upload the .properties.
I have not looked into DataCenter.
Regards
Arnold
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.