MAVEN 與 JAVA 包命名規(guī)范
拋出問題
在使用MAVEN搭建模塊化項(xiàng)目時(shí),我的組織結(jié)構(gòu)如下:
- root模塊
文件夾名:package-module-project
pom.xml文件:
<project>
<groupId>com.chuillusion</groupId>
<artifactId>chuillusion-package</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>chuillusionCore</module>
<module>chuillusionBrowser</module>
<module>chuillusionApp</module>
<module>chuillusionDemo</module>
</modules>
</project>
- 子模塊
2.1 核心模塊
文件夾名:chuillusionCore
pom.xml文件:
<project>
<parent>
<artifactId>chuillusion-package</artifactId>
<groupId>com.chuillusion</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>chuillusion.core</artifactId>
</project>
Java包命名:com.chuillusion.cores為根包
存在問題
- 項(xiàng)目文件夾命名與maven中artifactId不一致
root模塊項(xiàng)目命名為package-module-project,root所對(duì)應(yīng)的artifactId命名為chuillusion-package
- java包命名與maven不一致
核心模塊中java根包命名為:com.chuillusion.cores,核心項(xiàng)目中artifactId命名為chuillusion.core
- idea顯示不一致
當(dāng)項(xiàng)目名稱與artifactId不一致時(shí),idea則會(huì)在項(xiàng)目名則展示artifactId
如:chuillusionCore[chuillusion.core] ,即:項(xiàng)目名[artifactId]
命名規(guī)則探討
- 官網(wǎng)說明
參考MAVEN官方文檔中的命名規(guī)范
Guide to naming conventions on groupId, artifactId and version
groupId will identify your project uniquely across all projects, so we need to enforce a naming schema. It has to follow the package name rules, what means that has to be at least as a domain name you control, and you can create as many subgroups as you want. Look at
More information about package names.
eg.
org.apache.maven,org.apache.commonsA good way to determine the granularity of the
groupIdis to use the project structure. That is, if the current project is a multiple module project, it should append a new identifier to the parent'sgroupId.eg.
org.apache.maven,org.apache.maven.plugins,org.apache.maven.reportingartifactId is the name of the jar without version. If you created it then you can choose whatever name you want with lowercase letters and no strange symbols. If it's a third party jar you have to take the name of the jar as it's distributed.
eg.
maven,commons-mathversion if you distribute it then you can choose any typical version with numbers and dots (1.0, 1.1, 1.0.1, ...). Don't use dates as they are usually associated with SNAPSHOT (nightly) builds. If it's a third party artifact, you have to use their version number whatever it is, and as strange as it can look.
eg.
2.0,2.0.1,1.3.1
- 以驅(qū)動(dòng)包案例分析
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.43</version>
</dependency>
生成的包名稱為:mysql:mysql-connector-java-5.1.43.jar,即為groupId:artifactId-version.jar
源碼結(jié)構(gòu):com.mysql作為項(xiàng)目根包
疑問:個(gè)人感覺是沒有按照規(guī)范進(jìn)行命名的
- assertj分析
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.8.0</version>
</dependency>
源碼結(jié)構(gòu):org.assertj.core作為根包
- logback分析
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.3.0-alpha3</version>
<scope>test</scope>
</dependency>
源碼結(jié)構(gòu):ch.qos.logback.classic作為根包
- 結(jié)論
1)源碼包中需要有g(shù)roupId開頭,緊接artifactId作為根包
規(guī)范命名
養(yǎng)成良好的編碼習(xí)慣,從命名規(guī)范做起
修改項(xiàng)目命名
項(xiàng)目名與artifactId相對(duì)應(yīng),源碼目錄與整體結(jié)構(gòu)對(duì)應(yīng)
- root模塊
項(xiàng)目名稱:package-module-project
<project>
<groupId>com.chuillusion</groupId>
<artifactId>package-module-project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>chuillusion-core</module>
<module>chuillusion-browser</module>
<module>chuillusion-app</module>
<module>chuillusion-demo</module>
</modules>
</project>
root項(xiàng)目為空結(jié)構(gòu),只有一個(gè)pom文件負(fù)責(zé)管理子模塊,因此沒有源碼目錄結(jié)構(gòu)
- 核心模塊修改
修改方式一:
項(xiàng)目名稱:chuillusion-core
<project>
<parent>
<artifactId>package-module-project</artifactId>
<groupId>com.chuillusion</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>chuillusion-core</artifactId>
</project>
源碼根目錄結(jié)構(gòu):com.chuillusion.core
修改方式二
項(xiàng)目名稱:core
<project>
<parent>
<artifactId>package-module-project</artifactId>
<groupId>com.chuillusion</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>core</artifactId>
</project>
源碼根目錄結(jié)構(gòu):com.chuillusion.core
說明
寫這篇文章是因?yàn)?)項(xiàng)目中遇到的問題;2)在baidu上沒有相關(guān)文章
歡迎各位留言指正文章的錯(cuò)誤,謝謝!