0

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!

spinyBabbler
  • 392
  • 5
  • 19
  • Just out of curiosity, why are you creating maven projects on the fly? And sorry, I don't have an answer for your question. Never even thought of that :) – Jorge Campos Feb 25 '19 at 19:42
  • Quite hard to answer without minimal working example. Does it work without calling invoker? – Robert Scholte Feb 25 '19 at 20:29
  • Have you already created and installed the archetype as explained in [Maven docs](https://maven.apache.org/guides/mini/guide-creating-archetypes.html)? – user944849 Feb 25 '19 at 20:30
  • @JorgeCampos I need it for some tests but don't want to copy all the directories and dependencies that are required to start my project. Let me know if there is a better way. – spinyBabbler Feb 25 '19 at 20:51
  • @RobertScholte creating an archetype through the command line works. – spinyBabbler Feb 25 '19 at 20:52
  • What kind of tests are you talking about? – Jorge Campos Feb 25 '19 at 23:49
  • 1
    @JorgeCampos, we have a fragment that needs to keep on running for us to create new tests as well as test various differences in that fragment. Because there are many variations/permutations, I want to create a project that can be loaded with different configurations before the test start. It might be hard to explain without actually showing but if that makes sense. Anyway, I figured out a solution. Sorry about the late response! – spinyBabbler Mar 06 '19 at 15:58
  • 1
    No problem. Though what you explained seems like you actually need a way to register something on the fly, you don't actually need to create a project you could use the concept of adding plugins to your code base. But since it makes sence to you to do that its ok! cheers – Jorge Campos Mar 06 '19 at 17:05

1 Answers1

1

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.

spinyBabbler
  • 392
  • 5
  • 19