Jade Dungeon

Maven Archtype

Maven Archtype

生成项目

交互方式

mvn archtype:generate

先一个编号,然后一步步输入项目的:

  • groupId
  • artifactId
  • version
  • package:生成的Java包名

批处理方式

mvn archtype:generate -B \
	-DarchtypeGroupId=org.apache.maven.archtypes \
	-DarchtypeAritfactId=maven-archtype-quickstart \
	-DarchtypeVersion=1.0 \
	-DgroupId=com.myprjs \
	-DaritfactId=myprj \
	-Dversion=1.0-SNAPSHOT \
	-Dpackage=com.myprj.text

自定义Archtype

要包含:

  • pom.xml
  • src/main/resources/archtype-resources/pom.xml:pom模板
  • src/main/resources/META-INF/maven/archtype-metadata.xml:描述文件
  • `src/main/resources/archtype-resources/**其他的内容

pom.xml

<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>
	<groupId>com.juvenxu.mvnbook.archetypes</groupId>
	<artifactId>mvnbook-archetype-sample</artifactId>
	<version>1.0-SNAPSHOT</version>

</project>

生成的POM模板

<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>${groupId}</groupId>
	<artifactId>${artifactId}</artifactId>
	<version>${version}</version>
	<name>${artifactId}</name>
	<url>http://www.juvenxu.com</url>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.8.1</version>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<pluginManagement>
			<plugins>
				<plugin>
					<groupId>org.apache.maven.plugins</groupId>
					<artifactId>maven-compiler-plugin</artifactId>
					<configuration>
						<source>1.5</source>
						<target>1.5</target>
					</configuration>
				</plugin>
				<plugin>
					<groupId>org.apache.maven.plugins</groupId>
					<artifactId>maven-resources-plugin</artifactId>
					<configuration>
						<encoding>UTF-8</encoding>
					</configuration>
				</plugin>
			</plugins>
		</pluginManagement>
	</build>
</project>

archtype-metadata.xml

  • 声明哪些东西应该包含在Archtype中
  • 这些Archtype使用哪些属性参数
<?xml version="3.0" encoding="UTF-8"?>
<archetype-descriptor name="sample">

	<fileSets>
		<fileSet filtered="true" packaged="true">
			<directory>src/main/java</directory>
			<includes>
				<include>**/*.java</include>
			</includes>
		</fileSet>
		<fileSet filtered="true" packaged="true">
			<directory>src/test/java</directory>
			<includes>
				<include>**/*.java</include>
			</includes>
		</fileSet>
		<fileSet filtered="true" packaged="false">
			<directory>src/main/resources</directory>
			<includes>
				<include>**/*.properties</include>
			</includes>
		</fileSet>
	</fileSets>
	
	<requiredProperties>
		<requiredProperty key="port" />
		<requiredProperty key="groupId">
			<defaultValue>com.juvenxu.mvnbook</defaultValue>
		</requiredProperty>
	</requiredProperties>
	
</archetype-descriptor>
  • fileSets中src/main/java对应的内容放在src/main/resources/archtype-resources/src/main/java
    • filtered属性定义是否过渡参数,如${xxx}
    • packaged定义是否要放到包里
  • requiredProperties定义了必须的参数,以后生成项目时必须提供。

例:

对于项目目录来说,package是必须提供的参数,比如这里为com.juvenxu.mvnbook, 生成后的结构为:

Archtype的src/main/resources下 生成的项目根目录下
archtype-resources/src/main/java/ src/main/java/
archtype-resources/src/main/java/App.java src/main/java/com/juvenxu/mvnbook/App.java
archtype-resources/src/main/java/dao/Dao.java src/main/java/com/juvenxu/mvnbook/dao/Dao.java
archtype-resources/src/main/java/service/Service.java src/main/java/com/juvenxu/mvnbook/service/Service.java

对于filter的内容,会替换文件中的占位符,比如:

package ${package}.dao;

public class Dao {
}

生成项目时用的命令:

mvn archtype:generate -B \
	-DarchtypeGroupId=com.juvenxu.mvnbook.archtypes \
	-DarchtypeAritfactId=mvnbook-archtype-sample \
	-DarchtypeVersion=1.0-SNAPSHOT

Archtype Catalog

定义有哪些archtype模块:

<?xml version="1.0" encoding="UTF-8"?>
<archtype-catalog>
	<archtypes>
		<archtype>
			<groupId>com.apache.maven.archtypes</groupId>
			<artifactId>maven-archtype-quickstart</artifactId>
			<version>1.0</version>
			<description>quickstart</description>
		</archtype>
		<archtype>
			<groupId>com.juvenxu.mvnbook.archtypes</groupId>
			<artifactId>mvnbook-archtype-sample</artifactId>
			<version>1.0-SNAPSHOT</version>
			<description>sample</description>
		</archtype>
	</archtypes>
</archtype-catalog>

来源有:

默认指定为internal, local,可以手工指定,用逗号分开多个:

mvn archtype:generate -DarchtypeCatalog=file:///opt/my-cata.xml,local

生成本地仓库的Archtype catalog

默认遍历用户settings.xml定义的localRepository,并在它下面生成:

mvn archtype:crawl

可以手工指定:

mvn archtype:crawl -Drepository=/tmp/repository \
	-Dcatalog=/tmp/archtype-catalog.xml

nexus-archtype-plugin

nexus-archtype-plugin可以实时生成archtype-catalog.xml文件。

下载,解压到Nexus工作目录的sona-type-works/nexus下的plugin-repository子目录 ,重启就OK。

浏览仓库根目录下看到archtype-catalog.xml,右击下载就可以拿到。