?
MAVEN 與 JAVA 包命名規范
?
拋出問題
?
在使用MAVEN搭建模塊化項目時,我的組織結構如下:
?
- 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為根包
?
存在問題
?
- 項目文件夾命名與maven中artifactId不一致
root模塊項目命名為package-module-project
,root所對應的artifactId命名為chuillusion-package
?
- java包命名與maven不一致
核心模塊中java根包命名為:com.chuillusion.cores
,核心項目中artifactId命名為chuillusion.core
?
- idea顯示不一致
當項目名稱與artifactId不一致時,idea則會在項目名則展示artifactId
?
如:chuillusionCore[chuillusion.core]
,即:項目名[artifactId]
?
命名規則探討
?
- 官網說明
參考MAVEN官方文檔中的命名規范
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.commons
A good way to determine the granularity of the
groupId
is 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.reporting
artifactId 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-math
version 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
?
- 以驅動包案例分析
<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
?
源碼結構:com.mysql作為項目根包
?
疑問:個人感覺是沒有按照規范進行命名的
?
- assertj分析
<dependency><groupId>org.assertj</groupId><artifactId>assertj-core</artifactId><version>3.8.0</version>
</dependency>
?
源碼結構:org.assertj.core作為根包
?
- logback分析
<dependency><groupId>ch.qos.logback</groupId><artifactId>logback-classic</artifactId><version>1.3.0-alpha3</version><scope>test</scope>
</dependency>
?
源碼結構:ch.qos.logback.classic作為根包
?
- 結論
1)源碼包中需要有groupId開頭,緊接artifactId作為根包
?
規范命名
?
養成良好的編碼習慣,從命名規范做起
?
修改項目命名
?
項目名與artifactId相對應,源碼目錄與整體結構對應
?
- root模塊
項目名稱: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項目為空結構,只有一個pom文件負責管理子模塊,因此沒有源碼目錄結構
?
- 核心模塊修改
修改方式一:
?
項目名稱: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>
?
源碼根目錄結構:com.chuillusion.core
?
修改方式二
?
項目名稱: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>
?
源碼根目錄結構:com.chuillusion.core
?
說明
?
寫這篇文章是因為1)項目中遇到的問題;2)在baidu上沒有相關文章
?
歡迎各位留言指正文章的錯誤,謝謝!
---------------------
作者:楚丶迷夢
來源:CSDN
原文:https://blog.csdn.net/sinat_30254575/article/details/79735051
版權聲明:本文為作者原創文章,轉載請附上博文鏈接!
內容解析By:CSDN,CNBLOG博客文章一鍵轉載插件