Maven使用指南的笔记

  • 文档索引

  • Maven in 5 Minutes
    篇幅很短,快速上手,不求甚解。
    执行如下命令,创建项目的基础配置。

    mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.5 -DinteractiveMode=false

    对上述命令做一个简单的说明:

    • groupIdcom.mycompany.app
    • artifactIdmy-app

    上述命令执行完毕之后,在当前目录下创建目录my-app,执行命令tree -a my-app查看目录结构,如下:

    my-app/
    ├── .mvn
    │   ├── jvm.config
    │   └── maven.config
    ├── pom.xml
    └── src
    ├── main
    │   └── java
    │   └── com
    │   └── mycompany
    │   └── app
    │   └── App.java
    └── test
    └── java
    └── com
    └── mycompany
    └── app
    └── AppTest.java
    12 directories, 5 files
  • Maven Getting Started Guide
    完整的入门指导。
    常用的命令:

    • mvn clean,清理构建结果。
    • mvn compile,编译代码。
    • mvn test-compile,编译单元测试代码。
    • mvn test,编译代码和测试代码,运行单元测试代码。
    • mvn package,将项目打包。
    • mvn install,将项目的构建结果,安装到本地仓库内。

    父项目的pom.xml,样例如下:

    <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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mycompany.app</groupId>
    <artifactId>app</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>
    <modules>
    <module>my-app</module>
    <module>my-webapp</module>
    </modules>
    </project>

    项目的pom.xml,样例如下:

    <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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mycompany.app</groupId>
    <artifactId>my-webapp</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>
    <dependencies>
    <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
    <scope>test</scope>
    </dependency>
    </dependencies>
    </project>
  • Guide to naming conventions on groupId, artifactId, and version
    组件的groupIdartifactIdversion的命名规则。

  • Introduction to the Build Lifecycle

    使用Maven构建项目时,依次执行如下阶段:

    • validate
    • compile
    • test
    • package
    • verify
    • install
    • deploy
  • Introduction to the POM
    介绍pom.xml的组成,以及不同项目组合的定义方法。

  • Introduction to Build Profiles
    通过在settings.xml或者pom.xml中使用profile,可以为不同的构建场景定制构建过程和参数。
    执行如下命令,可以查看当前启用的profile。

    mvn help:active-profiles
  • Introduction to the Standard Directory Layout
    使用Maven来管理项目和构建时,基于约定大于配置的理念,预先定义了项目中各类文件的布局和目录命名。对于Java项目而言,默认的布局规则如下:

    • src/main/java,保存项目中的Java源码。
    • src/main/resources,保存项目中的配置文件。
    • src/main/filters,保存资源过滤器的配置文件。
    • src/main/webapp,保存Web项目的资源文件,比如html/css/js/图片比如jpeg或者png等。
    • src/test/java,保存项目中的单元测试代码。
    • src/test/resources,保存项目中单元测试代码依赖的配置文件。
    • src/test/filters,保存资源过滤器的配置文件。
    • src/assembly,保存项目构建时布局的配置文件。
    • target,保存构建的输出结果。

    其它目录不常用,因此此处不赘述。

  • Introduction to the Dependency Mechanism
    依赖管理中,相同软件的版本冲突,默认的处理规则。
    坐标中scope字段的可能取值:

    • compile,编译、运行时需要。

    • provided,编译时需要,运行时不需要。

    • runtime,编译时不需要,仅在运行时需要。

    • test,测试代码编译、测试代码运行时需要。

    • system,指定依赖本地的jar。
      样例如下:

      <dependency>
      <groupId>javax.sql</groupId>
      <artifactId>jdbc-stdext</artifactId>
      <version>2.0</version>
      <scope>system</scope>
      <systemPath>${java.home}/lib/rt.jar</systemPath>
      </dependency>
    • import,导入依赖,通常在dependencyManagement中使用,比如基于SpringBoot框架开发的应用。

    排除依赖的软件。

    <dependency>
    <groupId>group-a</groupId>
    <artifactId>artifact-a</artifactId>
    <version>1.0</version>
    <exclusions>
    <exclusion>
    <groupId>group-c</groupId>
    <artifactId>excluded-artifact</artifactId>
    </exclusion>
    </exclusions>
    </dependency>
  • Optional & Exclusion
    配置依赖时,可以增加属性optional,取值为true/false,样例如下:

    <dependency>
    <groupId>sample.ProjectA</groupId>
    <artifactId>Project-A</artifactId>
    <version>1.0</version>
    <scope>compile</scope>
    <optional>true</optional> <!-- value will be true or false only -->
    </dependency>

    在项目中没有实际使用过。

  • Settings Reference
    settings.xml的组成,各个标签的含义。
    样例模板,和关键标签,如下:

    <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd">
    <localRepository/>
    <interactiveMode/>
    <offline/>
    <pluginGroups/>
    <servers/>
    <mirrors/>
    <proxies/>
    <profiles/>
    <activeProfiles/>
    </settings>

    加载顺序:

    • ${user.home}/.m2/settings.xml
      用户目录下的settings.xml
    • ${maven.home}/conf/settings.xml
      Maven软件安装目录下的settings.xml
  • POM Reference
    pom.xml的组成,各个标签的含义。

  • FAQ

posted @   jackieathome  阅读(11)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示