Maven定义了很多archetype,可以用命令mvn archtype:generate 获得列表,然后更具体提示选择比较适合你的项目骨架。
但是每个公司组织都有自己的项目结构习惯,那么怎么把已经公司的项目骨架放到公用的地方让新的项目来套用呢,Maven为我们提供了很好的工具。
Maven Archetypes 的功能如同Velocity模板中替换变量,并根据package名等设置扩展java文件的目录结构。
1、用 mvn archetype:create-from-project 从已有的项目生成Archetype模板到 target/generated-sources/archetype。
处理过程中,archetype plugin 要找到package name和变量,将变量转为符号,将目录结构和一些文件做拷贝操作。
2、cd target/generated-sources/archetype,然后用mvn install 将Archetype模板安装到本地maven仓库里。
3、再你要创建新工程的目录里用mvn archetype:generate -DarchetypeCatalog=local命令 ,按照提示输入项目名,package名等变量,即生成项目
不光是apache,还有其他的公司也提供了maven的archetype项目骨架。很方便。
文字太多不形象,画个图。
附:
http://maven.apache.org/guides/introduction/introduction-to-archetypes.html
Provided Archetypes
Maven provides several Archetype artifacts:
Archetype ArtifactIds | Description |
---|---|
maven-archetype-archetype | An archetype which contains a sample archetype. |
maven-archetype-j2ee-simple | An archetype which contains a simplifed sample J2EE application. |
maven-archetype-mojo | An archetype which contains a sample a sample Maven plugin. |
maven-archetype-plugin | An archetype which contains a sample Maven plugin. |
maven-archetype-plugin-site | An archetype which contains a sample Maven plugin site. |
maven-archetype-portlet | An archetype which contains a sample JSR-268 Portlet. |
maven-archetype-quickstart | An archetype which contains a sample Maven project. |
maven-archetype-simple | An archetype which contains a simple Maven project. |
maven-archetype-site | An archetype which contains a sample Maven site which demonstrates some of the supported document types like APT, XDoc, and FML and demonstrates how to i18n your site. |
maven-archetype-site-simple | An archetype which contains a sample Maven site. |
用法:
mvn archetype:create
-DgroupId=[your project's group id]
-DartifactId=[your project's artifact id]
-DarchetypeArtifactId=maven-archetype-j2ee-simple