Maven fork process with the right class

I am creating a Maven plugin with a rather unique requirement for it to work properly: it must spawn new processes on its own and then wait for these processes to complete.

Although this is pretty trivial to do on the command line, Maven plugins do not run the same way as traditional Java code, and therefore there is no class path. I cannot figure out how to resolve the correct class path inside the plugin so that I can create a new JVM (by calling the Main method of another class inside the plugin).

Using the current MavenProject artifact, I can get the Artifact link for myself (plugin) and get its relative directory inside the local Maven repository:

 Artifact self = null; for (Artifact artifact : project.getPluginArtifacts()) { if ("my-group-id".equals(artifact.getGroupId()) && "my-artifact-id".equals(artifact.getArtifactId())) { self = artifact; break; } } if (self == null) { throw new MojoExecutionException("Could not find representation of this plugin in project."); } for (ArtifactRepository artifactRepository : project.getPluginArtifactRepositories()) { String path = artifactRepository.pathOf(self); if (path != null) { getLog().info("relative path to self: " + path); break; } } 

How can I get a link to all its dependencies (and transitive dependencies) so that I can build the full class path for a new call? I see that self has a dependency filter, but I don't know where to apply it.

Is this the right way to create a new self process inside a plugin? Is there a better way?

+7
source share
2 answers

I found a great article on the differences between dependency resolution on Maven 2 and Maven 3.

Given Artifact , this boils down to the following:

 private Set<Artifact> getDependenciesForArtifact(Artifact artifact) { ArtifactResolutionRequest arr = new ArtifactResolutionRequest() .setArtifact(artifact) .setResolveTransitively(true) .setLocalRepository(local); return repositorySystem.resolve(arr).getArtifacts(); } 

With Set you can build a by calling pathOf on the ArtifactRepository for each element and joining File.pathSeparator .

+3
source

Hm. This is actually not an answer, but some hints. Why do you need such a complicated thing? In addition, I would delve into the maven-surefire-plugin, which can develop jvm for unit tests and can handle the classpath. Alternatively, you can take a look at maven-invoker or the maven-invoker-plugin , which fully supports fork maven. Ah, what I missed. Take a look at the maven-dependency-plugin , which has a specific purpose for creating a class path, where you can take a look at the sources as they create the class path.

+1
source

All Articles