0

I'm using Maven Embedder followingly:

MavenCli cli = new MavenCli();
int result = cli.doMain(new String[] { "process-resources" }, "tmp/projectdir", System.out, System.err);

This works, and is equivalent to running command line command

mvn process-resources

in directory

tmp/projectdir

However, I need to specify an option to Maven, so that Maven Embedder performs the equivalent to command line command

mvn -Dstage=local process-resources

How can this be done?

simon
  • 12,666
  • 26
  • 78
  • 113
  • Why are you using the maven embedder and not Maven itself ? – khmarbaise Mar 30 '16 at 13:48
  • I'm using Maven Embedder for processing external configuration files at application startup. – simon Mar 30 '16 at 13:53
  • 1
    Using a build tool for configuration files? To be honest...sounds strange... – khmarbaise Mar 30 '16 at 13:56
  • Maven resources plugin does exactly that. ;) I know it may sound strange, but in this use case it is good (albeit unorthodox) solution. As maven is processing the configuration files anyway at build time with the maven resources plugin, creating a solution for externalizing the configuration files was easily doable this way. – simon Mar 30 '16 at 14:02
  • Why not packaging appropriate package which already contain the correct filtered etc. files...no need for embedder....and separated build and run time... – khmarbaise Mar 30 '16 at 14:28
  • Because 1) when clients have requirements they have to be fulfilled (in this case the requirement is to have the configuration files in an external location) 2) it is cumbersome to have to make a build each time configuration changes (especially in larger organizations) – simon Mar 30 '16 at 14:38

1 Answers1

0

-Dstage=local is a system variable that is set by just giving it as the argument parameter to doMain():

MavenCli cli = new MavenCli();
int result = cli.doMain(new String[] { "-Dstage=local", "process-resources" }, "tmp/projectdir", System.out, System.err);
Tunaki
  • 132,869
  • 46
  • 340
  • 423
  • Thanks, that works. :) I should have thought of it myself, but didn't think of the problem that way (the problem was not "adding a command line option" but "adding a system variable"). It works solving the problem for me, but does not answer the question suggested by the question title directly, which may be of interest for other people. I upvoted you, but apparently somebody else downvoted you. – simon Mar 30 '16 at 13:51
  • @simon Actually, I may know the reason of the downvote: no need to set explicitely the system properties, it does it on its own. Just pass the argument as you normally would. – Tunaki Mar 30 '16 at 14:03
  • Ok, thanks, that works. Apparently the string array is passed to the maven command exactly as it would be on the command line. – simon Mar 30 '16 at 14:08