A Maven archetype is a parameterized project template that is used to initialize, or create, a new Apache Maven project. Maven archetypes are ideal for creating initial, boilerplate code and scaffolding that is required to get a new Maven project in a working, standardized state.
In this post, I walk through the steps required to create a Maven archetype for a minimal Spring Boot project. I will also show how to customize the archetype so that the user of the archetype can choose which version of Spring Boot he or she wishes to use in their new Spring Boot project.
Before getting started, you need to make sure you have installed the following software on your computer:
To verify that you have installed the JDK correctly, open a terminal, or shell, and execute the following command:
javac --version
The output in your shell should be similar to the following:
javac 17.0.2
To verify that you have installed Apache Maven correctly, execute the following command:
mvn --version
The output in your shell should be similar to the following:
Apache Maven 3.8.5 (3599d3414f046de2324203b78ddcf9b5e4388aa0)
For creating an Apache Maven archetype, we are going to want to be in an empty directory to start. You can name the directory whatever you wish. The name of this directory is not important. We just need an empty directory tree to begin creating the archetype.
Within the empty archetype directory, create a file named archetype.properties
.
Within this properties file, you will define the following four properties:
When complete, archetype.properties
should look like the following:
archetype.groupId=com.nightsky archetype.artifactId=spring-boot-minimal archetype.version=1.0.0 springBootVersion=2.7.8
Next, we need to create a directory to hold all of the files that will compose the archetype.
Again, the name of this directory does not matter, but it is good practice to name it the same
as the value you provided for the archetype.artifactId
property within the
archetype.properties
file.
mkdir spring-boot-minimal
Within the archetype directory we just created, we need to create the standard directories that exist in Spring Boot projects. Execute the following commands to create the directories:
cd spring-boot-minimal mkdir -p src/main/java src/main/resources src/test/java src/test/resources
With all of the directories created, we can start creating the required files.
Let's start by creating the pom.xml
file expected by Apache Maven.
touch pom.xml
Copy the XML content below and paste it into your pom.xml
file.
You will need to change the following lines:
groupId
): The value should match the value of archetype.groupId
in the archetype.properties
fileartifactId
): The value should match the value of archetype.artifactId
in the archetype.properties
fileversion
): The value should match the value of archetype.version
in the archetype.properties
fileversion
): The value should match the value of springBootVersion
in the archetype.properties
file<?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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.nightsky</groupId> <artifactId>spring-boot-minimal</artifactId> <version>1.0.0</version> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.8</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
Next, we need to create the Spring Boot application configuration file (application.yml
).
touch src/main/resources/application.yml
Add the following to the file to make it a valid YAML file and configure Spring Boot to output its banner to the console during startup.
--- spring: main: banner-mode: console
The final requirement for the Spring Boot project is a class annotated with the
@SpringBootApplication
annotation that contains a standard Java
main
method, which bootstraps and launches the Spring-based application.
Create a file named App.java
in src/main/java/com/nightsky/spring/boot/minimal/
by executing the following commands:
mkdir -p src/main/java/com/nightsky/spring/boot/minimal/ touch src/main/java/com/nightsky/spring/boot/minimal/App.java
Then, copy the code below and paste it into App.java
package com.nightsky.spring.boot.minimal; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class App { /** * Point of entry for the Spring Boot application. * * @param args Command line arguments. */ public static void main(String[] args) { SpringApplication.run(App.class, args); } }
When building the archetype distributable, empty folders are omitted. To ensure the directory
tree under src/test/
is included in the archetype, we need to add a couple empty
files to the directories contained under src/test/
. Let's name them .gitkeep
,
and create them using the following commands:
touch src/test/java/.gitkeep touch src/test/resources/.gitkeep
I am naming them .gitkeep
for two reasons:
.gitkeep
file
to an empty folder ensures the folder is included in a new Git repository.
We are now ready to create the Maven archetype. This can be done by executing the following command:
mvn archetype:create-from-project -Darchetype.properties=../archetype.properties
If the process completes successfully, you will find a new directory in your archetype project folder.
It will be named target/
. This directory contains the files required to install the
archetype to your local Maven repository located in $HOME/.m2/
.
Before we install the archetype to our local Maven repository, we need to add a variable for the version
of Spring Boot the user would like to use in their Spring Boot project. We will add this
variable to the template pom.xml
file created by the
mvn archetype:create-from-project
process.
To add the variable, open target/generated-sources/archetype/src/main/resources/archetype-resources/pom.xml
.
Change the version of the Spring Boot parent artifact from 2.7.8
to ${springBootVersion}
.
The parent
entry should now look like the following:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>${springBootVersion}</version> </parent>
We also need to remove the default value of this variable from the archetype metadata. This will force the user of our archetype to specify which version of Spring Boot they wish to use in their project.
To remove the default value, open target/generated-sources/archetype/src/main/resources/META-INF/maven/archetype-metadata.xml
.
Look for the requiredProperties
element and remove the defaultValue
child element.
The requiredProperties
element should now look like the following:
<requiredProperties> <requiredProperty key="springBootVersion"> </requiredProperty> </requiredProperties>
The archetype is now complete, and we are ready to install it to our local Maven repository. Let's install the archetype by executing the following commands:
cd target/generated-sources/archetype/ mvn install
That's it! The custom Maven archetype is now ready to be used to create minimal Spring Boot projects.
After installing the custom Maven archetype to our local Maven repository, it can be used to create a minimal, standardized Spring Boot project by executing the following command:
mvn archetype:generate -DarchetypeCatalog=local \ -DarchetypeGroupId=com.nightsky -DarchetypeArtifactId=spring-boot-minimal -DarchetypeVersion=1.0.0
Note that the values of the command line arguments (archetypeGroupId
,
archetypeArtifactId
, and archetypeVersion
) must match the values of
the corresponding properties in the archetype.properties
file used to create the
Maven archetype.
Maven will prompt you for the following values:
After providing the required values, Maven will ask you confirm the values you provided.
Type Y
and press Enter
to confirm your entries.
Alternatively, if you want to skip the prompts, you can provide all required variable values on the command line like so:
mvn archetype:generate -DarchetypeCatalog=local \ -DarchetypeGroupId=com.nightsky -DarchetypeArtifactId=spring-boot-minimal -DarchetypeVersion=1.0.0 \ -DgroupId=com.myorganization -DartifactId=myproject -Dversion=1.0.0 -Dpackage=com.myorganization.app \ -DspringBootVersion=2.7.7 -DinteractiveMode=false
After executing the mvn archetype:generate
command, your working directory should have a
child directory with a name matching the value you chose for the artifactId
variable.
To verify that your Spring Boot project was created properly, enter the new project directory and run the Spring Boot application. For example:
cd myproject mvn spring-boot:run
If your project was created properly, you should see the Spring Boot banner displayed with the version
that you chose, and Apache Tomcat should be listening on port 8080
.