Home > Net >  Passing a value from cmd/runtime using gradle is not getting picked up
Passing a value from cmd/runtime using gradle is not getting picked up

Time:12-26

I have an abstract class that has a value of workingDirectory (variable) I extended this in a new class (BaseClass). I want to override this by providing a value at runtime, and this is not happening. It is taking the default value.

I am using Gradle to run that file, it has test cases

The command in the code (BaseClass)

private static final String workingDirectory = System.getenv("infrastructurePath")==null?"./infrastructure":System.getenv("infrastructurePath");

The command I used to run from cmd

.\gradlew --info :testModule:testInfrastructure -DinfrastructurePath='./infrastructure-localtests'

But, it is taking ./infrastructure every time and not ./infrastructure-localtests when I try to pass the value from the command prompt. Where do I make the change?

CodePudding user response:

I faced a similar situation long back. I was using IntelliJ for the same. Try making the following changes:

  1. In your base class, change the name of your workingDirectory variable. It should not be the same as you have in the abstract class.

  2. Use the new variable name everywhere.

  3. Use System.getproperty("name of the path / or path variable"); Do not use System.getenv() with a ternary operator. It didn't work for me.

  4. Now, go to your build.gradle.

  5. As you told you are running the test cases. you would be having a task created in tasks.register('YourTestName', Test)

  6. Inside that, add a new line of

    systemProperty "YourPath", System.getProperty("YourPath", "./TheDefaultPathIfNoConditionMatches");

Now, at run time, if you simply run the tests, the control will go to the default path. If you want it to go to a new path, add an -D argument like this

-DinfrastructurePath="./WhateverPath"

This should work for you. Make sure to thumbs up if this helps. Thank you.

CodePudding user response:

Maybe rename the directory with an underscore instead of a dash

Or if you don't want to take the risk of renaming the directory (because it might break other things), create an alias directory that points to the old one. On Windows, that's called a hard link.

  • Related