Suggested Pages

Saturday, September 29, 2012

Create and Install a Custom Maven Archetype - Tutorial

In this post I will show how to create a simple maven archetype.

Create Maven Archetype


You have to create a folder tree as this shown below:


STEP-1: Create a folder for the configuration files


As you can see in the previuos snapshot, you have to create a folder /META-INF/maven to put both archetype.xml and archetype-metadata.xml.

STEP-2: Create archetype.xml into folder /META-INF/maven/archetype.xml


In this file you should register all the resources of your archetype and distinguish among resources, sources, etc..
archetype.xml
<?xml version="1.0" encoding="UTF-8"?>
<archetype>
 <id>${artifactId}</id>
 <sources>
     <source>src/main/java/com/simonefolinoblogspot/mavenarchetype/main/Sample.java</source>
   </sources>
 <resources>
  <resource>src/main/webapp/resources/css/main.css</resource>
  <resource>src/main/webapp/resources/js/main.js</resource>
  <resource>src/main/webapp/WEB-INF/web.xml</resource>
  <resource>src/main/webapp/jsp/index.jsp</resource>
 </resources>
</archetype>
   

STEP-3: Create archetype-metadata.xml into folder /META-INF/maven/archetype-metadata.xml


archetype-metadata.xml

<?xml version="1.0" encoding="UTF-8"?>

<archetype-descriptor name="simple-webapp-archetype">
 <fileSets>
  <fileSet encoding="UTF-8" filtered="true">
   <directory>src/main/java</directory>
  </fileSet>
  <fileSet encoding="UTF-8" filtered="true">
   <directory>src/main/resources</directory>
  </fileSet>
  <fileSet encoding="UTF-8" filtered="true">
   <directory>src/main/webapp</directory>
  </fileSet>
 </fileSets>
</archetype-descriptor>


STEP-4: Create a pom.xml into the root of your maven archetype


In the following snippet of code you can notice that groupId, artifactId and versionId are parametrized.
pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>${groupId}</groupId>
 <artifactId>${artifactId}</artifactId>
 <name>${artifactId}</name>
 <version>${version}</version>
 <packaging>jar</packaging>
 <build>
  <plugins>
   <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
     <encoding>UTF-8</encoding>
     <source>1.5</source>
     <target>1.5</target>
    </configuration>
   </plugin>
  </plugins>
 </build>
 <dependencies>
  <dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>servlet-api</artifactId>
   <version>2.4</version>
   <scope>provided</scope>
  </dependency>
  <dependency>
   <groupId>javax.servlet.jsp</groupId>
   <artifactId>jsp-api</artifactId>
   <version>2.0</version>
   <scope>provided</scope>
  </dependency>
 </dependencies>
</project>


STEP-5: Create the structure of your custom archetype into the folder <CUSTOM-MAVEN-ARCHETYPE>/src/main/resources/archetype-resources/


Under the folder archetype-resources you have to put the structure declared in the configuration files written in the previous steps. This folder is the container of your project's archetype.

Install Maven Archetype


Open the shell and go to the path <CUSTOM-MAVEN-ARCHETYPE> at the same level of the first pom.xml in the folder tree. Then you type the following instruction:

mvn clean 
    install 
        -DartifactId=archetypeArtifactId 
        -DgroupId=archetypeGroupId 
        -Dversion=1.0-snapshot 
    archetype:update-local-catalog 
    archetype:crawl


Generate project from Archetype


Open the shell and type the following instruction at the path you prefer.


mvn archetype:generate 
    -DinteractiveMode=false  
    -DarchetypeArtifactId=archetypeArtifactId   
    -DarchetypeGroupId=archetypeGroupId   
    -DarchetypeVersion=1.0-snapshot      
    -DgroupId=mygroup
    -DartifactId=myartifactId
    -Dversion=1.0-snapshot
    -DarchetypeRepository=/repository 
    -DarchetypeCatalog=local



Suggested Posts:


No comments :

Post a Comment

Suggested Pages