Any good advanced guides on how to create new maven archetypes? [closed]
Asked Answered
M

6

17

I'm looking for a guide on how to create new maven archetypes that involve using parameters to create directories and file names where the parameters are used as prefixes in the file names and part of the package structure/directories that are created by the archetype.

All I can find is very simple instructions on how to make very simple projects.

Machutte answered 25/1, 2011 at 15:44 Comment(0)
L
8

why not use the maven website itself?

http://maven.apache.org/guides/mini/guide-creating-archetypes.html

Letaletch answered 25/1, 2011 at 15:55 Comment(8)
It only provides me the basics. I'd even take an open source archetype definition that I can use as a template for what I want to do.Machutte
so sorry my bad, I misunderstood the last part "simple instructions on how to make very simple projects."Letaletch
you want to create sth like appfuse but with maven then static.appfuse.org ?Letaletch
also that guide is out of date; you should now be creating an archetype-metadata.xml instead, else your integration tests won't runGlasgo
There are additional guides at that site other than the barebones introductory one. I found the ones from a property file or existing project useful.Coneflower
Down voted because the Maven guide there does not at all answer the OP questions (some of the answers below do however).Loden
Is there a better up to date guide for creating archetypes? It's concerning that the official docs are out of date and use a completely different XML format that is unmentioned in their tutorial.Smectic
Down voted for the same reason of @Partly Cloudy. The tutorial is not up to date, and don't seem to work. You have to redo the most step by yourself - regenerate your pom.xml, your App.java with $package variable and as it is specified in the tutorial itself, the archetype-metadata.xmlHoptoad
M
7

I think I found the a site that helps me a little more once I figured out that Velocity templates are used as part of the archetype processes.

HowToCreateMavenArchetypeFromProject

Machutte answered 25/1, 2011 at 16:29 Comment(0)
C
3

I think we met the same problem, finally i resolved it with enlightening from this article

assuming you want to generate package com.company.base.dal.dao, we call com.company.base is root package, dal.dao is core package, then just put your template java file under src/main/java/dal/dao, and tweak the packaged="true" in META-INF/maven/archetype-metadata.xml as below

            <fileSet filtered="true" packaged="true" encoding="GBK"><!--packaged="true" tells maven to copy the core package in to root package while creating a project.-->
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.java</include>
            </includes>
        </fileSet>

say, if you set -Dpackage=com.alibaba.china after execute mvn archetype:generate, it will create java source package as com/alibaba/china/dal/dao/sample.java

as how to give the prefix package information in sample.java, just use ${package} as below

package ${package}.dal.dao;

the archetype plugin will replace ${package} with -Dpackage as velocity template

Cliffcliffes answered 8/12, 2014 at 12:28 Comment(2)
Thanks, this helped me but I needed to change encoding to 'UTF-8'. Also, the link is dead so it's good that you included enough information in the answer.Durham
O Yes, the GBK is to accomodate chinese character, UTF-8 should be fineCliffcliffes
S
1

Even I am looking for the same thing. Even I find Maven home page very basic and does not fulfill our requirement.

I even tried HowToCreateMavenArchetypeFromProject But I still have one question. How to invoke a specific file of the archetype when the project of that type of archetype is generated. To be specific, if I have a java class App.java how to invoke that while creating project?

What happens with create-archetype-from-project is it creates a project of the same files and directory structure and we need to run that again to get that in action.

Also, ${groupId} and such stuff work only in maven files. What if I have a property file and wanna use the artifactId the user has entered?

Do let me know if you come across any good stuff. Meanwhile, I will also let you know when I find any solution.

Thanks

Sahara answered 24/12, 2012 at 6:35 Comment(0)
K
1

from the maven documentacion: http://maven.apache.org/archetype/archetype-models/archetype-descriptor/archetype-descriptor.html

A fileset defines the way the project's files located in the jar file are used by the Archetype Plugin to generate a project. If file or directory name contains property pattern, it is replaced with corresponding property value.

if you can name the file as

__artifactId___local.properties

... when you generate the proyect with artifactId=foo it will contains this file:

foo_local.properties

Keeshakeeshond answered 16/4, 2015 at 9:5 Comment(3)
Warning - the regex is too greedy so if you have a filename Blah___artifactId__ (triple underscore in the middle) it will fail with a silly message saying 'can't find variable _artifactId' when you actually wanted a literal underscore in front.Erubescence
in this case you could hack creating a maven property <code>underscore = _ </code> and name the file as Blah__underscore____artifactId_ –Keeshakeeshond
Agreed but I tried exactly that but no success - I've now elected to change the naming convention to get rid of underscores.Erubescence
P
0

here is a short description but very helpful http://javajeedevelopment.blogspot.fr/2012/05/how-to-create-maven-archetype.html

Putupon answered 26/3, 2013 at 22:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.