Master Maven: Build Custom Archetypes, Plugins, and Profile‑Based Packages

This tutorial walks you through creating Maven custom archetype templates, developing and using custom plugins, and configuring build profiles for environment‑specific packaging, providing step‑by‑step commands, code snippets, and visual guides for Java backend projects.

Java Backend Technology
Java Backend Technology
Java Backend Technology
Master Maven: Build Custom Archetypes, Plugins, and Profile‑Based Packages

This article introduces three practical Maven operations frequently needed in private repositories: custom archetype templates, custom plugins, and profile‑based packaging.

1. Creating a Custom Archetype Template

1.1 What is an archetype

When creating a Maven project you can choose from many Apache‑provided templates. The mvn archetype:generate command lists numerous options, each bundling specific dependencies and plugins. Companies often host their own archetype templates in a private repository with pre‑configured dependencies and versions.

1.2 Creating the archetype

If you already have a Maven project and want to turn it into an archetype, navigate to the project root (where pom.xml resides) and run the appropriate Maven command. Maven generates files under target that form the archetype structure.

1.3 Generating the archetype

Change to target/generated-sources/archetype/ and execute mvn install. After a successful build, run mvn archetype:crawl to create archetype-catalog.xml in the local repository.

1.4 Using the archetype

Run mvn archetype:generate -DarchetypeCatalog=local to create a new project from the local archetype. Maven will prompt for the template number, groupId, artifactId, version and package. The project is then generated successfully.

IDEA graphical support

IntelliJ IDEA also provides a UI for creating projects from archetype templates.

2. Developing a Custom Maven Plugin

Below is a simple example; for more advanced features refer to the Mojo API specification.

https://maven.apache.org/developers/mojo-api-specification.html

2.1 Plugin provider structure

The plugin project must modify its packaging to maven-plugin and adjust the pom.xml to include the Mojo dependencies.

The core logic is written in a class (e.g., MyMojo) using annotations such as @Mojo, @Parameter, and defaultPhase = LifecyclePhase.PACKAGE to define the goal name and execution phase.

After implementing the plugin, run mvn clean install to produce a JAR containing the plugin.

2.2 Plugin consumer

The consuming project adds the plugin to its pom.xml and can configure parameters that map to the @Parameter fields in the provider.

When the build reaches the package phase, Maven automatically executes the custom plugin.

3. Using Maven Profiles for Environment‑Specific Packaging

Projects often need different configurations for development, testing, and production (e.g., different application.properties files). Maven profiles allow you to include only the relevant resources in the final JAR.

Where to declare profiles

Per project: defined in pom.xml Per user: defined in ${USER_HOME}/.m2/settings.xml Global: defined in ${maven.home}/conf/settings.xml In this tutorial the profile is declared in the project pom.xml so that building with -Pdev packages dev.properties while -Pprod packages prod.properties.

The final build produces a JAR that contains only the selected application.properties file.

The complete source code for this tutorial is available at https://github.com/fantj/maven-tutorial .

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

mavenArchetypecustom pluginBuild Profile
Java Backend Technology
Written by

Java Backend Technology

Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.