Maven 简明教程
Maven - Overview
What is Maven?
Maven 是一种项目管理和理解工具,为开发人员提供了完整的构建生命周期框架。开发团队几乎可以在很短的时间内自动执行项目的构建基础设施,因为 Maven 使用了标准目录布局和默认构建生命周期。
Maven is a project management and comprehension tool that provides developers a complete build lifecycle framework. Development team can automate the project’s build infrastructure in almost no time as Maven uses a standard directory layout and a default build lifecycle.
在涉及多个开发团队的环境中,Maven 可以迅速建立按照标准工作的方式。由于大部分项目设置简单可重复利用,Maven 在创建报告、检查、构建和测试自动化设置时,简化了开发人员的工作。
In case of multiple development teams environment, Maven can set-up the way to work as per standards in a very short time. As most of the project setups are simple and reusable, Maven makes life of developer easy while creating reports, checks, build and testing automation setups.
Maven 为开发人员提供了管理以下内容的方式:
Maven provides developers ways to manage the following −
-
Builds
-
Documentation
-
Reporting
-
Dependencies
-
SCMs
-
Releases
-
Distribution
-
Mailing list
总结来说,Maven 简化并标准化了项目构建过程。它无缝处理编译、分发、文档编制、团队协作及其他任务。Maven 提高了可重复利用性,并负责大部分与构建相关的任务。
To summarize, Maven simplifies and standardizes the project build process. It handles compilation, distribution, documentation, team collaboration and other tasks seamlessly. Maven increases reusability and takes care of most of the build related tasks.
Maven Evolution
Maven 最初设计目的是简化 Jakarta Turbine 项目中的构建流程。它包含多个项目,每个项目都包含稍有不同的 ANT 构建文件。JAR 已签入 CVS。
Maven was originally designed to simplify building processes in Jakarta Turbine project. There were several projects and each project contained slightly different ANT build files. JARs were checked into CVS.
Apache 小组随后开发了 Maven ,它可以同时构建多个项目,发布项目信息,部署项目,在多个项目中共享 JAR,并协助团队协作。
Apache group then developed Maven which can build multiple projects together, publish projects information, deploy projects, share JARs across several projects and help in collaboration of teams.
Objective
Maven 的主要目标是为开发人员提供以下内容:
The primary goal of Maven is to provide developer with the following −
-
A comprehensive model for projects, which is reusable, maintainable, and easier to comprehend.
-
Plugins or tools that interact with this declarative model.
Maven 项目结构和内容在 XML 文件 pom.xml 中声明,该文件称为项目对象模型 (POM),它是整个 Maven 系统的基本单元。在后续章节中,我们将详细解释 POM。
Maven project structure and contents are declared in an xml file, pom.xml, referred as Project Object Model (POM), which is the fundamental unit of the entire Maven system. In later chapters, we will explain POM in detail.
Convention over Configuration
Maven 在 Convention 上使用 Configuration ,这意味着开发人员无需自己创建构建流程。
Maven uses Convention over Configuration, which means developers are not required to create build process themselves.
开发人员不必提及每个配置详情。Maven 为项目提供了可行的默认行为。当创建 Maven 项目时,Maven 将创建默认项目结构。开发人员只需要相应放置文件,而无需在 pom.xml 中定义任何配置。
Developers do not have to mention each and every configuration detail. Maven provides sensible default behavior for projects. When a Maven project is created, Maven creates default project structure. Developer is only required to place files accordingly and he/she need not to define any configuration in pom.xml.
例如,下一表格显示了项目源代码文件、资源文件和其他配置的默认值。假设 ${basedir} 表示项目位置 -
As an example, following table shows the default values for project source code files, resource files and other configurations. Assuming, ${basedir} denotes the project location −
Item |
Default |
source code |
${basedir}/src/main/java |
Resources |
${basedir}/src/main/resources |
Tests |
${basedir}/src/test |
Complied byte code |
${basedir}/target |
distributable JAR |
${basedir}/target/classes |
为了构建项目,Maven 为开发人员提供了提及生命周期目标和项目依赖项的选项(依赖于 Maven 插件功能及其默认约定)。大部分项目管理和构建相关任务均由 Maven 插件维护。
In order to build the project, Maven provides developers with options to mention life-cycle goals and project dependencies (that rely on Maven plugin capabilities and on its default conventions). Much of the project management and build related tasks are maintained by Maven plugins.
开发人员可以构建任何 Maven 项目,而无需理解各个插件的工作方式。我们在后续章节中将详细讨论 Maven 插件。
Developers can build any given Maven project without the need to understand how the individual plugins work. We will discuss Maven Plugins in detail in the later chapters.
Features of Maven
-
Simple project setup that follows best practices.
-
Consistent usage across all projects.
-
Dependency management including automatic updating.
-
A large and growing repository of libraries.
-
Extensible, with the ability to easily write plugins in Java or scripting languages.
-
Instant access to new features with little or no extra configuration.
-
Model-based builds − Maven is able to build any number of projects into predefined output types such as jar, war, metadata.
-
Coherent site of project information − Using the same metadata as per the build process, maven is able to generate a website and a PDF including complete documentation.
-
Release management and distribution publication − Without additional configuration, maven will integrate with your source control system such as CVS and manages the release of a project.
-
Backward Compatibility − You can easily port the multiple modules of a project into Maven 3 from older versions of Maven. It can support the older versions also.
-
Automatic parent versioning − No need to specify the parent in the sub module for maintenance.
-
Parallel builds − It analyzes the project dependency graph and enables you to build schedule modules in parallel. Using this, you can achieve the performance improvements of 20-50%.
-
Better Error and Integrity Reporting − Maven improved error reporting, and it provides you with a link to the Maven wiki page where you will get full description of the error.