原料:maven3,eclipse 4.4.2
需求:领导希望使用自定义的目录结构创建maven工程.
maven默认web工程目录结构如下:
希望自定义为:
step1:使用 maven-archetype-webapp 默认原型新建一个web工程.
step2:修改目录结构为自定义的.
step3:打开命令行,切换到工程根目录,执行 mvn archetype:create-from-project会在生成一个目录为target\generated-sources\archetype将这个文件夹整个复制出来.比如到G盘.将 archetype\src\main\resources\archetype-resources 下的 .setting,.classpath,.project 文件/文件夹删除
step4:在 archetype\src\main\resources\META-INF 新建文件
archetype.xml
<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>webapp3-archetype</id>
<sources>
<!-- 指定源代码 -->
<source>src/Web3Servlet.java</source>
</sources>
<!-- 指定测试源代码 -->
<testSources>
<source>test/TestApp.java</source>
</testSources>
</archetype>
修改 archetype\src\main\resources\META-INF\archetype-metadata.xml 指定工程需要的目录
<?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="webapp3"
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" encoding="UTF-8">
<directory>src</directory>
<includes>
<include>**/*.java</include>
</includes>
</fileSet>
<fileSet filtered="true" encoding="UTF-8">
<directory>test</directory>
<includes>
<include>**/*.java</include>
</includes>
</fileSet>
<fileSet filtered="true" encoding="UTF-8">
<directory>WebRoot</directory>
<includes>
<include>**/*.jsp</include>
</includes>
</fileSet>
<fileSet filtered="true" encoding="UTF-8">
<directory>WebRoot/WEB-INF</directory>
<includes>
<include>**/*.xml</include>
</includes>
</fileSet>
</fileSets>
</archetype-descriptor>
step5:在命令行将目录切换到修改完后的archetype目录.执行 mvn clean install
step6:在eclipse中添加该原型:
file->new->maven project->next 点击Add Archetype填入坐标.就行了(可能需要勾选 Include snapshot archetypes才能显示出来).