# 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.&#x20;

### 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:**&#x20;

```xml
// 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>    
```

2. **Prototype file** = Store the code and configuration. The location is in `src/main/resources/archetype-resources`
3. **Pom for generate project** = Pom for generated project from archetype. The location is in `src/main/resources/archetype-resources`
4. **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)

{% hint style="warning" %}
If you found some issue maybe you need to create the `settings.xml` in `.m2` \
ref: <https://stackoverflow.com/questions/59603307/calling-mvn-archetypecreate-from-project-on-a-simple-archetypewebapp-project-i>
{% endhint %}

2. Verify correctness of code in `target/generated-sources/archetype/src/main/resources/archetype-resources`
3. Navigate to folder `target/generated-sources/archetype` and run command `mvn clean install`&#x20;
4. 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**](https://github.com/brightkut/zeldra)
