Maven Archetype multi Module custom code scaffold

Posted by matanoosh on Fri, 14 Jan 2022 12:36:10 +0100

Most companies will have a common template project to help you quickly create a project. Usually, this project needs to integrate some internal middleware, unit testing, standard code format, common code layering, etc.

Today, use Maven's Archetype plug-in to simply implement this function.

It is clear from the above figure that the actual use of this plug-in mechanism involves several simple steps:

  1. Archetype: create from project, which generates a prototype project according to its own project code
  2. Generate prototype files through install and other commands
  3. archetype:generate, which generates the target project through the prototype

It seems very simple, but you will find that if you use the information found on the Internet to play, the projects you generate are very stupid X, including the official documents. I really don't understand why they are so awesome, step by step.. What the fuck?

For a lot of problems, such as the module name will not change, the package name will not change, and the code will not change. There are a lot of problems with dependency error reporting. It is still necessary to say what to do in the middle.

Create Archetype

First, prepare our own template project to ensure that the code is OK.

Enter the project root directory and execute the command:

mvn archetype:create-from-project

Then the target folder will be generated under the project root directory. This is very simple and there will be no obstacles. Look at the following figure and pay attention to the name of each module. This is what I changed!!

You should find that he doesn't grow like this. That's the problem! Keep looking down.

The core part here is how to modify the files under target / generated sources / SRC / main / resources / archetype resources.

If you continue without modification, the final generated project will find that the module name and package name will not change, and there will be a lot of errors referenced in the code.

Then, let's see how to change this pile of broken problems.

Modify parent pom

First, if you find the pom file in the root directory, you will find that the module information is missing, which must be added.


Each module referenced by the parent pom dependency should also be modified. The groupId and artifact ID should be changed in the way I give. Don't write it dead!!

   ... ...

Modify module

This is the problem in the figure. The module generated by default may be the name of the template project and needs to be modified to be similar__ rootArtifactId__- Note that the form of client is double underlined.

Then, the cross referenced groupId and artifactId in the module are modified accordingly according to the parent pom.

Archetype metadata modification

Find meta-inf / Maven / archetype metadata XML file, modify the relevant parts of modules, and focus on the modification methods of id, dir and name.

    <module id="${rootArtifactId}-client" dir="__rootArtifactId__-client" name="${rootArtifactId}-client">
        <fileSet filtered="true" packaged="true" encoding="UTF-8">
    ... ...

After modification, enter the target / generated sources / archetype directory and execute the command:

mvn install

By the way, execute mvn deploy and upload it to nexus.

Using Archetype

After the above steps, the prototype Archetype has been created. In fact, the largest pit, that is, the part above, took half a day. It's like a pit father.

Let's see how to use it. There are two ways to use it.

command line

Just go to the path where you want to save the project and execute the command.

mvn archetype:generate -DarchetypeCatalog=local

Enter groupId and artifactId as prompted to complete the creation.

How do you give it to others?

Your local maven repository directory (such as ~ /. m2/repository) has a file archetype catalog XML, just share it with others.

<?xml version="1.0" encoding="UTF-8"?>
<archetype-catalog xsi:schemaLocation=""
      <description>Example Project</description>


Create a new project, select Maven, check Create from archetype, and select Add Archetype

Next, enter the GroupId, ArtifactId and Version information of our customized Archetype.

After adding successfully, we can see our own archetype in the list, and then create it according to the process.