0
votes

I have a command like this:

Ant Command:

<path-to-ant/bin/ant -f build.xml sql -lib <path-to-lib>/lib -Dhome=path-home -Ddev=path-to-dev -DsdkMode=install -DcompileSource.dest=path-to-dest -DOSTEMP=/tmp -D-BIFLocation=path-to-loation -DexecutionDirectory=path-exec-dir

At beggining, we use ProcessBuilder builder = new ProcessBuilder(command); to run this, currentlyric we want to run it with ANT API, so we build it with ant Project. I just build a method like:

// string value after "-f" is the location of buildFile
    File buildFile = new File(antCommand.get((antCommand.indexOf("-f")+ 1)));

    // string value before "-lib" is the target to run
    String traget = antCommand.get((antCommand.indexOf("-lib")- 1));

    Project project = new Project();
    project.setUserProperty("ant.file", buildFile.getAbsolutePath());
    project.init();

    //set logger
    DefaultLogger consoleLogger = new DefaultLogger();
    consoleLogger.setErrorPrintStream(System.err);
    consoleLogger.setOutputPrintStream(System.out);
    consoleLogger.setMessageOutputLevel(Project.MSG_INFO);
    project.addBuildListener(consoleLogger);

    ProjectHelper projectHelper = ProjectHelper.getProjectHelper();
    project.addReference("ant.projectHelper", projectHelper);
    projectHelper.parse(project, buildFile);

    project.executeTarget(traget);

But I do NOT know how to pass the parameter from the command line that starts with -D using Java.

Anyone have ideas or such experience? Thank you very much.

1

1 Answers

0
votes

Add it as a system property in the build.xml target

<sysproperty key="sdkMode" value="sdkMode" />

...then grab it in the code

String sdkMode = System.getProperty("sdkMode");

...and of course specify it on the command line

 ant <run-target> -DsdkMode=TEST