Building a Custom Maven Archetype (Scaffolding) for Spring Boot Projects from Scratch
This article explains how to create a reusable Maven archetype for Spring Boot applications, covering the definition and benefits of scaffolding, step‑by‑step template preparation, archetype generation, artifact adjustments, publishing to a repository, and using the scaffold to quickly start new microservice projects.
Instead of merely providing a project template, this guide teaches you how to build your own scaffolding (Maven archetype) from zero to one, enabling rapid generation of new projects with common configurations.
What is a scaffolding? It is a quick way to create a base project template that includes essential components such as persistence, logging, exception handling, and permission control.
Why use a scaffolding? In micro‑service architectures, many teams need to split monolithic or heavy micro‑service systems into modules (e.g., product, order, user) or domains (e.g., insurance, claims). Manually creating each service repeats boiler‑plate work, risks errors, and wastes resources; a scaffolding automates this process.
Step 1 – Prepare a base Spring Boot template that already integrates common libraries like MyBatis‑Plus, Redis, Logback, Hutool, and Swagger‑UI, or use an existing internal template.
Example Spring Boot template project:
Step 2 – Generate the archetype
2.1 Open the template project in IDEA and verify the Maven path (e.g., D:/Program Files/apache-maven-3.2.5 ).
D:/Program Files/apache-maven-3.2.5
D:\Program Files\apache-maven-3.2.5\conf\settings.xml
D:\Program Files\apache-maven-3.2.5\repository2.2 Ensure the template builds successfully.
2.3 Pay attention to the groupId , artifactId , and version in pom.xml (e.g., org.springframework.boot.demo , spring-boot-stage-demo , 0.0.1‑SNAPSHOT ).
org.springframework.boot.demo
spring-boot-stage-demo
0.0.1‑SNAPSHOT2.4 Run the Maven command to create the archetype from the existing project:
mvn archetype:create-from-projectAlternatively, execute the command from the project directory:
d:
cd D:\spring-stagging
mvn archetype:create-from-projectThe generated archetype structure will appear (see Fig. 4).
If the generated structure is incorrect, adjust the artifactId to match the module prefix.
2.5 Clean IDEA metadata and unnecessary source files:
rm -rf .idea
find . -name ".iml" -type f -print -exec rm -rf {} \;
find . -name "xxxMain" -type f -print -exec rm -rf {} \;2.6 Publish the archetype by configuring distributionManagement in pom.xml and deploying to your Maven repository (e.g., xxx‑central , xxx‑snapshots ).
xxx‑central
libs-releases‑local
http://artifactory.66.com/libs-releases‑local
xxx‑snapshots
libs‑snapshot‑local
http://artifactory.66.com/libs-snapshots‑localThe GAV coordinates to use later are:
org.springframework.boot.demo
spring-boot-stage-demo-archetype
0.0.1‑SNAPSHOT
maven‑archetypeUpload the archetype project to a Git repository for future updates.
Step 3 – Use the scaffolding to create new projects
Configure the GAV of the new target project and run Maven archetype generation with the new artifactId .
Example configuration screenshots are provided (Fig. 8‑11).
After configuring Maven settings, the new project is generated automatically.
Finally, the new project is created based on the updated artifactId (Fig. 11).
Reference
https://maven.apache.org/archetype/maven-archetype-plugin/
Conclusion
Using a custom scaffolding project can reduce the time required to initialize a new engineering project from a day or more to about an hour, greatly improving development efficiency.
JD Retail Technology
Official platform of JD Retail Technology, delivering insightful R&D news and a deep look into the lives and work of technologists.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.