archetype:翻译成原型,其实就是我们所谓的脚手架。
maven已经给我们提供了很多archetype,但很多时候我们还需要自己手工去做属于自己的个性化archetype。
1、做一个maven工程(model-project)(包括了工具类,配置文件,等),我们的目标是通过自定义的archetype可以生成这个工程的样子。
2、右键工程model-project run as 运行命令:archetype:create-from-project,该命令会生成/target/generated-sources/archetype代码。
3、新建一个普通java工程template,右键configure--->conver to maven 填写上maven坐标,删除src目录,备份pom.xml。
4、把第2步生成的/target/generated-sources/archetype下面的代码都copy到template工程中,覆盖原有代码,修改pom.xml中的坐标为备份中的坐标,同时必面把version中的SNAPSHOT去掉,保证不是快照才能在后面看到该archetype。
5、install template工程。
6、这里新建工程时,在default local 中就能选中刚才我们自己定义的archetype了。
注意:有时需要修改/src/main/resources/META-INF/maven/archetype-metadata.xml该文件
filtered:是否过滤,比如用用户输入的坐标,包名来替换类中的包名,坐标。
packaged:是否按包输出,比如src/main/java会级联生成三个目录。
文件一般结构为:
<?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="hello-api" xmlns="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <fileSets> <fileSet filtered="true" packaged="true" encoding="UTF-8"> <directory>src/main/java</directory> <includes> <include>**/*.*</include> </includes> </fileSet> <fileSet filtered="true" packaged="flase" encoding="UTF-8"> <directory>src/main/php</directory> <includes> <include>**/*.*</include> </includes> </fileSet> </fileSets> </archetype-descriptor>
很容易看明白,但有一种特殊情况是parent工程结构如下:
<modules> <module dir="api" name="service-api"> <fileSets> <fileSet filtered="true" encoding="UTF-8" packaged="true"> <directory>src/main/java</directory> <includes> <include>**/*.*</include> </includes> </fileSet> <fileSet filtered="true" encoding="UTF-8" packaged="true"> <directory>src/test/java</directory> <includes> <include>**/*.*</include> </includes> </fileSet> <fileSet filtered="true" encoding="UTF-8" packaged="false"> <directory>src/main/php</directory> <includes> <include>**/*.*</include> </includes> </fileSet> <fileSet filtered="true" encoding="UTF-8" packaged="false"> <directory>src/main/csharp</directory> <includes> <include>**/*.*</include> </includes> </fileSet> <fileSet filtered="true" encoding="UTF-8" packaged="false"> <directory>src/main/html</directory> <includes> <include>**/*.*</include> </includes> </fileSet> <fileSet filtered="true" encoding="UTF-8" packaged="false"> <directory>src/main/resources</directory> <includes> <include>**/*.*</include> </includes> </fileSet> <fileSet filtered="true" encoding="UTF-8" packaged="false"> <directory>src/test/resources</directory> <includes> <include>**/*.*</include> </includes> </fileSet> </fileSets> </module> <module dir="service" name="service-service"> <fileSets> <fileSet filtered="true" encoding="UTF-8" packaged="false"> <directory>META-INF/service-conf</directory> <includes> <include>**/*.*</include> </includes> </fileSet> <fileSet filtered="true" encoding="UTF-8" packaged="true"> <directory>src/main/java</directory> <includes> <include>**/*.*</include> </includes> </fileSet> <fileSet filtered="true" encoding="UTF-8" packaged="true"> <directory>src/test/java</directory> <includes> <include>**/*.*</include> </includes> </fileSet> <fileSet filtered="true" encoding="UTF-8" packaged="false"> <directory>src/main/resources</directory> <includes> <include>**/*.*</include> </includes> </fileSet> <fileSet filtered="true" encoding="UTF-8" packaged="false"> <directory>src/test/resources</directory> <includes> <include>**/*.*</include> </includes> </fileSet> </fileSets> </module> </modules>