Quickly create project templates based on existing projects

MVN Archetype

background

During development, especially when using Maven as a package management tool, there is always a headache when creating new modules.

Common scenarios

An open source project downloaded from the Internet (or a large company project) requires the creation of a new module or system, and this open source project can have many layers. Manual creation is likely to produce errors. Errors, such as Maven dependencies, are sometimes subtle and hard to spot.

A selected.The original model

Take a demo as an example. This demo is nested layer by layer (ali Cola4.0 demo). If you create a new domain module, you have to create six modules, which is very dangerous and very boring.

2. BuildThe original model

1. Select a template to be created

In the demo, we will directly create the entire demo as the original model, so that we can also directly use this architecture to create projects.

2. GenerateThe original model

Go to the pom. XML directory of the demo parent project (demo-web-parent) and execute (no modification required).

mvn archetype:create-from-project  
Copy the code

3. SendThe original modelGo to local warehouse

The original model just generated above needs to be sent to the local Maven repository before it can be used. Go to the generated target\generated-sources\archetype directory and run

mvn clean install
Copy the code

4. Check whether the push is successful

Open Maven’s dependency directory ~\.m2\repository and you can see the file archetype-catalog.xml.


      
<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.alibaba.cola.demo.web</groupId>
      <artifactId>demo-web-parent-archetype</artifactId>
      <version>1.0.0 - the SNAPSHOT</version>
      <description>demo-web-parent-archetype</description>
    </archetype>
  </archetypes>
</archetype-catalog>
Copy the code

3.The original modelCreate a new project \ module

1. Create a vm using IDEA

  1. File->New->Project/Module, see the following interface, enter justarchetype-catalog.xmlI read it in the file3 d coordinate. The fourth item is optional.

2. Set the parameters based on actual conditions.3. The third step, after all, is more important, there are many options, of course, you can not fill in.

key The instance instructions
groupId com.tianjingle.wang Project name of the new project
artifactId demotianjingle The name of the project to create
version 1.0.0 – the SNAPSHOT The version number of the project to create
package com.tianjingle.wang The name of the base package for the project to create
archetypeGroupId com.example ./m2 in XML
archetypeArtifactId demo-archetype ./m2 in XML
archetypeVersion 0.0.1 – the SNAPSHOT ./m2 in XML
archetypeCatalog local Select templates from the local repository
interactiveMode false

2. Run the MVN command

Field description, as shown in the table above

mvn archetype:generate -DgroupId=ltzDemo -DartifactId=com.ltz -Dversion=1.0.0-SNAPSHOT -Dpackage=com.ltz.demo -DarchetypeGroupId=com.alibaba.cola.demo.web -DarchetypeArtifactId=demo-web-parent-archetype -DarchetypeVersion=1.0.0-SNAPSHOT -B -DarchetypeCatalog=local -DinteractiveMode=false
Copy the code

4. The vm is created successfully