Tyson
Tyson

Reputation: 1715

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!

Upvotes: 5

Views: 3640

Answers (4)

Jurgen Voorneveld
Jurgen Voorneveld

Reputation: 279

I don't have the reputation to comment so I'll have to make an answer instead. It's not a good idea to have these lines in your script:

mv <artifactId>/* .
rm -rf <artifactId>

If someone decides to fill in an empty (or just '/') artifactId you end up trying to move the whole disk into this directory, only to then delete the entire disk. Instead you can do this:

mv ./<artifactId>/* .
rmdir ./<artifactId>

The move can't hurt and the rmdir can only delete empty directories.

Upvotes: 1

Piotr Statuch
Piotr Statuch

Reputation: 51

I know it's old topic but I had similar issue:

The archetype:generate goal has a parameter outputDirectory, you can set it to root folder (../). When generating artifact from archetype it's important that your artifact name is same as current directory.

mvn archetype:generate -DarchetypeGroupId=[archetype-group] -DarchetypeArtifactId=[archetype-id] -DoutputDirectory=../ -DartifactId=[current-folder-name] -DgroupId=[whatever-you-want] -Dversion=[ex: 0.1] -Dpackage=[jar|pom|etc.] -B

Upvotes: 4

František Hartman
František Hartman

Reputation: 15086

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

Upvotes: 3

Aaron Digulla
Aaron Digulla

Reputation: 328644

I have three ideas:

  1. You can try to use an absolute path for basedir but I think the property is overwritten by Maven when it sees a pom.xml file.

  2. Use a script and pushd .. to change to the parent folder before you run mvn archetype:generate. popd will get you back.

  3. Use a maven module build. That way, you have a root POM which builds the archetype and you can run mvn archetype:generate to generate a module inside of the project:

     demo-project/
         pom.xml
         archetype/
             pom.xml
    

Upvotes: 1

Related Questions