0

I am trying to build a project to checkout code from SCM and build it using maven. Is there a way we can achieve running maven goals from java without a maven home being provided(no maven installation). For SVN, I am using svnkit to achieve the same. For maven, I tried using the below 2 options.

http://maven.apache.org/ref/3.0-beta-3/maven-embedder/ Can anyone give a good example of using org.apache.maven.cli.MavenCli programattically?

For both the above options, I need to have a maven installation. Any suggestions?

Community
  • 1
  • 1
Upen
  • 1,388
  • 1
  • 22
  • 49

1 Answers1

1

No, just include the jar in your project and the following, to compile code:

MavenCli cli = new MavenCli();
int result = cli.doMain(new String[]{"compile"},
        "workspace/MiscMaven",
        System.out, System.out);
System.out.println("result: " + result);

Per a comment, here's how you add the dependency to your project. This does not install maven on your system, merely includes the JARs in your project build:

<dependency>
    <groupId>org.apache.maven</groupId>
    <artifactId>maven-embedder</artifactId>
    <version>3.3.3</version>
</dependency>
<dependency>
    <groupId>org.eclipse.aether</groupId>
    <artifactId>aether-connector-basic</artifactId>
    <version>1.0.2.v20150114</version>
</dependency>
<dependency>
    <groupId>org.eclipse.aether</groupId>
    <artifactId>aether-transport-wagon</artifactId>
    <version>1.0.2.v20150114</version>
</dependency>
<dependency>
    <groupId>org.apache.maven.wagon</groupId>
    <artifactId>wagon-http</artifactId>
    <version>2.9</version>
</dependency>
<dependency>
    <groupId>org.apache.maven.wagon</groupId>
    <artifactId>wagon-provider-api</artifactId>
    <version>2.9</version>
</dependency>
<dependency>
    <groupId>org.apache.maven.wagon</groupId>
    <artifactId>wagon-http-lightweight</artifactId>
    <version>2.9</version>
</dependency> 
hd1
  • 33,938
  • 5
  • 80
  • 91
  • Hi hd1. MavenCli would require a maven installation to run rite?. I tried running the above code and got the below error. Exception in thread Caused by: java.lang.ClassNotFoundException: org.codehaus.plexus.util.xml.pull.EntityReplacementMap at java.net.URLClassLoader$1.run(URLClassLoader.java:202) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:190) at java.lang.ClassLoader.loadClass(ClassLoader.java:306) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) – Upen Jul 07 '15 at 22:47
  • Also have the below in my pom file.' org.apache.maven maven-embedder 3.1.1 org.eclipse.aether aether-connector-wagon 0.9.0.M2 org.apache.maven.wagon wagon-http-lightweight 2.5 ' – Upen Jul 07 '15 at 22:49