Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 625
Tyson Avatar asked Jun 25 '14 03:06

Tyson


People also ask

What is the use of archetype generate in Maven?

Sponsoring Apache Thanks Follow ASFMavenProject archetype:generate Full name: org.apache.maven.plugins:maven-archetype-plugin:3.2.1:generate Description: Generates a new project from an archetype, or updates the actual project if using a partial archetype.

How to create a new project based on an archetype?

To create a new project based on an Archetype, you need to call mvn archetype:generate goal, like the following: Please refer to Archetype Plugin page. Maven provides several Archetype artifacts: An archetype to generate a sample archetype project.

How do I create a new project using the Maven SDK?

To create a new application project or smart library project, use the Maven archetypes (simple archetype or lib archetype) with the BMC Helix Platform SDK. For your projects, decide where you want your project code to reside.

What is the Maven-archetype-plugin?

The maven-archetype-plugin allows the user to create a Maven project through the generate goal and existing archetype. For more information on this plugin, you can visit the homepage.


Video Answer


2 Answers

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

like image 189
František Hartman Avatar answered Oct 16 '22 03:10

František Hartman


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
like image 26
Piotr Statuch Avatar answered Oct 16 '22 04:10

Piotr Statuch