When we try to create a maven project into a java IDE (eclipse, netbeans...), it asks which archetype to use. Of course we can use the default archetype provided by the IDE. In this article I will try to detail how to create our own archetype.
First, we create a simple maven project, with eclipse new -> other -> maven:
After that we modify the pom.xml, automatically generated by eclipse like the following:
First, we create a simple maven project, with eclipse new -> other -> maven:
After that we modify the pom.xml, automatically generated by eclipse like the following:
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemalocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelversion>4.0.0</modelversion>
<groupid>com.blogger.archetype</groupid>
<artifactid>brmoez</artifactid>
<version>1.0-SNAPSHOT</version>
<packaging>maven-archetype</packaging>
<name>My archetype creator</name>
<build>
<extensions>
<extension>
<groupid>org.apache.maven.archetype</groupid>
<artifactid>archetype-packaging</artifactid>
<version>2.0</version>
</extension>
</extensions>
<plugins>
<plugin>
<artifactid>maven-archetype-plugin</artifactid>
<version>2.0</version>
<extensions>true</extensions>
</plugin>
</plugins>
</build>
</project>
or use directly the command line:
archetype:generate -DarchetypeGroupId=com.blogger.archetype -DarchetypeArtifactId=myarchetype-creator
-DgroupId=com.blogger -Dpackage=com.blogger.brmoez -DartifactId=brmoez
Second, we can add some classes or resource files to be generated automatically when creating a project and setting the archetype myarchetype-creator, we can also add some parameters (required properties) to be given to the archetype when creating the project.
The files will be added under src/main/resources.
The parameters of the archetype will be configured in the archetype descriptor, for example:
<archetype-descriptor name="brmoez">
<!-- required properties -->
<requiredProperties>
<requiredProperty key="property_1" />
<requiredProperty key="property_2">
<defaultValue>value_2</defaultValue>
</requiredProperty>
</requiredProperties>
<!-- fileSets -->
<fileSets>
<fileSet filtered="true" packaged="true" encoding="UTF-8">
<directory>src/main/java</directory>
<includes>
<include>**/*.java</include>
</includes>
</fileSet>
<fileSet filtered="true" encoding="UTF-8">
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>META-INF/*</include>
</includes>
</fileSet>
<fileSet filtered="true" packaged="true" encoding="UTF-8">
<directory>src/test/java</directory>
<includes>
<include>**/*</include>
</includes>
</fileSet>
<fileSet filtered="true" encoding="UTF-8">
<directory>src/test/resources</directory>
<includes>
<include>**/*</include>
</includes>
</fileSet>
</fileSets>
</archetype-descriptor>
Thanks Moez, but what about packaging ?
ReplyDeleteIn fileSet we have an attribute packaged="true", this file will be added to the package later.
ReplyDeleteI am using maven archetypes for generation of code and as given they also use Velocity. Velocity can be used for dynamic file generation, it can use java objects and use there methods, fields etc to generate dynamic file. However as far as maven archetype is concerned I see that they are only capable of simple variable substitution and a static file/folder structure.
ReplyDeleteIs there a way to do processing prior to creating a project from an archetype, add java objects to the velocity context, and have the archetype templates be able to access them.
Is there some documentation about how to use java objects with the maven archetype to generate dynamic file content?