Apache Maven Standard Directory Layout – Apache Maven标准目录布局

最后修改: 2017年 11月 17日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.介绍

Apache Maven is one of the most popular build tools for Java projects. Apart from just decentralizing dependencies and repositories, promoting a uniform directory structure across projects is also one of its important aspects.

Apache Maven是Java项目中最受欢迎的构建工具之一。除了分散依赖关系和存储库外,促进项目间统一的目录结构也是其重要的方面之一。

In this quick article, we’ll explore the standard directory layout of a typical Maven project.

在这篇短文中,我们将探讨一个典型的Maven项目的标准目录布局。

2. Directory Layout

2.目录布局

A typical Maven project has a pom.xml file and a directory structure based on defined conventions:

一个典型的Maven项目有一个pom.xml文件和一个基于定义惯例的目录结构。

└───maven-project
    ├───pom.xml
    ├───README.txt
    ├───NOTICE.txt
    ├───LICENSE.txt
    └───src
        ├───main
        │   ├───java
        │   ├───resources
        │   ├───filters
        │   └───webapp
        ├───test
        │   ├───java
        │   ├───resources
        │   └───filters
        ├───it
        ├───site
        └───assembly

The default directory layout can be overridden using project descriptors, but this is uncommon and discouraged.

默认的目录布局可以用项目描述符来覆盖,但这是不常见的,不鼓励这样做。

Going ahead in this article, we’ll uncover more details about each standard file and subdirectory.

在这篇文章中,我们将揭开关于每个标准文件和子目录的更多细节。

3. The Root Directory

3.根目录

This directory serves as the root of every Maven project.

该目录是每个Maven项目的根。

Let’s take a closer look at the standard files and subdirectories that are typically found at root:

让我们仔细看看通常在根目录下发现的标准文件和子目录。

  • maven-project/pom.xml – defines dependencies and modules needed during the build lifecycle of a Maven project
  • maven-project/LICENSE.txt – licensing information of the project
  • maven-project/README.txt – summary of the project
  • maven-project/NOTICE.txt – information about third-party libraries used in the project
  • maven-project/src/main – contains source code and resources that become part of the artifact
  • maven-project/src/test – holds all the test code and resources
  • maven-project/src/it – usually reserved for integration tests used by the Maven Failsafe Plugin
  • maven-project/src/site – site documentation created using the Maven Site Plugin
  • maven-project/src/assembly – assembly configuration for packaging binaries

4. The src/main Directory

4、src/main目录

As the name indicates, src/main is the most important directory of a Maven project. Anything that is supposed to be part of an artifact, be it a jar or war, should be present here.

顾名思义,src/main是Maven项目中最重要的目录。无论是jar还是war,只要是工件的一部分,都应该在这里出现。

Its subdirectories are:

它的子目录是。

  • src/main/java – Java source code for the artifact
  • src/main/resources – configuration files and others such as i18n files, per-environment configuration files, and XML configurations
  • src/main/webapp – for web applications, contains resources like JavaScript, CSS, HTML files, view templates, and images
  • src/main/filters – contains files that inject values into configuration properties in the resources folder during the build phase

5. The src/test Directory

5.src/test目录

The directory src/test is the place where tests of each component in the application reside.

目录src/test是应用程序中每个组件的测试所在的地方。

Note that none of these directories or files will become part of the artifact. Let’s see its subdirectories:

注意,这些目录或文件都不会成为工件的一部分。让我们看看它的子目录。

  • src/test/java – Java source code for tests
  • src/test/resources – configuration files and others used by tests
  • src/test/filters – contains files that inject values into configuration properties in the resources folder during the test phase

6. Conclusion

6.结论

In this article, we looked at the standard directory layout for an Apache Maven project.

本文中,我们研究了Apache Maven项目的标准目录布局。

Multiple examples of Maven project structures can be found in the GitHub project.

GitHub项目中可以找到多个Maven项目结构的例子。