1. 簡介
隨着項目開發的規範逐漸落地,對應的項目目錄結構要亟待需要進行統一,這樣就可以使的所有的項目都有相同的工程目錄結構,這樣代碼review,跨系統開發等都相對來說要容易很多。
2. 使用
僅僅使用如下命令就可以生成對應的目錄結構,步驟如下:
(1)在命令行輸入mvn archetype:generate 命令,如下所示:
mvn archetype:generate -DarchetypeGroupId=com.ctfin.framework -DarchetypeArtifactId=code-framework -DarchetypeVersion=0.0.1-SNAPSHOT -DarchetypeCatalog=local
(2)根據命令提示輸入對應的項目的groupId和artifactId,以及對應的appName和packageName等參數,就可以生產對應的項目了,如下圖所示:
生成對應的項目目錄結構如下圖所示:
3. 設計和原理
3.1項目目錄結構設計
模塊之間的依賴關係如下:
模塊功能介紹:
3.2實現原理說明
利用mvn來生成對應的項目目錄架構,下面將一步一步講述這個實現的過程:
3.2.1新建項目
在eclipse上新建一個mvn項目,選擇maven-archetype-archetype,單機next進行下一步
3.2.2輸入項目新建信息
輸入項目對應的groupId,artifactId和版本等信息,如下圖所示:
3.2.3項目結構
單擊finish,對應的項目就建好了,如下圖所示:
3.2.4項目pom.xml新增內容
<distributionManagement>
<repository>
<id>thirdparty</id>
<name>ctfin_thirdparty</name>
<url>http://192.168.1.202:9091/repository/thirdparty/</url>
</repository>
</distributionManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-archetype-plugin</artifactId>
<version>2.3</version>
</plugin>
<plugin> <!-- 打源碼 -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.4</version>
<configuration>
<attach>true</attach>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
3.2.4 Archetype.xml重命名
由於我們要建多模塊的項目,則需要將META-INF/maven/archetype.xml重命名爲archetype-metadata.xml,具體參見:https://maven.apache.org/guides/mini/guide-creating-archetypes.html和http://maven.apache.org/archetype/archetype-models/archetype-descriptor/archetype-descriptor.html
3.2.5新建模塊結構
可以在archetype-resources文件夾中新建新的模塊(新的文件夾)
對應的archetype-resources/biz-service-impl/pom.xml文件的內容如下:
對應的archetype-resources/pom.xml定義:
GroupId,artifactId和version,appName在archetype-metadata.xml中定義如下:
3.2.6新建模塊定義
然後在archetype-metadata.xml中增加對應的模塊定義:注意module定義中的id對應的是模塊下面對應pom.xml中的${artifactId}的值
3.2.7執行項目編譯打包部署命令
模塊都定義好之後,在這個項目的根目錄下面執行mvn clean install命令
3.2.8根據新建結構生成新的項目
根據項目的groupId,artifactId和version生成對應的新工程,按照這個項目結構,對應的命令如下:
mvn archetype:generate -DarchetypeGroupId=ctfin.framework -DarchetypeArtifactId=InduFramework -DarchetypeVersion=0.0.1-SNAPSHOT -DarchetypeCatalog=local
3.2.9將項目上傳私倉
將項目jar包上傳的私倉,通過如下命令:
mvn deploy:deploy-file -DpomFile=../pom.xml -DgroupId=com.ctfin.framework -DartifactId=code-framework -Dversion=0.0.1-SNAPSHOT -Dpackaging=jar -Dfile=code-framework-0.0.1-SNAPSHOT.jar -Dsources=code-framework-0.0.1-SNAPSHOT-sources.jar -Durl=http://192.168.1.202:9091/repository/thirdparty/ -DrepositoryId=thirdparty
4. 代碼地址
https://github.com/lwjaiyjk/projectFrameworkGenerate
轉載請說明出處