George Smith
George Smith

Reputation: 1067

DataSource.groovy classpath

def encryptedUid = getClass().getClassLoader()
                             .getResourceAsStream('user.txt')
                             .getText()

This code in the dataSource.groovy file works fine when I run it in a windows environment, but when I check the code in and Jenkins tries to load DataSource.groovy I get:

Error loading DataSource.groovy: Cannot invoke method getText() on null object.

The user.txt file is in the root of the src/java folder, and I know that it is built into the war file in a windows build. It just doesn't even get to building the war file on the Linux box.

Any ideas?

Upvotes: 0

Views: 362

Answers (1)

Dónal
Dónal

Reputation: 187499

Apparently you're trying to configure the database username/password, but don't want to put them in DataSource.groovy directly for security reasons. Here's how I handle this:

Put the secret configuration in a file /grails-app/conf/secret.properties. The contents of this file are shown below:

dataSource.username=root
dataSource.password=secret
# other secret configuration

Include this file in the grails configuration by adding the following to Config.groovy

grails.config.locations = ["classpath:secret.properties"]

If you want to be able to override the config. in secret.properties on a per-environment basis, change this to

grails.config.locations = [
    "classpath:secret.properties",
    "classpath:secret-${Environment.current}.properties"
]

You can then (optionally) add a file secret-DEVELOPMENT.properties that will override the configuration in secret.properties in the dev environment, and likewise for the other environments.

Of course, in order for this to work, the secret*.properties files must be present when building the war (or executing run-app) and should not be checked into VCS.

You're not restricted to placing these config. files in a relative location to the classpath. You can put them anywhere on the file system by using the file: prefix instead of classpath:. Finally, you can put the secret configuration in a .groovy config. file instead of a .properties file.

Upvotes: 1

Related Questions