Logo

dev-resources.site

for different kinds of informations.

An Updated Guide to Maven Archetypes

Published at
3/11/2024
Categories
java
maven
programming
beginners
Author
carter907
Categories
4 categories in total
java
open
maven
open
programming
open
beginners
open
Author
9 person written this
carter907
open
An Updated Guide to Maven Archetypes

About-Maven-Archetypes

What is the point?

One of the major goals in programming is to harbour re-usability and scalability. One way in which we can reuse code is by creating a templating system that allows you to quickly get up and running with predefined code that you're confident in. Maven archetypes one example of this system in action.

How do I create my own?

You can create your own Maven Archetype by using a Maven Archetype! Using the predefined org.apache.maven.archetypes:maven-archetype-archetype. Make sure you choose the archetype from Maven Central, as it has the most up-to-date version. You can also safely delete the test directory of the root folder as we this is simply a demonstration. Once you've done that, you can expect the following project structure.

my-archetype/
|-- src/
|   |-- main/
|   |   |-- resources/
|   |   |   |-- archetype-resources/
|   |   |   |   |-- src/
|   |   |   |       |-- main/
|   |   |   |           |-- java/
|   |   |   |           |   |-- App.java
|   |   |   |           |-- test/
|   |   |   |           |   |-- TestApp.java
|   |   |   |-- META-INF/
|   |   |       |-- maven/
|   |   |           |-- archetype-meta.xml  
|   |   |                   
|   |   |-- pom.xml
|-- pom.xml
Enter fullscreen mode Exit fullscreen mode

archetype-meta.xml

archetype.meta.xml provides metadata for your archetype such as definitions for folder locations and how they should behave. More specifically, this is where you will define the expected layout
of your source files, tests, and other properties that you will use in your project. Those files are created directly by you in the archetype-resources folder.

<archetype-descriptor xmlns="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.0.0 http://maven.apache.org/xsd/archetype-descriptor-1.0.0.xsd"  
  name="${artifactId}">

  <fileSets> <!-- fileSets are used to denote some arbitrary group of folders in your project. -->
      <!-- per maven project specification, you should have a directory for your java source files -->
    <fileSet filtered="true" packaged="true">
      <directory>src/main/java</directory>                                                                                                                           
    </fileSet>                                                                                       
      <!-- and a directory for your test files -->
    <fileSet filtered="true" packaged="true">                                                                                                                        
      <directory>src/test/java</directory>                                                                                                                           
    </fileSet>                                                                                                                                                       
  </fileSets>
</archetype-descriptor>
Enter fullscreen mode Exit fullscreen mode

Filtered & Packaged attributes

filtered - You will be using property binding in the java source files to
bootstrap any required properties.

packaged - the fileset will be prepended to the package or group-id of the newly created
project.

Required Properties

Required properties allow you to specify parameters that will
be queried to the user when building your archetype with the help of mvn achetype:generate. These properties can have a defaultValue which will be used if the user does not input any value. these properties are interpolated into the scaffold created in archetype-resources. You can learn more about this templating is done by checking out the Apache Velocity Project

<requiredProperties>
    <requiredProperty key="about">
        <defaultValue>no about specified</defaultValue>
    </requiredProperty>
    <requiredProperty key="color"/>
    <requiredProperty key="due-date">
        <defaultValue>10 days from today</defaultValue>
    </requiredProperty> 
</requiredProperties>

Enter fullscreen mode Exit fullscreen mode

here we create a few required properties. Only color will be required by mvn command but all will be queried if interactive mode is remained on.

Defining Properties

To define properties, we use the following syntax:

${name-of-property}
Enter fullscreen mode Exit fullscreen mode

Here is our example template locate in src/main/resources/archetype-resources/main/java/

package $package;

/**
 *
 *  ${about}
 *  due-date: ${due-date}
 */
public class App
{
    public static void main( String[] args )
    {
        System.out.println("Your favorite color is ${color}");
    }
}

Enter fullscreen mode Exit fullscreen mode

Installing

Installing is a matter of calling mvn clean install to install the archetype to our local repository, .m2/repository, which can be found as a hidden folder under your user directory.

Generating a project using the Archetype & Final Result

Now it's time to finally generate the template and retrieve it from our
local repository which will be referenced automatically when we call mvn achetype:generate

mvn archetype:generate
    -DgroupId=com.example \
    -DartifactId=my-archetype-created \
    -DarchetypeArtifactId=my-new-maven-archetype \
    -DarchetypeGroupId=org.example \
    -DarchetypeVersion=1.0-SNAPSHOT \
    -DinteractiveMode=false \
    -Dcolor=red \
    -Ddue-date=09/05/2023 \
    -Dabout='This project was created using Maven Archetypes, and this text is from the command line'


Enter fullscreen mode Exit fullscreen mode

This command will generate the project in the current directory
using your artifact id (my-archetype-created in the above example). Now that we have are project set, we can view the templating taken place in App.java

package com.example;

/**
 *
 *  This project was created using Maven Archetypes, and this text is from the command line
 *  due-date: 09/05/2023
 */
public class App
{
    public static void main( String[] args )
    {
        System.out.println("Your favorite color is red");
    }
}
Enter fullscreen mode Exit fullscreen mode
maven Article's
30 articles in total
Favicon
SpringBoot Web Service - Part 5 - Github Action
Favicon
SpringBoot Web Service - Part 2 - Preparing Using Spring Initializr
Favicon
SpringBoot Web Service - Part 1 - Create Repository
Favicon
SpringBoot Web Service - Part 4 - Initial Configuration
Favicon
Identifying and Removing Unused Dependencies in pom.xml
Favicon
JeKa: The Simplest Way to Start with Java for Real
Favicon
JeKa: The Simplest Way to Create Uber and Shade Jars
Favicon
JeKa: The Simplest Way to Publish on Maven Central
Favicon
Preparando o ambiente de desenvolvimento da melhor API de tabelas de campeonato que vocรช jรก viu!
Favicon
Maven Guide for Beginners
Favicon
Wednesday Links - Edition 2024-10-16
Favicon
Unlock 10% Discounts in 5 Minutes: Build a Drools Project with Maven
Favicon
Getting Started with Maven
Favicon
Quick fix: com.github.everit-org.json-schema:org.everit.json.schema:jar:1.12.2 was not found
Favicon
How to deploy to maven central repo
Favicon
No SNAPSHOTs
Favicon
Maven Commands Cheat Sheet
Favicon
Apache Maven Kirish
Favicon
Difference between mvn install and mvn package
Favicon
Adding Liquibase plugin into Apache Maven managed project
Favicon
Accelerate Maven Application Builds: Maximizing Efficiency with Docker Volumes for Maven Repository Sharing
Favicon
Check for newer versions of dependencies in pom.xml
Favicon
Accelerate Your Automation Testing with Maven: A Step-by-Step Guide ๐Ÿš€
Favicon
Junit Badge For Git Project
Favicon
Jenkins CICD
Favicon
Set JVM Timezone when running JUnit 5 tests
Favicon
Maven 4 - Part I - Easier Versions
Favicon
An Updated Guide to Maven Archetypes
Favicon
H2 database Setup Error Unable to load name org.hibernate.dialect.Oracle10gDialect
Favicon
Why I prefer Maven over Gradle

Featured ones: