一、基本命名規范:
groupId:定義當前Maven項目隸屬的實際項目,例如org.sonatype.nexus,此id前半部分org.sonatype代表此項目隸屬的組織或公司,后部分代表項目的名稱,如果此項目多模塊話開發的話就子模塊可以分為org.sonatype.nexus.plugins和org.sonatype.nexus.utils等。特別注意的是groupId不應該對應項目隸屬的組織或公司,也就是說groupId不能只有org.sonatype而沒有nexus。
例如:我建立一個項目,此項目是此后所有項目的一個總的平台,那么groupId應該是org.jsoft.projectName,projectName是平台的名稱,org.jsoft是代表我個人的組織,如果以我所在的浪潮集團來說的話就應該是com.inspur.syncdata。
artifactId:是構件ID,該元素定義實際項目中的一個Maven項目或者是子模塊,如上面官方約定中所說,構建名稱必須小寫字母,沒有其他的特殊字符,推薦使用“實際項目名稱-模塊名稱”的方式定義,例如:spirng-mvn、spring-core等。
推薦格式:使用實際項目名稱作為artifactId的前綴,緊接着為模塊名稱
舉例:nexus-indexer、spring-mvc、hibernate-c3po……這些id都是以實際項目名稱作為前綴,然后接着一個中划線,再緊跟項目的模塊名稱,默認情況下maven會在artifactId添加version作為最后生成的名稱。例如:spirng-mvn-2.0.0.jar
version:版本號,不要使用日期作為版本,推薦例如這樣的命名:2.0、2.0.1、1.3.1,如果為快照版本(SNAPSHOT),那么會自動在版本號后面加上快照的標識。
二、多模塊開發命名規范:
1、同一項目中所有模塊版本保持一致
2、子模塊統一繼承父模塊的版本
3、統一在頂層模塊Pom的<dependencyManagement/>節中定義所有子模塊的依賴版本號,子模塊中添加依賴時不要添加版本號
4、開發測試階段使用SNAPSHOT
5、生產發布使用RELEASE
6、新版本迭代只修改頂層POM中的版本
舉例:
流程
1、研發開發新功能,假設當前開發的POM版本都是1.0.0-SNAPSHOT
2、封版發布到生產之前,將版本號修改為RELEASE版本。在Maven根模塊下執行以下命令,使用versions-maven-plugin
-
交互式,輸入命令后會彈出提醒要求設置新的版本號。
mvn -DgenerateBackupPoms=false versions:set ... ... Enter the new version to set 1.0.0-SNAPSHOT: 1.0.0
- 非交互式,直接在命令行參數執行新的版本號
mvn -DnewVersion=1.0.0 -DgenerateBackupPoms=false versions:set
該命令在項目根目錄執行,成功執行后會修改根模塊以及所有子模塊里的parent的版本。
3、提交修改版本后的源碼,按要求打Tag,並提交Tag給配管發布
4、Jenkins上在構建服務實現模塊的時候,通過versions:set設置服務實現模塊的版本號為SCM的Tag號,命令如下:(假設子模塊submodule為服務模塊,一般是WEB服務或者WS服務)
mvn -f submodule2/pom.xml -DnewVersion=${SCM_TAG} clean versions:set
示例
父模塊的POM配置
parent_pom.xml
<groupId>com.haier.hairy</groupId> <artifactId>maven-release-test</artifactId> <packaging>pom</packaging> <!-- 明確父項目的版本號 --> <version>3.2.3-SNAPSHOT</version> <modules> <module>submodule1</module> <module>submodule2</module> </modules> <properties> <hry-cxf-common.version>0.0.2</hry-cxf-common.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>com.haier.hairy</groupId> <artifactId>hry-cxf-common</artifactId> <version>${hry-cxf-common.version}</version> </dependency> <dependency> <groupId>com.haier.hairy</groupId> <artifactId>maven-release-test.submodule1</artifactId> <version>${project.parent.version}</version> </dependency> <dependency> <groupId>com.haier.hairy</groupId> <artifactId>maven-release-test.submodule2</artifactId> <version>${project.parent.version}</version> </dependency> </dependencies> </dependencyManagement> <build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-release-plugin</artifactId> <version>2.5.3</version> <configuration> <preparationGoals>clean install</preparationGoals> </configuration> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>versions-maven-plugin</artifactId> <version>2.1</version> </plugin> </plugins> </pluginManagement> </build>
子模塊的POM配置
submodule_pom.xml
<parent> <artifactId>maven-release-test</artifactId> <groupId>com.haier.hairy</groupId> <version>3.2.3-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <packaging>jar</packaging> <artifactId>maven-release-test.submodule1</artifactId> <dependencies> <dependency> <groupId>com.haier.hairy</groupId> <artifactId>hry-cxf-common</artifactId> </dependency> <dependency> <groupId>com.haier.hairy</groupId> <artifactId>maven-release-test.submodule2</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>versions-maven-plugin</artifactId> </plugin> </plugins> </build>
三、其它
1、命名全部小寫
2、 單詞之間使用中橫杠隔開
參考:
http://blog.csdn.net/limm33/article/details/60959044(以上內容部分轉自此篇文章)
https://maven.apache.org/guides/mini/guide-naming-conventions.html(官方包命名規范)
http://www.voidcn.com/article/p-bgimnzku-mz.html(以上內容部分轉自此篇文章)