2

I have a project that is calling a maven plugin (open-api-generator) to make a build based on an api specification. I don't want to share the project but I'd like to create a jar that somewhat simulate a "mvn compile" using an internal maven pom.xml.

Is this possible?

Phate
  • 6,066
  • 15
  • 73
  • 138
  • If you have maven installed if course this is possible, cd inside the project folder call: mvn compile and that should be all – William Andrés Bernal Jul 17 '20 at 16:17
  • Perhaps you can use class [`ProcessBuilder`](https://docs.oracle.com/javase/8/docs/api/java/lang/ProcessBuilder.html) to execute `mvn` command? Alternatively, perhaps use the classes in the [`java.util.jar`](https://docs.oracle.com/javase/8/docs/api/java/util/jar/package-summary.html) package to create a JAR file. – Abra Jul 17 '20 at 16:21
  • Use Maven Embedder. – Amit kumar Jul 17 '20 at 16:22
  • 2
    Potential duplicate of https://stackoverflow.com/questions/5141788/how-to-run-maven-from-java – Robert Jul 17 '20 at 16:23

2 Answers2

1

Code :

MavenCli obj = new MavenCli();
obj.doMain(new String[]{"compile"}, "project_dir", System.out, System.out);
P

Add :

  1. maven-embedder
  2. aether-connector-wagon and
  3. wagon-http-lightweight

dependencies in your pom.xml.

Amit kumar
  • 2,169
  • 10
  • 25
  • 36
  • This seems what I'm searching for, I'll try it and mark as correct if it works! But how to make it point to a file inside of the jar itself? – Phate Jul 17 '20 at 16:36
  • Replace `project_dir` with the path to the file. Somewhat like "/src/main/test/MavenFile". – Amit kumar Jul 17 '20 at 16:39
0

Programatically it is possible to call the Shell command. For example with:

ProcessBuilder processBuilder = new ProcessBuilder();
processBuilder.command("bash", "-c", "mvn compile /foo/bar");
Maxi
  • 415
  • 4
  • 24