Most companies have a common template project to help you create a project quickly. Typically, the project involves integrating some internal middleware, unit testing, standard code formats, common code layering, and so on.

Today, just use Maven’s Archetype plugin to make this easy.

As you can see clearly from the above figure, there are just a few simple steps to actually take advantage of this plug-in mechanism:

  1. Archetype :create-from-project, according to their own project code to generate a prototype project
  2. Generate prototype files using commands such as install
  3. Archetype :generate, generate target projects through prototypes

It looks pretty simple, but you’ll find that when you play with what you find on the Internet, the projects you generate are pretty stupid, including the official documentation, and I really don’t understand why they’re so cool, step by step. What the fuck is that?

There are a lot of problems, such as module name does not change, package name does not change in the code, dependency error a lot of problems, but it is necessary to say what to do in the middle.

Create the Archetype

First, prepare our own template project and make sure the code is OK.

Go to the project root directory and run the following command:

mvn archetype:create-from-project
Copy the code

Then the project root directory will generate the target folder, this is very simple, there is no obstacle, you look at the picture below, pay attention to the name of each module, this is I changed!!

You’ll probably notice he doesn’t look like this, that’s the problem! Read on.

This part of the core is how to modify the target/generated sources/SRC/main/resources/archetype – file under resources.

If you continue without modification, the resulting project will find that the module name will not change, the package name will not change, and the code reference will have a bunch of errors.

And then, let’s see what we can do. This is a bunch of problems.

Father modified pom

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

	<modules>
		<module>${rootArtifactId}-client</module>
		<module>${rootArtifactId}-common</module>
		<module>${rootArtifactId}-service</module>
		<module>${rootArtifactId}-facade</module>
		<module>${rootArtifactId}-starter</module>
	</modules>
Copy the code

Every module referenced by parent POM dependencies should also be modified, groupId and artifactId should be modified as I give them, don’t die!!

<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>${groupId}</groupId>
				<artifactId>${rootArtifactId}-client</artifactId>
				<version>${project.version}</version>
			</dependency>. .</dependencies>
	</dependencyManagement>
Copy the code

Modify the module

This is the problem in the figure. The module generated by default is probably the name of the template project and needs to be changed to something like __rootArtifactId__-client, with double underscores.

The cross-reference groupId and artifactId in the Module are then modified according to the parent POM.

Archetype – metadata changes

Go to meta-INF /maven/archetype-metadata. XML and modify modules. Pay attention to id, dir, and name.

<modules>
    <module id="${rootArtifactId}-client" dir="__rootArtifactId__-client" name="${rootArtifactId}-client">
      <fileSets>
        <fileSet filtered="true" packaged="true" encoding="UTF-8">
          <directory>src/main/java</directory>
          <includes>
            <include>**/*.java</include>
          </includes>
        </fileSet>
      </fileSets>
    </module>. .</modules>    
Copy the code

Go to target/generated-sources/archetype and run the following command:

mvn install
Copy the code

By the way, execute MVN deploy and upload to nexus.

The use of Archetype

Archetype is finished, the biggest pit is the one above, it took a long time, it’s a pit.

Now let’s see how it works. Two ways to use it.

The command line

Go anywhere you want to save the project and run the command.

mvn archetype:generate -DarchetypeCatalog=local
Copy the code

GroupId and artifactId can be created as prompted.

How can it be used by other people?

Your local Maven repository directory (such as ~/.m2/repository) has a file archetype-catalog.xml that you can share with others.


      
<archetype-catalog xsi:schemaLocation="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-catalog/1.0.0 http://maven.apache.org/xsd/archetype-catalog-1.0.0.xsd"
    xmlns="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-catalog/1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <archetypes>
    <archetype>
      <groupId>com.example</groupId>
      <artifactId>template-archetype</artifactId>
      <version>1.0.0 - the SNAPSHOT</version>
      <description>Example Project</description>
    </archetype>
  </archetypes>
</archetype-catalog>
Copy the code

IDEA

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

Next, enter GroupId, ArtifactId, Version information for our customized Archetype.

Once Add is successful, we can see our own archetype in the list and then create it according to the process.