MAVEN and JAVA package naming convention
throw questions
When building a modular project with MAVEN, my organizational structure is as follows:
- root module
Folder name: package-module-project
pom.xml
document:
<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>
- submodule
2.1 Core
module Folder name: chuillusionCore
pom.xml
document:
<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 package name: com.chuillusion.cores is the root package
There is a problem
- The project folder naming is inconsistent with the artifactId in maven
The root module project is named package-module-project
, and the artifactId corresponding to root is namedchuillusion-package
- java package naming inconsistent with maven
The java root package in the core module is named: com.chuillusion.cores
, and the artifactId in the core project is namedchuillusion.core
- idea shows inconsistency
When the project name is inconsistent with the artifactId, idea will display the artifactId in the project name
Such as: chuillusionCore[chuillusion.core]
, namely:项目名[artifactId]
Discussion on naming rules
- Official website description
Refer to the naming convention in the official MAVEN documentation
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
- Case study with driver package
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.43</version>
</dependency>
The generated package name is: mysql:mysql-connector-java-5.1.43.jar
, which isgroupId:artifactId-version.jar
Source code structure: com.mysql as the project root package
Question: Personal feeling is not named according to the norm
- assertj analysis
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.8.0</version>
</dependency>
Source structure: org.assertj.core as the root package
- logback analysis
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.3.0-alpha3</version>
<scope>test</scope>
</dependency>
Source structure: ch.qos.logback.classic as the root package
- in conclusion
1) The source package needs to start with groupId, followed by artifactId as the root package
canonical naming
Develop good coding habits, starting with naming conventions
Change project name
The project name corresponds to the artifactId, and the source code directory corresponds to the overall structure
- root module
Project name: 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>
The root project has an empty structure, only one pom file is responsible for managing submodules, so there is no source directory structure
- core module modification
Modification one:
Project name: 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>
Source root directory structure:com.chuillusion.core
Modification method two
Project name: 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>
Source root directory structure:com.chuillusion.core
illustrate
This article is written because 1) the problems encountered in the project; 2) there are no related articles on baidu
Welcome to leave a message to correct the mistakes of the article, thank you!