Master Maven Archetype: Quickly Bootstrap Consistent Java Projects
Learn how to use Maven Archetype to create standardized project skeletons, customize templates, generate and share archetypes, and integrate them into IDEs, streamlining Java project initialization, reducing manual setup errors, and improving development efficiency across teams.
Preface
In most enterprise software development ecosystems, project initialization follows a set of template standards, requiring integration of internal middleware, common code layers, and standard formatting. Traditional project startup copies an old project and manually adjusts package names and configurations, which is inefficient and error‑prone, increasing maintenance cost and risk.
Maven Archetype is a project template tool that lets us easily define the basic structure of a project, including necessary dependencies, configuration file templates, and specific code snippets, greatly simplifying project creation, reducing repetitive work, and ensuring consistency so developers can focus on core business logic.
What is Maven Archetype
Maven Archetypeis a template mechanism in
Mavenprojects that can generate a template project based on an existing one, providing the skeleton structure—directories, files, and configurations—so we can quickly start a new project without writing everything from scratch.
How to customize an Archetype
Copy Maven's
settings.xmlto the
.m2directory.
In the project root, run the Maven command:
<code>mvn archetype:create-from-project</code>After successful execution, a
targetfolder is generated in the project root.
Delete unnecessary folders under
target(e.g.,
.idea,
test,
target).
Copy
target\generated-sources\archetypeto a safe location to prevent accidental removal by
mvn clean.
Open the copied project and modify the parent
pom.
Add missing
moduleentries.
<code><modules>
<module>${rootArtifactId}-adapter</module>
<module>${rootArtifactId}-app</module>
<module>${rootArtifactId}-client</module>
<module>${rootArtifactId}-common</module>
<module>${rootArtifactId}-domain</module>
<module>${rootArtifactId}-infrastructure</module>
<module>${rootArtifactId}-start</module>
</modules></code>Update the
dependencyManagementsection to reference each module as follows:
<code><dependency>
<groupId>${groupId}</groupId>
<artifactId>${rootArtifactId}-adapter</artifactId>
<version>${project.version}</version>
</dependency></code>How to use an Archetype
In the modified archetype project root, run:
<code>mvn install</code>After success, an
archetype-catalog.xmlfile appears in the
.m2\repositorydirectory.
Add this file to IDEA’s
archetype catalogs.
When creating a new project, select the added catalog and archetype, fill in project information, and create the project directly.
How to share an Archetype
In the archetype project root, run:
<code>mvn deploy</code>Upload the generated
archetype-catalog.xmlto an OSS and obtain its URL.
Team members add this URL to IDEA’s
archetype catalogs; the remaining steps are the same as above.
Conclusion
Maven Archetypeis a powerful tool that helps quickly create project skeletons, saving tedious configuration and initialization work. By customizing an archetype, teams can tailor templates to project needs and boost development efficiency. Adding the appropriate catalog and archetype in IDEA enables rapid project creation.
References
https://maven.apache.org/archetype/index.html
https://baeldung-cn.com/maven-archetype
Dangbei Technology Team
Dangbei Technology Team public account
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.