文章目录
- 项目的整体结构
- / pom.xml 生成骨架基本依赖
- resources / META-INF / maven / archetype-metadata.xml 生成哪些文件
- resources / pom.xml 生成的项目的依赖
- 具体项目
项目的整体结构
/ pom.xml 生成骨架基本依赖
pom.xml里需要引入生成骨架的基本依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.tyzhou.demo</groupId>
<artifactId>demo-archetype</artifactId>
<version>1.0.0.RELEASE</version>
<build>
<extensions>
<extension>
<groupId>org.apache.maven.archetype</groupId>
<artifactId>archetype-packaging</artifactId>
<version>3.0.1</version>
</extension>
</extensions>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-archetype-plugin</artifactId>
<version>3.0.1</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
resources / META-INF / maven / archetype-metadata.xml 生成哪些文件
这个文件用来配置当前项目有几个module
,并且指明哪些文件要被自动生成,包括java、配置文件、test文件。
<?xml version="1.0" encoding="UTF-8"?>
<archetype-descriptor
xsi:schemaLocation="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.0.0 http://maven.apache.org/xsd/archetype-descriptor-1.0.0.xsd"
name="demo"
xmlns="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<!-- 骨架配置文件:配置生成的多模块包,并且配置哪些文件需要被生成 -->
<modules>
<module id="${rootArtifactId}-controller" dir="__rootArtifactId__-controller"
name="${rootArtifactId}-controller">
<fileSets>
<!-- packaged 表示生成包路径,为无知留下了眼泪-->
<fileSet filtered="true" packaged="true" encoding="UTF-8">
<directory>src/main/java</directory>
<includes>
<include>**/*.java</include>
</includes>
</fileSet>
</fileSets>
</module>
<module id="${rootArtifactId}-start" dir="__rootArtifactId__-start" name="${rootArtifactId}-start">
<fileSets>
<fileSet filtered="true" packaged="true" encoding="UTF-8">
<directory>src/main/java</directory>
<includes>
<include>**/*.java</include>
</includes>
</fileSet>
<fileSet filtered="true" encoding="UTF-8">
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
<include>**/*.yml</include>
</includes>
</fileSet>
<fileSet filtered="true" packaged="true" encoding="UTF-8">
<directory>src/test/java</directory>
<includes>
<include>**/*.java</include>
</includes>
</fileSet>
<fileSet filtered="true" encoding="UTF-8">
<directory>src/test/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
<include>**/*.yml</include>
</includes>
</fileSet>
</fileSets>
</module>
</modules>
</archetype-descriptor>
resources / pom.xml 生成的项目的依赖
这个文件是生成的项目的最外层pom.xml文件。我这里使用了pom
打包方式,它会聚合其它项目。大家看我上一步中,我指定了有两个module
。在最终生成的项目中,上面得两个module
会自动配置在这个pom.xml中。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>${groupId}</groupId>
<artifactId>${artifactId}</artifactId>
<!-- 使用pom打包方式来聚合其它项目 -->
<packaging>pom</packaging>
<version>${version}</version>
<name>${artifactId}</name>
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<!-- 导入自定义依赖 -->
<dependencies>
</dependencies>
</project>
具体项目
pom.xml
具体的子项目的配置方式,artifactId与name属性会自动生成,与项目名称一致。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>${groupId}</groupId>
<artifactId>${rootArtifactId}</artifactId>
<version>${version}</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>${artifactId}</artifactId>
<version>${version}</version>
<name>${artifactId}</name>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
CustomerController
Java代码里得包路径,可以使用${package}EL表达式得到。
#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
package ${package}.controller;
import org.springframework.web.bind.annotation.*;
@RestController
public class CustomerController {
@GetMapping(value = "/customer")
public Object listCustomerByName(String name){
return name;
}
}
骨架发布
使用 mvn clean install发布骨架
我这里把骨架生成到了此jar包中。
通过骨架生成项目代码
第一行是固定命令。
第二行到第四行是我希望生成项目及项目包路径。
倒数后三个命令是使用刚才生成的骨架。
mvn org.apache.maven.plugins:maven-archetype-plugin:2.4:generate
-DgroupId=com.ceshi -DartifactId=helloworld
-Dversion=1.0.0-SNAPSHOT
-Dpackage=com.ceshi.helloworld
-DarchetypeArtifactId=demo-archetype
-DarchetypeGroupId=cn.tyzhou.demo
-DarchetypeVersion=1.0.0.RELEASE
最终生成得效果
项目源代码地址
源代码下载