'How can I specify the directory where to create the project for archetype:generate?

I am using mvn archetype:generate -B ... to generate a maven project.

It places the generated project in the current working directory.

Can this be customized to place the generated project in a directory I specify? I don't see any options to do so in the command line --help menu.



Solution 1:[1]

You cannot specify a directory for archetype:generate, this plugin always targets the current working directory.

The Maven Archetype docs suggest using the basedir parameter. Something like this perhaps:

mvn archetype:generate 
    -DgroupId=foo 
    -DartifactId=bar 
    -Dbasedir=/some/other/directory

But, unfortuntately that doesn't work, even with that parameter specified the archetype is generated into the current working directory. Looking at the Maven Archetype JIRA I can see that there is an open issue for this:

https://issues.apache.org/jira/browse/ARCHETYPE-311

This issue has been open since April 2010 and it has been raised against version 2.2.0 of the archetype plugin and I have just verified that this issue still exists with in latest version of the archetype plugin, the following command completed but created bar in the current working directory rather than in /some/other/path ...

mvn org.apache.maven.plugins:maven-archetype-plugin:3.0.1:generate
    -DarchetypeArtifactId=maven-archetype-quickstart
    -DinteractiveMode=false 
    -DgroupId=bar 
    -DartifactId=foo 
    -Dbasedir=/some/other/directory

So, if you want to use archetype:generate and you want the generated project to exist somewhere else then I think you might have to write a simple script which ...

  • Invokes the plugin
  • Moves the created directory to your desired location once the plugin has finished running

Solution 2:[2]

-Dbasedir doesn't work.

You could specify output directory by passing -DoutputDirectory=/some/other/directory

Here's the documentation https://maven.apache.org/archetype/maven-archetype-plugin/generate-mojo.html

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 glytching
Solution 2 Vinod Pillai