aspose file tools*
The moose likes Ant, Maven and Other Build Tools and the fly likes maven custom archetypes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Engineering » Ant, Maven and Other Build Tools
Bookmark "maven custom archetypes" Watch "maven custom archetypes" New topic
Author

maven custom archetypes

Marin Cedervall
Greenhorn

Joined: Jul 04, 2012
Posts: 2
Hi,
New to this forum so I start by saying hello to all.

I have come across a real weird issue when creating custom maven archetypes that I found no way around.

Problem 1.
This particular project structure is meant to be used for backend database objects where I don't want/need to follow the default maven source structure.
The default source folder structure (src/main/java, src/main/resources, src/site) is not really suitable for me so my first question is if it is possible to change this.
The structure I am looking at consists of a number of database related folders (Constraints, Table, Grants etc etc) where I would like these to be created as individual root folders within the project structure where each folder should have a readMe.txt file.
At the project root I also expect to find the pom.xml and a release-descriptor.xml
Example:


The project structure on my custom archetype does not allow me to deviate from the default src/main/... file structure.
The only way I managed to get this to work is by pointing to the src/main/java/ source alternative the src/main/resources




This works but I don't like the file structure
I also have problems getting the release-descriptor.xml into the project root.
If change the archetype.xml to look like this...
<resource>/Constraints/readMe.txt</resource>
<resource>/DBLinks/readMe.txt</resource>

or
<source>/Constraints/readMe.txt</source>
<source>/DBLinks/readMe.txt</source>

where I also moved the archetype folder structure to the rchetype-resources folder maven complains about not being able to find the source/resource in the src/main/java directory.


Problem 2 (and this is a really weird one)
My archetype.xml have a reference to the resource>src/main/resources/Forms/readMe.txt</resource>
After running the mvn install on the project then executing the mvn archetype:generate against my new custom archetype it comes back with the following error...
D:\Applications>mvn archetype:generate -DarchetypeGroupId=au.gov.wa.commerce.archetypes -DarchetypeArtifactId=quickoradb
-DarchetypeVersion=1.0.0-SNAPSHOT
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom <<<
[INFO]
[INFO] --- maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Interactive mode
[WARNING] Archetype not found in any catalog. Falling back to central repository (http://repo1.maven.org/maven2).
[WARNING] Use -DarchetypeRepository=<your repository> if archetype's repository is elsewhere.
Downloading: http://repo1.maven.org/maven2/au/gov/wa/commerce/archetypes/quickoradb/1.0.0-SNAPSHOT/maven-metadata.xml
Define value for property 'groupId': : au.gov.wa.commerce
Define value for property 'artifactId': : testQ
Define value for property 'version': 1.0-SNAPSHOT: :
Define value for property 'package': au.gov.wa.commerce: :
Confirm properties configuration:
groupId: au.gov.wa.commerce
artifactId: testQ
version: 1.0-SNAPSHOT
package: au.gov.wa.commerce
Y: :
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: quickoradb:1.0.0-SNAPSHOT
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: au.gov.wa.commerce
[INFO] Parameter: packageName, Value: au.gov.wa.commerce
[INFO] Parameter: package, Value: au.gov.wa.commerce
[INFO] Parameter: artifactId, Value: testQ
[INFO] Parameter: basedir, Value: D:\Applications
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[ERROR] ResourceManager.getResource() parse exception
[ERROR] org.apache.velocity.exception.ParseErrorException: Lexical error: org.apache.velocity.runtime.parser.TokenMgrErr
or: Lexical error at line 11, column 2. Encountered: <EOF> after : ""
at org.apache.velocity.Template.process(Template.java:137)
at org.apache.velocity.runtime.resource.ResourceManagerImpl.loadResource(ResourceManagerImpl.java:415)
at org.apache.velocity.runtime.resource.ResourceManagerImpl.getResource(ResourceManagerImpl.java:335)
at org.apache.velocity.runtime.RuntimeInstance.getTemplate(RuntimeInstance.java:1102)
at org.apache.velocity.app.VelocityEngine.mergeTemplate(VelocityEngine.java:498)
at org.apache.maven.archetype.old.DefaultOldArchetype.processTemplate(DefaultOldArchetype.java:847)
at org.apache.maven.archetype.old.DefaultOldArchetype.processTemplate(DefaultOldArchetype.java:717)
at org.apache.maven.archetype.old.DefaultOldArchetype.processResources(DefaultOldArchetype.java:758)
at org.apache.maven.archetype.old.DefaultOldArchetype.processTemplates(DefaultOldArchetype.java:674)
at org.apache.maven.archetype.old.DefaultOldArchetype.createArchetype(DefaultOldArchetype.java:329)
at org.apache.maven.archetype.generator.DefaultArchetypeGenerator.processOldArchetype(DefaultArchetypeGenerator.
java:135)
at org.apache.maven.archetype.generator.DefaultArchetypeGenerator.generateArchetype(DefaultArchetypeGenerator.ja
va:104)
at org.apache.maven.archetype.generator.DefaultArchetypeGenerator.generateArchetype(DefaultArchetypeGenerator.ja
va:143)
at org.apache.maven.archetype.generator.DefaultArchetypeGenerator.generateArchetype(DefaultArchetypeGenerator.ja
va:169)
at org.apache.maven.archetype.DefaultArchetypeManager.generateProjectFromArchetype(DefaultArchetypeManager.java:
73)
at org.apache.maven.archetype.mojos.CreateProjectFromArchetypeMojo.execute(CreateProjectFromArchetypeMojo.java:2
07)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:320)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537)
at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)
at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 22.047s
[INFO] Finished at: Thu Jul 05 10:26:27 WST 2012
[INFO] Final Memory: 7M/18M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-archetype-plugin:2.2:generate (default-cli) on project sta
ndalone-pom: Error merging velocity templates -> [Help 1]


The error ...
[ERROR] org.apache.velocity.exception.ParseErrorException: Lexical error: org.apache.velocity.runtime.parser.TokenMgrErr
or: Lexical error at line 11, column 2. Encountered: <EOF> after : ""

btw. The warning thrown is just noise - The archetype is indeed found.
[WARNING] Archetype not found in any catalog. Falling back to central repository (http://repo1.maven.org/maven2).
[WARNING] Use -DarchetypeRepository=<your repository> if archetype's repository is elsewhere.


If I comment out the line <resource>src/main/resources/Forms/readMe.txt</resource> in my archetype.xml and rebuild the archetype it runs without errors and creates all other folders listed in my archetype.
I have tried to rename this folder and archetype.xml reference but each time I enable this line it fails with the same error.
I have hex edited the archetype.xml to see if there is some unprintable characters in the file but found no issues with it.
The readMe.txt within the Forms folder is also ok.


Any takings on these questions ?

cheers
Marin Cedervall
Greenhorn

Joined: Jul 04, 2012
Posts: 2
Marin Cedervall wrote:
Problem 2 (and this is a really weird one)
cheers


I managed to sort this issue. Still a weird one but after recreating some readMe.txt files it all works.
One would think there is some non printable Hex values in these files but I have checked and rechecked and - no - they are all normal.

Anyway - it works now.

So the remaining issue I have is with the default directory structure and the control of the location of the deployment-descriptior.xml
Would like to have all folders direct under the main project folder and the descriptor next to the pom.xml

Can this be controlled somehow ?

cheers
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15624
    
  15

As a general rule, we do not recommend altering the directory structure for Maven projects. It's generally not worth the grief so set up and maintain the overrides, it's likely to conflict in unexpected ways with built-in goals and it makes it harder for strangers to come in and adopt a project when the familiar structure isn't present.

I'm not quite sure what these "database directories" are all about. Maven is normally used to construct a single deployable unit. The closest thing I can come to what you're implying is that some of my Maven projects have the ability to generate OS-installer packages which create and maintain files and directories external to the Java product being produced.

For Java artifacts themselves, static resources should appear in src/main/resources except when using alternative mojos such as the WAR plugin that uses src/main/webapp instead.

It's possible that what you really need to do is construct a custom plugin, but that, too is not undertaken lightly. In fact, if these extra directories have that much significance, Maven may not be the ideal tool for the job and you might be better off using Ant.


Customer surveys are for companies who didn't pay proper attention to begin with.
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5772
    
    7

I often have to add additional directories into the Maven directory structure. My basic mechanism is to add another directory under src/main, or perhaps under src (exactly where depends on numerous factors), and place files there. Thus, if I were dealing with your files I would have:

src/main/db/Constraints/readMe.txt
src/main/db/DBLinks/readMe.txt
...

I then set up the Resources plugin to copy the files to target/db. If I want to package the results, perhaps into a zip file to distribute to others, I configure the Assembly plugin to copy the files from target/db to the base directory of the zip file.

This basically follows the Maven directory structure - all sources are user src, all build results are under target.


JBoss In Action
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: maven custom archetypes
 
Similar Threads
Modifying JBOSS twiddle code
maven subversion
Maven: Cannot find ArtifactResolver with hint: project-cache-aware
first example, i use maven2
linking my own application jar file to my maven build.