MAVEN 與 JAVA 包命名規範
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上沒有相關文章
歡迎各位留言指正文章的錯誤,謝謝!
相關文章
- java命名規範Java
- Mysql 安裝包命名規範MySql
- Java中的命名規範。Java
- JS變數與命名規範JS變數
- PHP 命名規範PHP
- JavaScript 命名規範JavaScript
- PHP命名規範PHP
- CSS命名規範CSS
- SqlServer命名規範SQLServer
- Google命名規範Go
- 【學習筆記】之:Java命名規範筆記Java
- 前端開發規範:命名規範、html規範、css規範、js規範前端HTMLCSSJS
- 『前端規範化』CSS命名規範化前端CSS
- Android 程式碼規範 - 命名規範Android
- Android程式碼規範:命名規範Android
- 阿里Android開發規範:資原始檔命名與使用規範阿里Android
- Python命名規範Python
- CSS — BEM 命名規範CSS
- 前端命名基本規範前端
- css 命名規範 BEMCSS
- Android命名規範Android
- C#命名規範C#
- mysql及php命名規範MySqlPHP
- div+css命名規範CSS
- 前端工程程式碼規範(一)——命名規則與工程約定前端
- 一文帶你瞭解Java的命名規範!Java
- 零基礎快速入門:java的命名規範Java
- Golang 推薦的命名規範Golang
- css命名和書寫規範CSS
- 我的專案命名規範
- BEM命名規範結合SCSSCSS
- CSS 選擇器命名規範CSS
- 檔案/資源命名規範
- C#開發命名規範C#
- Laravel命名規範速查表Laravel
- 軟體版本命名規範
- css書寫和命名規範CSS
- android檔案命名規範Android