MAVEN 與 JAVA 包命名規範

weixin_34290000發表於2018-03-28

MAVEN 與 JAVA 包命名規範

丟擲問題

在使用MAVEN搭建模組化專案時,我的組織結構如下:

  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. 專案資料夾命名與maven中artifactId不一致

root模組專案命名為package-module-project,root所對應的artifactId命名為chuillusion-package

  1. java包命名與maven不一致

核心模組中java根包命名為:com.chuillusion.cores,核心專案中artifactId命名為chuillusion.core

  1. idea顯示不一致

當專案名稱與artifactId不一致時,idea則會在專案名則展示artifactId

如:chuillusionCore[chuillusion.core] ,即:專案名[artifactId]

命名規則探討

  1. 官網說明

參考MAVEN官方文件中的命名規範

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. 以驅動包案例分析
<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作為專案根包

疑問:個人感覺是沒有按照規範進行命名的

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

原始碼結構: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>

原始碼結構:ch.qos.logback.classic作為根包

  1. 結論

1)原始碼包中需要有groupId開頭,緊接artifactId作為根包

規範命名

養成良好的編碼習慣,從命名規範做起

修改專案命名

專案名與artifactId相對應,原始碼目錄與整體結構對應

  1. 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檔案負責管理子模組,因此沒有原始碼目錄結構

  1. 核心模組修改

修改方式一:

專案名稱: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上沒有相關文章

歡迎各位留言指正文章的錯誤,謝謝!

相關文章