πŸƒMaven Archetype

Describe about Maven Archetype

Maven Archetype is a Maven project templating toolkit that we can use to create our own custom template of the project (Think as a Image) , Then this image will be used to generate the project.

Advantage

  1. Reduce time when we need to initialize new project

  2. Have a standard project structure when we work with multiple team.

Component

  1. Archetype Descriptor = Configure selected file that we want to use for generate in archetype and define which folder is test folder or code folder. The location is in src/main/resources/META-INF/maven that consist of archetype.xml or archetype-metad.xml

Example:

// Example
<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>
      </includes>
    </fileSet>
    <fileSet filtered="true" packaged="true" encoding="UTF-8">
      <directory>src/test/java</directory>
      <includes>
        <include>**/*.java</include>
      </includes>
    </fileSet>
<fileSets>    
  1. Prototype file = Store the code and configuration. The location is in src/main/resources/archetype-resources

  2. Pom for generate project = Pom for generated project from archetype. The location is in src/main/resources/archetype-resources

  3. Pom for archetype = It is root level of archetype project or target folder ( If archetype is created from existing project )

How to create new project from archetype ?

  1. use this command to generate the project

mvn archetype:generate \
    -DarchetypeGroupId=<archetype-group-id> \
    -DarchetypeArtifactId=<archetype-artifact-id> \
    -DarchetypeVersion=<archetype-version> \
    -DgroupId=<new-project-group-id> \
    -DartifactId=<new-project-artifact-id> \
    -Dversion=<new-project-version> 

There are 2 way to create own archetype

  1. Create new archetype project. ( can use maven archetype plugin to generate sample code)

  2. Create archetype from existing project

How to create archetype from existing project ?

  1. Run command mvn archetype:create-from-project (You can add -X to debug If error occured to debug)

  1. Verify correctness of code in target/generated-sources/archetype/src/main/resources/archetype-resources

  2. Navigate to folder target/generated-sources/archetype and run command mvn clean install

  3. Double check in .m2/repository whether the archetype is created or not.

For more information you can learn in this repository: https://github.com/brightkut/zeldra

Last updated