美文网首页我爱编程
MAVEN 与 JAVA 包命名规范

MAVEN 与 JAVA 包命名规范

作者: chuIllusions丶 | 来源:发表于2018-03-28 22:56 被阅读1285次

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

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

相关文章

  • MAVEN 与 JAVA 包命名规范

    MAVEN 与 JAVA 包命名规范 抛出问题 在使用MAVEN搭建模块化项目时,我的组织结构如下: root模块...

  • Java项目版本管理规范

    Java项目版本管理规范 版本命名规则 Prong Boot / Prong Cloud的版本命名规范在maven...

  • 领域驱动设计与工程构建规范

    在领域驱动的理念基础下,定义工程命名规范和包路径规范,与大家交流命名规范。在此使用java实现工程,运行生成初始开...

  • 命名规则

    Java Bean的命名规范如下: (1)包命名:全部字母小写。 (2)类命名:每个单词首字母大写。 (3)属性名...

  • maven 命名规范

    Guide to naming conventions on groupId, artifactId, and v...

  • Android开发规范

    一、Java语言规范 详见:Android开发java编写规范 二、Android资源文件命名与使用 1. 【推荐...

  • java 使用包进行类管理

    定义java包 1.package+包名2.java中一个包内不能存在同名类3.包命名规范为 域名倒叙+模块名+功...

  • Android开发中的优化方案

    一、命名规范 代码规范先从命名规范开始,Android的命名规范主要涉及:Java源代码,xml文件,图片资源。 ...

  • python命名规范

    Google Python命名规范 module_name, 模块 package_name, 包 模块与包 ...

  • android 开发规范

    AS规范: 编码格式——UTF-8;.java、.xml等——完成后,格式化;删除多余的资源; 命名规范 1.包名...

网友评论

    本文标题:MAVEN 与 JAVA 包命名规范

    本文链接:https://www.haomeiwen.com/subject/txlpcftx.html