Maven命名规范收集

一、基本命名规范:

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(以上内容部分转自此篇文章)

原文地址:https://www.cnblogs.com/EasonJim/p/8411509.html