MAVEN 与 JAVA 包命名规范

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上没有相关文章

欢迎各位留言指正文章的错误,谢谢!

    原文作者:chuIllusions丶
    原文地址: https://www.jianshu.com/p/35fd5584de65
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞