咱们在实际工做中 ,有些项目的架构是类似的,例如基于 restful的接口项目,若是每次都从新搭建一套架构或者经过拷贝创建一个项目不免有些得不偿失,这里咱们能够用maven的archtype创建项目模版来解决 。java
建立maven archetype的步骤:apache
archetype的pom,这里面的groupId,artifactId,version会在执行maven 的archetype generate 时显示restful
其中pom中的 id应该和archetype.xml里面的artifactId一致架构
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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>my.groupId</groupId> <artifactId>quickstart</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> </project>
archetype.xml maven
<archetype xmlns="http://maven.apache.org/plugins/maven-archetype-plugin/archetype/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/plugins/maven-archetype-plugin/archetype/1.0.0 http://maven.apache.org/xsd/archetype-1.0.0.xsd"> <id>quickstart</id> <sources> <source>src/main/java/App.java</source> </sources> <testSources> <source>src/test/java/AppTest.java</source> </testSources> </archetype>
<allowPartial>true</allowPartial> 该标签说明在一个已经存在的项目中依然能够运行 mvm archetypeui
<sources>, <resources>, <testSources>, <testResources> , <siteResources>表明项目的不一样部分spa
<sources>,<testSources>包含<source>,这个表示里面的一个源码文件rest
<testResources> a <siteResources>包含<resource>,这个表示里面是一个资源文件code
<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> <packaging>jar</packaging> <name>A custom project</name> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project> ${artifactId}和${groupId} 将在执行maven archetype 里面提示
配置完后,执行
mvn install完成安装
安装后,执行
mvn archetype:generate \
-DarchetypeGroupId=<archetype-groupId> \
-DarchetypeArtifactId=<archetype-artifactId> \
-DarchetypeVersion=<archetype-version> \
-DgroupId=<my.groupid> \
-DartifactId=<my-artifactId>
能够完成该架构的安装
eg:
mvn archetype:generate \
-DarchetypeGroupId=com.taobao.data.arch \
-DarchetypeArtifactId=jersey-quickstart \
-DarchetypeVersion=1.0-SNAPSHOT \
-DgroupId=com.taobao.data.zl \
-DartifactId=testjersey