通过
maven-archetype-plugin
插件可以自定义符合我们自己需求的archetype。
1.什么是Archetype
Archetype是一个Maven项目模板工具包。通过Archetype我们可以快速搭建Maven项目。
常见的Archetype:
简单的maven工程骨架
maven-archetype-quickstart
maven-archetype-simple
简单的maven web工程骨架
- maven-archetype-webapp
2.使用archetype创建项目
mvn archetype:generate
上面的命令执行后会输出很多maven官方提供的archetype,我们可以根据这些archetype来生成项目骨架。
官方提供的archetype不能满足我们的需求时,我们就需要自定义archetype来方便我们自己使用了。
3.创建自定义的archetype项目
3.1 创建一个maven project
建好自己想要的目录,想引用的包。
- Java代码存放到src/main/java目录下面
- 会将一些通用的配置文件放到src/main/resources目录下面
- 还会有一些html等的文件存放到src/main/webapp目录下面
- pom文件引用
注意version类型一定是RELEASE版本
3.2 添加archetype插件
然后在创建的maven project的pom.xml文件中添加archetype插件。
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-archetype-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
3.3 运行archetype项目
1.安装该archetype项目到你的本地仓库。
mvn archetype:create-from-project
此时会在该项目的target目录下发现如下文件:
---target
---generated-sources
---archetype
---our project
进入到archetype目录下,也就是到达我们项目的根目录执行:
cd target/generated-sources/archetype/
mvn install
[INFO] Installing /home/local/xiao/projects/tmp/project/target/generated-sources/archetype/target/project-1.0-SNAPSHOT.jar to /home/xiao/.m2/repository/com/company/project/1.0-SNAPSHOT/project-1.0-SNAPSHOT.jar
[INFO] [archetype:update-local-catalog]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6 seconds
[INFO] Finished at: Fri Feb 22 23:23:23 GMT 2016
[INFO] Final Memory: 15M/45M
[INFO] ------------------------------------------------------------------------
执行crawl命令,生成archetype-catalog.xml:
mvn archetype:crawl
会发现在咱们的本地仓库的根目录生成archetype-catalog.xml骨架配置文件。
<archetype>
<groupId>com.jeiker.archetype-demo</groupId>
<artifactId>archetype-demo-archetype</artifactId>
<version>1.0.0-RELEASE</version>
<description>archetype-demo</description>
</archetype>
2.发布该archetype项目到你的本地私服(nexus)。
mvn deploy
4.配置IDE
在IDE中配置本地私服(nexus)中archetype的地址。
http://127.0.0.1:8081/nexus/content/groups/public/archetype-catalog.xml
5.使用自定义的archetype
本地
mvn archetype:generate -DarchetypeCatelog=local
远程
mvn archetype:generate -DarchetypeCatelog=romote
mvn archetype:generate -DarchetypeCatelog=http:localhost:8080/archetype-catalog.xml
创建maven普通项目:
mvn archetype:generate
-DgroupId=com.jeiker.xiao
-DartifactId=maven-demo
-DarchetypeArtifactId=maven-archetype-quickstart
-DinteractiveMode=false
-DarchetypeCatalog=local
创建maven web项目:
mvn archetype:generate
-DgroupId=com.jeiker.xiao
-DartifactId=maven-demo-web
-DarchetypeArtifactId=maven-archetype-webapp
-DinteractiveMode=false
-DarchetypeCatalog=local
6.archetype命令
1.帮助命令:
archetype:help
2.爬取一个maven仓库,创建目录文件:
archetype:crawl
3.根据一个工程,创建一个新的archetype:
archetype:create-from-project
4.根据一个archetype,创建一个新的工程:
archetype:generate
5.根据当前的archetype工程,创建一个jar包:
archetype:jar
6.更新本地的maven目录:
archetype:update-local-catalog