在開發項目的時候,有許多項目結構都是差不多的,使用的框架也一樣,spring配置文件也一樣的,只是每個項目的名稱,包名等不同,如果按照之前的方法,可以拷貝一個項目,然后再做一些修改就好了。這也不是不可以,只是我覺得,應該還有更帥的招,Maven可以使用archetype創建項目,那是否也可以創建自定義結構的項目了,所以就了解了一下通過Maven使用現有項目創建項目模板。嘗試了一下,很不錯,可以自動處理包名等轉換,幾個命令就可以生成雷同結構的項目,在此記錄一下過程
既然做為模板項目,里面的配置都是做好了的,是一個可以用的項目,加入了Eclipse支持,項目中包含一些Eclipse配置文件,本例中項目目錄為
D:\workspace\myprj
2 創建模板工程
進入項目目錄后,運行生成archetype命令
D:\workspace\myprj>mvn archetype:create-from-project
會在myprj目錄下生成target目錄,其下生成如下目錄
classes
generated-sources
generated-test-sources
m2e-wtp
maven-status
surefire-reports
test-classes
模板代碼主要位于generated-sources目錄中
3 修改模板代碼
項目中的所有代碼都會自動替換為占位符,除了Eclipse配置文件之外,接下來修改模板,進入target\generated-sources\archetype\src\main\resources\archetype-resources\目錄,將Eclipse配置文件也加入占位符,以方便新項目的生成,比如修改下面兩個文件,加入占位符artifactId
文件1:.project
#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>${artifactId}</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.common.project.facet.core.builder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.validation.validationbuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
</natures>
</projectDescription>
文件2:.settings\org.eclipse.wst.common.component
<?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">
<wb-module deploy-name="${artifactId}">
<wb-resource deploy-path="/" source-path="/target/m2e-wtp/web-resources"/>
<wb-resource deploy-path="/" source-path="/src/main/webapp" tag="defaultRootSource"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/resources"/>
<property name="context-root" value="${artifactId}"/>
<property name="java-output-path" value="/${artifactId}/target/classes"/>
</wb-module>
</project-modules>
4 修改項目文件列表
配置完成后,通過mvn生成新的項目時,上面的Eclipse配置文件也不會被放入新的工程里,需要在配置文件中加入這些文件的信息,以及標記哪些文件是需要使用占位符替換的,修改target\generated-sources\archetype\src\main\resources\META-INF\maven\archetype-metadata.xml文件,在現有文件中可以看到如下信息
<fileSet filtered="true" encoding="UTF-8">
<directory>.settings</directory>
<includes>
<include>**/*.xml</include>
</includes>
</fileSet>
也就是說,默認情況下,mvn只將.settings目錄下的*.xml文件生成到項目中,其中filtered="true"表示需要進行內容過濾替換占位符,我們把需要添加的文件放入此配置,修改后結果如下
<fileSet filtered="true" encoding="UTF-8">
<directory>.settings</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.container</include>
<include>**/*.component</include>
<include>**/*.name</include>
<include>**/*.jsdtscope</include>
<include>**/*.prefs</include>
</includes>
</fileSet>
<fileSet filtered="true" encoding="UTF-8">
<directory></directory>
<includes>
<include>.classpath</include>
<include>.project</include>
</includes>
</fileSet>
如果需要過濾掉一些不需要的文件,也可以在此配置。準備好后,就可以安裝了
5 將模板項目安裝到本地倉庫
進入/target/generated-sources/archetype/目錄,執行安裝
D:\workspace\myprj\target\generated-sources\archetype>mvn install
此命令會在maven倉庫中生成archetype-catalog.xml文件,倉庫位置取決于maven配置文件settings.xml中的設置,內容如下,可以修改描述信息,方便在創建項目時進行提示
<?xml version="1.0" encoding="UTF-8"?>
<archetype-catalog xsi:schemaLocation="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-catalog/1.0.0 http://maven.apache.org/xsd/archetype-catalog-1.0.0.xsd"
xmlns="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-catalog/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<archetypes>
<archetype>
<groupId>com.zhouf</groupId>
<artifactId>myprj-archetype</artifactId>
<version>0.0.1-SNAPSHOT</version>
<description>simple project with springmvc and jetty</description>
</archetype>
</archetypes>
</archetype-catalog>
6 創建項目測試
執行生成命令
D:\workspace>mvn archetype:generate -DarchetypeCatalog=local
此命令可以在任何目錄下執行,會在當前目錄生成項目
Choose archetype:
1: local -> com.zhouf:myprj-archetype (simple project with springmvc and jetty)
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): : 1
Define value for property 'groupId': com.zhouf
Define value for property 'artifactId': demoprj1
Define value for property 'version' 1.0-SNAPSHOT: :
Define value for property 'package' com.zhouf: :
Confirm properties configuration:
groupId: com.zhouf
artifactId: demoprj1
version: 1.0-SNAPSHOT
package: com.zhouf
Y: :
生成項目demoprj1
7 導入Eclipse運行
將生成的項目導入到Eclipse環境中,直接部署到服務器上跑一個,測試通過,不需要修改配置,帥
如果項目是通過mvn jetty:run運行的,也可以省略中間加入Eclipse配置文件的操作
轉自我的簡書:
https://www.jianshu.com/p/fb5c0e39692f