Back to Build_Automation

See Also Java

Maven

Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project's build, reporting and documentation from a central piece of information.

1. Quick Overview

1.1. What is Maven?

Maven本质上是一个项目管理和综合工具,提供了统一的方法来帮助管理:

更多背景知识可以参考Philosophy of Maven

1.2. How can Maven benefit my development process?

Maven通过把标准约定和实践应用于构建过程,使用户在加快开发周期的同时获取得更高的成功。更详细的内容参阅使用Maven的好处

1.3. Core Conceptions

1.3.1. Maven Archetypes

Maven Archetypes: 定义maven初试项目模板

2. Getting Started with Maven

2.1. The Build Lifecycle

参考Introduction to the Build Lifecycle

There are three built-in build lifecycles:

The default lifecycle has the following build phases:

因为存在依赖关系和执行顺序,指定执行某个阶段时会自动从第一个阶段开始执行到当前阶段

A Build Phase is Made Up of Plugin Goals

2.2. The POM

参考POM Reference

2.2.1. What is a POM?

参考http://maven.apache.org/guides/introduction/introduction-to-the-pom.html

项目对象模型(Project Object Model)是Maven中的基本工作单位。它是一个XML文件,其中包含用来构建Maven项目的信息和配置细节。它包含了大多数项目的默认值。

2.2.2. Super POM

Super POM是Maven的默认POM。所有的POM都扩展自Super POM,除非显式地设置,否则一切取值来自于Super POM。

http://maven.apache.org/ref/3.2.5/maven-model-builder/super-pom.html

2.2.3. Minimal POM

参考http://maven.apache.org/guides/introduction/introduction-to-the-pom.html#Minimal_POM

The minimum requirement for a POM are the following:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.li3huo.server</groupId>
  <artifactId>server-project</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>
  <name>server-project</name>
</project>

2.2.4. The key elements in POM

2.2.5. Project Inheritance vs Project Aggregation

参考http://maven.apache.org/guides/introduction/introduction-to-the-pom.html#Project_Inheritance_vs_Project_Aggregation

2.2.6. Project Interpolation and Variables

参考

...
        <properties>
                <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
                <jmeter.lib.ext>/opt/app/java/jmeter/apache-jmeter-2.9/lib/ext</jmeter.lib.ext>
        </properties>

        <dependencies>
        ...
                <outputDirectory>${jmeter.lib.ext}</outputDirectory>
...

2.3. Standard Directory Layout

参考Introduction to the Standard Directory Layout

src/main/java

Application/Library sources

src/main/resources

Application/Library resources

src/main/filters

Resource filter files

src/main/config

Configuration files

src/main/scripts

Application/Library scripts

src/main/webapp

Web application sources

src/test/java

Test sources

src/test/resources

Test resources

src/test/filters

Test resource filter files

src/it

Integration Tests (primarily for plugins)

src/assembly

Assembly descriptors

src/site

Site

LICENSE.txt

Project's license

NOTICE.txt

Notices and attributions required by libraries that the project depends on

README.txt

Project's readme

2.4. Configure

Maven configuration occurs at 3 levels:

3. Plugins Sample

参考Available Plugins

3.1. Dependency Graph

mvn dependency:tree

                        <plugin>
                                <!--
                                http://www.summa-tech.com/blog/2011/04/12/a-visual-maven-dependency-tree-view/
                                {{{
                                        mvn dependency:tree -DoutputType=graphml -DoutputFile=dependency.graphml
                                }}}
                                and open dependency.graphml with yED(http://www.yworks.com/products/yed/demo/yEd-3.11.1.dmg)
                                yED configuration
                                {{{
                                        Tools > Fit Note to Label … > OK
                                        Layout > Hierarchical > Orientation > Left to Right > OK
                                }}}
                                 -->
                                <groupId>org.apache.maven.plugins</groupId>
                                <artifactId>maven-dependency-plugin</artifactId>
                                <version>2.8</version>
                        </plugin>

Anothing plgin:http://code.google.com/p/maven-overview-plugin/

                        <plugin>
                                <!-- 
                                        http://code.google.com/p/maven-overview-plugin/
                                        {{{
                                                mvn com.googlecode.maven-overview-plugin:maven-overview-plugin:1.6:overview
                                                or
                                                mvn overview:overview
                                        }}}
                                 -->
                                <groupId>com.googlecode.maven-overview-plugin</groupId>
                                <artifactId>maven-overview-plugin</artifactId>
                                <version>1.6</version>
                        </plugin>

target/site/images/overview.png

3.2. Package

http://www.infoq.com/cn/news/2011/06/xxb-maven-9-package

3.3. Mark the Build Number

http://www.mojohaus.org/buildnumber-maven-plugin/usage.html This plugin works in one of 3 ways: with an SCM(svn/git), with a sequential build number, or with a timestamp.

https://github.com/twotwo/framework-java/tree/master/mvn_build_number

3.4. Build a Uber Jar

Uber-JAR就是把整个工程都放到一个jar中的行业叫法。实现方式很多,推荐使用Maven-Shade-Plugin方式。

https://github.com/twotwo/framework-java/tree/master/mvn_uber_jar

3.5. Enable Compiler Warnings

http://frequal.com/java/EnableWarningsInMaven.html

https://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html

<project>
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <compilerArgument>-Xlint:all</compilerArgument>
          <showWarnings>true</showWarnings>
          <showDeprecation>true</showDeprecation>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

4. Maven Index

5. Reference


CategoryTool

MainWiki: Maven (last edited 2016-08-17 16:36:40 by twotwo)