'Maven generate achetype in existed directory

I'm trying to create empty Maven Web Project with an existing dir (actually from github project, it's empty and contains only README file).

But maven seems to detect directory existence and failed with error:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-archetype-plugin:2.2:generate (default-cli) on project standalone-pom: Directory loms already exists - please run from a clean directory -> [Help 1]

Any chance to force maven use an existing directory?

Invocation command:

mvn archetype:generate -DgroupId=org.reaver.devs -DartifactId=loms -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false


Solution 1:[1]

Since the directory is already existing, maven won't be able to generate another artifact project with same name. And since your loms directory is a git project and it is not acceptable to delete it, you can follow a tricky way:

  • Generate a project with your desired archetype but with diferrent artifact name (just will serve as a swap directory):

    mvn archetype:generate -DgroupId=org.reaver.devs \
                           -DartifactId=loms-2 \
                           -DarchetypeGroupId=org.apache.maven.archetypes \
                           -DarchetypeArtifactId=maven-archetype-webapp \
                           -DinteractiveMode=false
    
  • The above command will generate a new directory with maven nature, and it absolutely fits in what you are expecting for your loms project, expect the <artifactId> in your pom.xml which must be updated to loms (note that I will be using command lines but you may use any visual tools to update the file):

    vi pom.xml
    
  • Remove the -2 suffixe in the artifactId so it matched loms.

  • Copy all files/folders under loms-2/ directory to your project directory loms/.

  • Now you have your project initialized.

Solution 2:[2]

Today I tried to do the same. I found that the following works for my use case, where a) the remote repository is always created with a main branch containing only a basic README.md, and b) we use PRs for everything, even the first actual commit.

After using the archetype to create the folder, I executed the following commands in the newly created folder:

git init
git remote add origin <url>
git fetch origin
git branch -m feature/setup
git add .
git commit -m "Initial setup"
git rebase -Xtheirs origin/main
  • The first two commands are straightforward, and are used when pushing to an empty remote repository.
  • The fetch retrieves a reference to origin/main.
  • The renaming is perhaps unnecessary for you, if master is a valid name. I don't know how this works if the remote branch is also called master.
  • The add and commit are also straightforward.
  • The rebase resolves conflicts in favour of the newly created branch, in other words what the archetype created.

After this I can add more commits, or push and let the PR be completed as-is.

Solution 3:[3]

The quickstart archetype works fine for this. It gives a warning but then creates the pom.xml and the maven src/ directory structure inside an existing directory (i.e. the same as the artifactId).

It must be run from the PARENT directory.

mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false

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 tmarwen
Solution 2 Rob Spoor
Solution 3 egerardus