Home > Software design >  How to get VM options in application when running gradle?
How to get VM options in application when running gradle?

Time:10-12

I run gradle task:

gradle test -DtestProfile=dockerTest

When I trying to get this variable from application:

System.getProperty("testProfile")

I get null.

How to correctly get VM options in application?

Some explains.

I have some profiles for running tests in different environments. And different properties for each profile, looking like:

application-local.yml
application-test.yml
application-dockerTest.yml
...

Each environment should start their own tests depends of its profile. Than, I wrote resolver:

class TestActiveProfilesResolver : ActiveProfilesResolver {

    override fun resolve(testClass: Class<*>): Array<String> =
        System.getProperty("testProfile")
}

to use it here:

@ActiveProfiles(resolver = TestActiveProfilesResolver::class)

And I want to send 'testProfile' variable when gradle test starts, with VM variable -DtestProfile=dockerTest, for example.

But System.getProperty("testProfile") == null.

CodePudding user response:

There's two JVM's to consider here

  1. Gradle's JVM
  2. The Application's JVM

When you pass a "-D" parameter to Gradle you are setting a system property in Gradle's JVM which is not the same JVM that runs your application

You haven't said how you are running your application yet.

  • Are you running the application in a test case?
  • Are you running it via the "run" task from the application plugin?

If you wanted to set the property during tests you'd do something like

test {
   systemProperty 'testProfile', 'dockerTest'
}

If you wanted to set the system property when running your application via the "application" plugin you'd do

apply plugin: 'application'

run {
   systemProperty 'testProfile', 'dockerTest'
}
  • Related