Jump to content
Excelsior Forums
AndyDenby

Load external configuration file

Recommended Posts

 

am using the Excelsior Jet Gradle plug-in to secure our java web application based on Ratpack. Using the excelsiorJet block below I have successfully compiled and ran the application using gradle jetTestRun.

excelsiorJet {

    //UNCOMMENT THIS ONCE YOU HAVE RUN GRADLE TEST RUN
    globalOptimizer = true

    mainClass = 'ratpack.groovy.GroovyRatpackMain'

    groupId = 'com.reshufl'
    version = '1.0.1'

    //jvmArgs "-Xms256m", "-Xmx512m", "-Djava.class.path=./config/"
    jvmArgs "-Xms256m", "-Xmx512m"

    stackAllocation = true
    inlineExpansion = 'aggressive'

    compilerOptions = ["-Djet.gc.ratio=11", "-Djet.gc.no.swap",  "-Djava.class.path=./config/application.properties"]
    //compilerOptions = ["-Djet.gc.ratio=11", "-Djet.gc.no.swap"]

    optimizationPreset = 'smart'

    dependencies {
        dependency {
            groupId = 'com.reshufl'
            isLibrary = false
        }
        //dependency {
        //    path = new File(project.projectDir, "src/ratpack/application.properties")
        //    packagePath = 'config'
        //    pack = 'none'
        //}
    }

    packageFiles {
        file {
            path = new File(project.projectDir, "src/ratpack/application.properties")
            packagePath = "config"
        }
    }

    runtime {
        flavor = 'server'
        components = ['jce']
    }
}

On startup Ratpack is configured to load the configuration file "src/ratpack/application.properties" that is currently being built into the deployment package.

I am trying to externalize this configuration file so that it is read from a config directory created by the installer so that the end user can modify the application properties on start-up. So far I managed to create a copy of the file under the config directory however I've been unable to modify the classpath to read this version of the file first.

I've looked at the documentation but just don't seem to be able to get this going. Please could you point me in the right direction for how to do this using the gradle plug in. 

Share this post


Link to post
Share on other sites
Managed to get this going with the following excelsiorJet block. Key points in bold. 

1. Add the classpath to the jvmArgs
2. Add the dependency block to copy the config files from src/ratpack to the config/ratpack folder under app

excelsiorJet {

    // UNCOMMENT THIS ONCE YOU HAVE RUN GRADLE TEST RUN
    globalOptimizer = true

    // ONCE YOU HAVE EVERYTHING ELSE WORKING TRY...
    // protectData = true

    mainClass = 'ratpack.groovy.GroovyRatpackMain'

    groupId = 'com.group'
    version = '1.0.1'
    artifactName = 'myartifact'

    jvmArgs "-Djava.class.path=\$(Root)/config/ratpack", "-Xms256m", "-Xmx512m"

    stackAllocation = true
    inlineExpansion = 'aggressive'
    optimizationPreset = 'smart'

    compilerOptions = ["-Djet.gc.ratio=11", "-Djet.gc.no.swap"]
    dependencies {
        dependency {
            groupId = 'com.reshufl'
            isLibrary = false
        }
        dependency {
            path = new File(project.projectDir, "src/ratpack/")
            packagePath = 'config'
            pack = 'none'
            protect='not-required'
            optimize='auto-detect'
        }
    }

    runtime {
        flavor = 'server'
        components = ['jce']
    }
}

Issue closed.

Share this post


Link to post
Share on other sites

Just to reiterate what has been discussed with @AndyDenby over email and to provide a public statement on this issue:

It is highly discouraged to manually alter `java.class.path` property. If you will attempt to do it, you must provide ALL dependencies explicitly in that class path.

So for this particular case the suggested workaround is:

1. Remove altering of `java.class.path` from `jvmArgs` configuration. Gradle plugin will handle adding all dependencies into class path on its own.

2. Exclude packaged configs (which you want to change after deployment) from created .jar file using following configuration:


jar {
    exclude('config')
}

Assuming that all packaged config files are located in 'config' directory inside of .jar.

The reason for such workaround is that Gradle plugin currently searches import class path entries before external class path entries, so if you don't exclude configs from .jar, JVM will find them in resources of compiled .jar file instead of your packaged external configs.

There is a ticket in Excelsior JET Gradle plugin's bug tracker to add proper support for modification of `java.class.path` - https://github.com/excelsior-oss/excelsior-jet-gradle-plugin/issues/36

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×