DoTaL

My repository for development tidbits, and a few personal anecdotes

Creating a new Maven archetype

Posted by doubleA on April 7, 2008

The latest version of the Maven archetype plugin allows you to create an archetype from an existing project. The alpha-3 version is required if you are creating an archetype from a multi module project. This is very important and it requires you to configure some additional repositories in order to download the alpha-3 version.

Below are the steps and issues I ran into:

Configuration
1. Edit settings.xml to add new repositories. Since archetype is a plugin and a core maven feature, you have to setup a repository and plugin repository.

Repository Configuration – add this under the repositories tag:

  1. <repository>

  2. 	<id>snapshots</id>

  3. 	<url>http://people.apache.org/repo/m2-snapshot-repository/</url>

  4. 	<!-- The releases element here is due to an issue in Maven 2.0 that will be

  5. 	fixed in future releases. This should be able to be disabled altogether. -->

  6. 	<releases>

  7. 		<updatePolicy>always</updatePolicy>

  8. 	</releases>

  9. 	<snapshots>

  10. 		<updatePolicy>always</updatePolicy>

  11. 	</snapshots>

  12. </repository>

Plugin repository Configuration – add this under the pluginRepositories tag:

  1. <pluginRepository>

  2. 	<id>snapshots</id>

  3. 	<url>http://people.apache.org/repo/m2-snapshot-repository/</url>

  4. 	<!-- The releases element here is due to an issue in Maven 2.0 that will be

  5. 	fixed in future releases. This should be able to be disabled altogether. -->

  6. 	<releases>

  7. 		<updatePolicy>always</updatePolicy>

  8. 	</releases>

  9. 	<snapshots>

  10. 		<updatePolicy>always</updatePolicy>

  11. 	</snapshots>

  12. </pluginRepository>   

See http://maven.apache.org/guides/development/guide-testing-development-plugins.html for more details on setting up snapshot repos

2. Open a command prompt and go to the root project folder of the parent project that will be used to build the archetype. Run “mvn -U archetype:create-from-project”
3. The alpha-3 version will download
4. Switch to the target/generated-sources/archetype directory
5. Run “mvn install” to put the archetype in the local repo
6. Comment out the plugin repo so that updates are not always downloaded
7. From another directory, run “mvn archetype:generate -DarchetypeCatalog=local”

Issues
I did run into several issues when creating an archetype from one of my projects. There were no major issues, but one of them does need some further research.

1. If you have ant tasks defined in your pom.xml, the archetype will create successfully, but no new projects can be created from it. The error mentions that you can’t have certain characters in the CDATA of the pom. For me, I had to completely remove the ant tasks from my project. But, I’ve been thinking of doing this anyway because I use the ant tasks to control my runtime environment settings. A better approach for this would be to use profiles, so now I kind of have to look at profiles to work around this issue.

2. I also found an issue with ### symbols in log statements and println statements. The workaround was to replace any ### chars with — and then everything worked.

3. The project that is created from the archetype has submodules named “__rootArtifactId__-web” and “__rootArtifactId__-core” instead of “MyNewApp-core” and “MyNewApp-web”. It’s only the folder name that has the wrong name. The poms all references MyNewApp-web as the module name. A folder rename solves this issue.

2 Responses to “Creating a new Maven archetype”

  1. gentile said

    How is it that you go about renaming the folder during the “mvn archetype:generate -DarchetypeCatalog=local” step – what are you adding to your archetype to accomplish this? I also need to rename files to correspond with custom properties entered in during the archetype:generate process.

    Thanks!

  2. doubleA said

    Gentile, sorry if I wasn’t clear on the folder rename. I actually did a manual rename after the acrhetype:generate finished. I think this could be a bug in the alpha3 version of the archetype plugin and it may go away in a later release.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>