MAVEN 與 JAVA 包命名規(guī)范

MAVEN 與 JAVA 包命名規(guī)范

拋出問題

在使用MAVEN搭建模塊化項(xiàng)目時(shí),我的組織結(jié)構(gòu)如下:

  1. 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>
  1. 子模塊

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為根包

存在問題

  1. 項(xiàng)目文件夾命名與maven中artifactId不一致

root模塊項(xiàng)目命名為package-module-project,root所對(duì)應(yīng)的artifactId命名為chuillusion-package

  1. java包命名與maven不一致

核心模塊中java根包命名為:com.chuillusion.cores,核心項(xiàng)目中artifactId命名為chuillusion.core

  1. idea顯示不一致

當(dāng)項(xiàng)目名稱與artifactId不一致時(shí),idea則會(huì)在項(xiàng)目名則展示artifactId

如:chuillusionCore[chuillusion.core] ,即:項(xiàng)目名[artifactId]

命名規(guī)則探討

  1. 官網(wǎng)說明

參考MAVEN官方文檔中的命名規(guī)范

Guide to naming conventions on groupId, artifactId and version

  1. 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's groupId.

    eg. org.apache.maven, org.apache.maven.plugins, org.apache.maven.reporting

  2. 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

  3. 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

  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)行命名的

  1. assertj分析
<dependency>
    <groupId>org.assertj</groupId>
    <artifactId>assertj-core</artifactId>
    <version>3.8.0</version>
</dependency>

源碼結(jié)構(gòu):org.assertj.core作為根包

  1. 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作為根包

  1. 結(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)

  1. 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)

  1. 核心模塊修改

修改方式一:

項(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ò)誤,謝謝!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,586評(píng)論 19 139
  • 簡介 概述 Maven 是一個(gè)項(xiàng)目管理和整合工具 Maven 為開發(fā)者提供了一套完整的構(gòu)建生命周期框架 Maven...
    閩越布衣閱讀 4,535評(píng)論 6 39
  • 文章作者:Tyan博客:noahsnail.com 2.Introduction to the Spring Fr...
    SnailTyan閱讀 5,539評(píng)論 7 56
  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,855評(píng)論 0 10
  • 老鐵們,今天開始下果園采蜜柚了, 水分,糖度,色澤都基本okay, 不僅色澤亮麗肉厚多汁, 且清甜微酸細(xì)膩清甜, ...
    柴可夫JJ閱讀 367評(píng)論 0 0

友情鏈接更多精彩內(nèi)容