认识
Maven is an attempt to apply patterns to a project’s build infrastructure in order to promote comprehension and productivity by providing a clear path in the use of best practices. Maven is essentially a project management and comprehension tool and as such provides a way to help with managing: Builds, Documentation, Reporting, Dependencies, SCMs, Releases, Distribution.
Philosophy of Maven | History of Maven
如今构建单个 Java 项目需要用到很多第三方的类库(非常之多,令人瞠目结舌)。并且 Jar 包间的关系错综复杂,某个个 Jar 包往往又会引用其他多个 Jar 包,任何缺失的 Jar 包都会导致项目编译失败。而 Maven 是个 Jar 管理工具,我们只需在配置文件(pom.xml)中进行声明,然后 Maven 会到仓库(Maven Repository)帮助我们下载所有的 Jar 包,极大降低管理难度。
官网
文档
- https://maven.apache.org/guides/index.html
- 《Maven 实战》(许晓斌)
Maven 3.6.3
- Introduction | https://maven.apache.org/ref/3.6.3/
- Settings | https://maven.apache.org/ref/3.6.3/maven-settings/settings.html
- …
仓库
组成
概念 | Fundamentals
配置 | Configuration
Maven configuration occurs at 3 levels:
Project defines information that
applies to the project, no matter who is building it.
User configuration in ~/.m2/settings.xml.
参考
Settings 文档,以了解关于 .m2/settings.xml 配置 。
针对配置的最佳实践:
Installation and
User configuration cannot be used to add shared project information – for example, setting <organization> or <distributionManagement> company-wide. For this, You should have your projects inherit from a company-wide parent pom.xml.
Installation and
User configuration both define
settings for the current environment.
常用配置,参考 Configuring Maven 文档。
MAVEN_OPTS | This variable contains parameters used to start up the JVM running Maven and can be used to supply additional options to it. E.g. JVM memory settings could be defined with the value -Xms256m -Xmx512m.
MAVEN_ARGS | Starting with Maven 3.9.0, this variable contains arguments passed to Maven before CLI arguments. E.g., options and goals could be defined with the value -B -V checkstyle:checkstyle.
.mvn/ | Located within the project’s top level directory, the files maven.config / jvm.config / extensions.xml contain project specific configuration for running Maven. This directory is part of the project and may be checked in into your version control.
.mvn/extensions.xml | 早期(up to Maven 3.2.5),Maven 通过命令行 mvn -Dmaven.ext.class.path= 或 ${MAVEN_HOME}/lib/ext 来指定 extension 的路径。现在,引入 .mvn/extensions.xml 文件,通过声明的方式来使用 extensions 而不再需要修改环境。
.mvn/maven.config | 指定需要传递给 mvn 命令的参数。Starting with Maven 3.3.1+, this can be solved by putting this options to a script but this can now simple being done by defining .mvn/maven.config file which contains the configuration options for the mvn command line.
.mvn/jvm.config | Starting with Maven 3.3.1+ you can define JVM configuration via .mvn/jvm.config file which means you can define the options for your build on a per project base. This file will become part of your project and will be checked in along with your project. So no need anymore for MAVEN_OPTS, .mavenrc files.
仓库 | Repository
This documentation is for those that need to use or contribute to the Maven central repository. This includes those that need dependencies for their own build or projects that wish to have their releases added to the Maven central repository, even if they don’t use Maven as their build tool.
Maven Central Repository | https://maven.apache.org/repository/index.html
Introduction to Repositories | https://maven.apache.org/guides/introduction/introduction-to-repositories.html
Maven Repository Metadata Model | https://maven.apache.org/ref/3.9.9/maven-repository-metadata/repository-metadata.html
构建
WIP
性质
—— 该部分将记录 Maven 工具所具备的功能及其相关特性。注意,配置 Maven 工具,例如 settings.xml 文件的配置,其属于构建可用 Maven 工具,其并不属于该部分。
Maven’s Objectives
Making the build process easy
Providing a uniform build system
Providing quality project information
Encouraging better development practices
Introduction/Maven’s Objectives
What is Maven Not?
Maven is a site and documentation tool
Maven extends Ant to let you download dependencies
Maven is a set of reusable Ant scriptlets
These are not the only features Maven has, and its objecties are quite different.
Introduction/What is Maven Not?
Feature Summary
Simple project setup that follows best practices – get a new project or module started in seconds
Consistent usage across all projects – means no ramp up time for new developers coming onto a project
Superior dependency management including automatic updating, dependency closures (also known as transitive dependencies)
Able to easily work with multiple projects at the same time
A large and growing repository of libraries and metadata to use out of the box, and arrangements in place with the largest Open Source projects for real-time availability of their latest releases
Extensible, with the ability to easily write plugins in Java or scripting languages
Instant access to new features with little or no extra configuration
Ant tasks for dependency management and deployment outside of Maven
Model based builds: Maven is able to build any number of projects into predefined output types such as a JAR, WAR, or distribution based on metadata about the project, without the need to do any scripting in most cases.
Coherent site of project information: Using the same metadata as for the build process, Maven is able to generate a web site or PDF including any documentation you care to add, and adds to that standard reports about the state of development of the project. Examples of this information can be seen at the bottom of the left-hand navigation of this site under the “Project Information” and “Project Reports” submenus.
Release management and distribution publication: Without much additional configuration, Maven will integrate with your source control system (such as Subversion or Git) and manage the release of a project based on a certain tag. It can also publish this to a distribution location for use by other projects. Maven is able to publish individual outputs such as a JAR, an archive including other dependencies and documentation, or as a source distribution.
Dependency management: Maven encourages the use of a central repository of JARs and other dependencies. Maven comes with a mechanism that your project’s clients can use to download any JARs required for building your project from a central JAR repository much like Perl’s CPAN. This allows users of Maven to reuse JARs across projects and encourages communication between projects to ensure that backward compatibility issues are dealt with.
使用方法 | Getting Started
1)安装 Maven 工具,获得 mvn 命令;
2)编写 pom.xml 文件,以定义构建行为;
3)执行 mvn package 命令,完成构建;
mvn [options] [<goal(s)>] [<phase(s)>]
1)Running Apache Maven
2)Building a Project with Maven
3)Maven in 5 Minutes
4)Maven Getting Started Guide
应用
Jenkins Pipeline Shared Libraries
我们选择 Maven 并非因为我们是 Java 开发者,而是因为:我们使用 Groovy 语言编写 Jenkins Pipeline Shared Libraries;并且,我们属于运维,需要支持 Java 项目,所以学习使用同类型的构建工具能够降低技术沟通成本。
通过 Maven 工具,实现对 Jenkins Pipeline Shared Libraries 项目的管理,并能够进行单元测试;
改进
Help Maven | https://maven.apache.org/guides/development/guide-helping.html
Develop Maven | https://maven.apache.org/developers/index.html
参考
超级详细的 Maven 使用教程_进修的 CODER 的博客-CSDN 博客_maven
maven(一) maven 到底是个啥玩意~