javamavenpom.xmlmaven-pluginmaven-invoker-plugin

How to set artifact id and other info programmatically using maven invoker


I'm trying to create a maven project programmatically by running a java application. In the application, I have set the pom file as:

request.setPomFile(new File(thePomFile));
request.setGoals( Collections.singletonList( "archetype:generate" ) );

This is a pom file with archetype info that I want to use since I have my own project structure.

Two questions:

  1. I get an error saying "No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0)"; why is this even though I provided the archetype pom file?
  2. I also get "[WARNING] Property groupId is missing. Add -DgroupId=someValue [WARNING] Property artifactId is missing. Add -DartifactId=someValue [WARNING] Property package is missing. Add -Dpackage=someValue"; how to I supply these information to the request. The api doesn't seem to have it.

I tried looking up the api docs, and other stackoverflow questions but couldn't find anything close to this. Thank you!


Solution

  • I was able to supply the information in point 2, by answering the question in the first place.

    In the request itself, you need to do:

    request.setGoals(Collections.singletonList("archetype:generate -DgroupId=.....");
    

    Then if you look at the api docs, there is a command line builder that I didn't notice before: https://maven.apache.org/shared/maven-invoker/apidocs/index.html

    MavenCommandLineBuilder mavenCmd = new MavenCommandLineBuilder();
    

    Set the necessary configurations like base directory, path to maven executable etc to this mavenCmd.

    Then, you can send your previously created request to it, like:

    Commandline result = mavenCmd.build(request); 
    

    To answer my first point, I ended up removing setting pom itself because I was fetching it remotely and mentioned the archetype in the command I sent to mavenCmd. There might be a better way but this worked for me and I'm good with it.