Maven scaffolding best practices

Posted by Fabio9999 on Mon, 09 Mar 2020 09:50:01 +0100

Background and objectives

To develop a new project, we need to build a new project. We often copy and paste configuration from other existing projects. It is inevitable that we will step into a hole because of some detailed configuration. Especially for new employees, when they don't know the company's project structure, basic middleware, configuration and jar package version, they will waste a lot of unnecessary time. Based on these pain points, we hope to achieve the following goals:

  • Unify and standardize the project structure and general configuration, and the agreement is greater than the configuration.
  • Improve work efficiency, integrate the company's basic middleware, basic util, code generation tools and other rapid development components, and do it out of the box.
  • Reduce the maintenance cost of the project, so that members of the group can help each other quickly.
  • Unified jar package management. Members of the group do not need to care about the version of general jars. A common parent pom is maintained in the group. When the jar package needs to be upgraded, the upgrade is coordinated.

How to build scaffolding

To build scaffolding, first of all, we need a template project. This template project will integrate some tool classes, general configuration, company middleware, automatic code generation tools, etc., and have a good hierarchical structure to meet the standard of out of the box use. The following is the project structure formulated according to the ari java development specification.

.
├── example-common
│   ├── pom.xml
│   └── src
├── example-dao
│   ├── pom.xml
│   └── src
├── example-domain
│   ├── pom.xml
│   └── src
├── example-manager
│   ├── pom.xml
│   └── src
├── example-message
│   ├── pom.xml
│   └── src
├── example-service
│   ├── pom.xml
│   └── src
├── example-web
│   ├── pom.xml
│   └── src
├── README.md
├── .gitignore
└── pom.xml

Define maven coordinates of pom of scaffold root

<!-- Inherit the team's public pom,Including common three-party package dependency, company middleware and common maven Plug-ins, etc. -->
<parent>
    <groupId>com.company.framework</groupId>
    <artifactId>dependencies-parent</artifactId>
    <version>1.0.0</version>
</parent>
<groupId>com.company</groupId>
<artifactId>example</artifactId>
<version>1.0-SNAPSHOT</version>

Add the following plug-ins to the parent pom

<build>  
    <pluginManagement>  
        <plugins>  
            <!-- Scaffold plug-in -->
            <plugin>  
                <groupId>org.apache.maven.plugins</groupId>  
                <artifactId>maven-archetype-plugin</artifactId>   
            </plugin>  
            <plugin>  
                <groupId>org.apache.maven.plugins</groupId>  
                <artifactId>maven-compiler-plugin</artifactId>  
                <configuration>  
                    <source>1.8</source>  
                    <target>1.8</target>  
                </configuration>  
            </plugin>  
            <plugin>  
                <groupId>org.apache.maven.plugins</groupId>  
                <artifactId>maven-resources-plugin</artifactId>  
                <configuration>  
                    <encoding>UTF-8</encoding>  
                </configuration>  
            </plugin>
            <!-- A command update, mvn versions:set -DnewVersion=1.2-SNAPSHOT -->
            <plugin>
		<groupId>org.codehaus.mojo</groupId>
		<artifactId>versions-maven-plugin</artifactId>
		<configuration>
		    <generateBackupPoms>false</generateBackupPoms>
		</configuration>
	    </plugin>  
        </plugins>  
    </pluginManagement>  
</build>

Rename the. Gitignore file to "gitignore" and create the archetype.properties file in the root directory of the template project. Add the following

# Exclude files packaged into scaffolding
excludePatterns=archetype.properties,*.iml,.idea/,logs/,build.sh
# The maven scaffold will discard the. Gitignore file, which needs to be renamed as "gitignore". The scaffold will replace the "XX" file according to the configuration
gitignore=.gitignore

Execute at root after completion

mvn archetype:create-from-project -Darchetype.properties=archetype.properties

After execution, it will be found that the scaffold project is generated in. / target / generated sources / archetype directory. The generated scaffold project can be regarded as an independent project. The directory structure is as follows

.
├── pom.xml
├── src
│   ├── main
│   │   └── resources
│   │       ├── META-INF
│   │       │   └── maven
│   │       │       └── archetype-metadata.xml
│   │       └── archetype-resources
│   │           ├── __rootArtifactId__-common
│   │           │   ├── pom.xml
│   │           │   └── src
│   │           ├── __rootArtifactId__-dao
│   │           │   ├── pom.xml
│   │           │   └── src
│   │           ├── __rootArtifactId__-domain
│   │           │   ├── pom.xml
│   │           │   └── src
│   │           ├── __rootArtifactId__-manager
│   │           │   ├── pom.xml
│   │           │   └── src
│   │           ├── __rootArtifactId__-message
│   │           │   ├── pom.xml
│   │           │   └── src
│   │           ├── __rootArtifactId__-service
│   │           │   ├── pom.xml
│   │           │   └── src
│   │           ├── __rootArtifactId__-web
│   │           │   ├── pom.xml
│   │           │   └── src
│   │           ├── __gitignore__
│   │           ├── README.md
│   │           └── pom.xml
│   └── test
│       └── resources
│           └── projects
│               └── basic
│                   ├── archetype.properties
│                   └── goal.txt

Execute mvn install in. / target / generated sources / archetype directory to complete the local installation of the scaffold project. If you need to push to the company's private server, you need to add the following content in the pom of the scaffold project, and execute mvn deploy to complete the private server push.

<!-- id libs-snapshots-local And libs-releases-local Need local maven setting.xml Consistency of configuration in file -->
<distributionManagement>
    <snapshotRepository>
        <id>libs-snapshots-local</id>
	<name>libs-snapshots</name>
	<url>Your private server snapshot warehouse address</url>
    </snapshotRepository>
    <repository>
        <id>libs-releases-local</id>
	<name>libs-releases</name>
	<url>Your personal clothes. releases Warehouse address</url>
    </repository>
</distributionManagement>
Copy code

How to use scaffolding

Command mode

Enter the following command to enter groupId and artifactId as prompted, and press enter directly for other options to skip.
Note: the local maven setting.xml file needs to configure the Maven private server address in advance.

mvn archetype:generate 
-DarchetypeGroupId=com.company 
-DarchetypeArtifactId=example-archetype 
-DarchetypeVersion=1.0-SNAPSHOT

idea interface mode

  1. Click file - > New - > project in turn, add scaffolds according to the figure below, fill in scaffolds GroupId, ArtifactId, Version, and Repository fill in the address of maven private service.

     

  2. Select the new scaffold in step 1, click Next to fill in the GroupId, ArtifactId and Version of your project, and then click Next to complete the project generation.

     

  3. The structure of the generated project is as follows. So far, the code framework has been generated and the business code can be written.

 

Published 308 original articles, won praise 1858, visited 14.25 million+
His message board follow

Topics: xml Maven snapshot Apache