2

I want to call the help:effective-pom mojo directly from java/scala.

Is there a minimal example how to setup the maven-runtime and call the mojo?

I assume some context is needed before calling the stuff as supposed in the answer to this question:

Reading POM's with its children

e.g.

http://svn.apache.org/viewvc/maven/plugins/tags/maven-help-plugin-2.1.1/src/main/java/org/apache/maven/plugins/help/EffectivePomMojo.java?view=markup

Specifically, the effective-pom mojo writes some ugly prefix and suffix to the xml which is not needed and I want to work directly on the xml later on.

Community
  • 1
  • 1
Bastl
  • 2,926
  • 5
  • 27
  • 48

2 Answers2

0

Hm. If i check the contents it has no garbage. The output will result into a single file. If you like to read the pom you should take a look at aether lib, but the problem is that can read the pom with it but you would like execute a mojo instead which needs to build a Maven environment around which is very complex. Take a look here to get an impression how it could look like.

After some time I found this which looks more the thing you like to use.

khmarbaise
  • 92,914
  • 28
  • 189
  • 235
  • That involves invoking a second JVM which will slow down things heavily. I have not only one file to parse but a big bunch of them. Further more, the output file contains garbage and possibly multiple files. Question is how to invoke it directly from a java program, without starting a new process. – Bastl May 31 '12 at 07:37
  • Update my answer accordingly. – khmarbaise May 31 '12 at 09:11
0

Use Maven Embedder/MavenCli

Java code

MavenCli cli = new MavenCli();
cli.doMain(new String[]{"help:effective-pom"}, "project_dir", System.out, System.out);

Project configuration

<dependencies>
    <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-embedder</artifactId>
        <version>3.1.1</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.aether</groupId>
        <artifactId>aether-connector-wagon</artifactId>
        <version>0.9.0.M2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.maven.wagon</groupId>
        <artifactId>wagon-http-lightweight</artifactId>
        <version>2.5</version>
    </dependency>
</dependencies>
MariuszS
  • 30,646
  • 12
  • 114
  • 155