Search code examples
javagitmavenjenkinsmaven-archetype

Using Maven Archetype Generate in the same Directory


Is there a way to run mvn archetype:generate and target the current directory instead of creating a directory from the artifactId? The plugin supposedly takes a basedir parameter, but passing in "-Dbasedir=." doesn't do the trick.

For additional context, I've got 2 Git repositories setup. The first contains the source of a Maven archetype for generating custom web services, and the second is a sample service generated from the archetype. I've got a Jenkins job that builds my archetype by essentially just running "mvn clean install". I'm trying to setup a second Jenkins job as part of our CI workflow generates a test service using "mvn archetype:generate", builds the service with mvn clean install, spins up the service, runs some integration tests, and then pushes the source for the test service into a second repository if the tests pass. Both Jenkins jobs use the Maven 2/3 build job type, and I've specified our Git repo information in the SCM section of the job configuration, so the jobs start by doing a "git clone".

For the second job, my current workflow looks like this:

// Clean out the existing source and commit locally.
git rm -r . 
git commit -m "Cleaning out previous version." .

// Generate the new source from the archetype.
mvn archetype:generate ...

// Big hack that I'd like to remove.
mv <artifactId>/* .
rm -rf <artifactId>

// Add the new generated source and commit locally.
git add -A .
git commit -m "Committing new version." .

// Build and test.
mvn integration-test

// Assuming that passed, commit the changes.
git push origin master

The hack is there because I'm not sure how to tell the archetype plugin to use my current directory instead of creating a nested directory with the name of the artifactId, so I have to move everything back into my local repository root directory and delete the directory that was created. Not the end of the world, but it feels ugly. Note that I'm open to suggestions on how to better accomplish my overall goal in addition to answers to my initial question. :) Thanks in advance!


Solution

  • It is not possible without some kind of workaround, because of 2 reasons:

    1. The basedir property doesn't work, see this issue.

    2. The archetype plugin always adds artifactId as the last child directory. You can see this in the sources.

    Your options

    • keep your hack, it is not so horrible ;-)

    • is it important that the project is in the root of the repository? if not just do cd <artifactId> before the build/integration tests

    • if building from root of the repository is required then use root pom.xml of type pom, with the generated project as child module (I think this is what other post suggests as #3)

    • provide a PR to add desired functionality to maven archetype plugin, this might take some time, but is probably the cleanest solution