Maven

Reference

Basic Concepts

Compile

Compile:将源代码转换成机器识别的机器语言的过程,就叫做编译。而对于Java而言,编译阶段会将Java源码转换成JVM识别的字节码,然后由JVM将字节码翻译成机器识别的机器码,这两个过程合并后就相当于传统编译器执行的编译过程。也正是有了JVM,Java会有良好的跨平台和可移植性。

对于java应用程序,IDE一般都会提供三个功能:CompileMakeBuild,它们三个完成的功能都是将java文件编译成class文件,但是区别还是有的。

  • Compile: 编译选中的文件,不管之前是否被编译过
  • Make: 编译选中的文件,只编译上次编译后变化过的文件。目的是节省编译时间,已经编译过的文件,但没有改动的文件就不会再重新编译一遍了。
  • Build: 对整个工程重新编译,无论它曾经是否被编译过。此外build还会将工程打包,所以build的时间是最长的。

Repositories

Build Lifecycle

There are three built-in build lifecycles:default,cleanandsite. Thedefaultlifecycle handles your project deployment, thecleanlifecycle handles project cleaning, while thesitelifecycle handles the creation of your project’s site documentation.

A Build Lifecycle is Made Up of Phases(阶段)

the default lifecycle comprises of the following phases (for a complete list of the lifecycle phases, refer to the Lifecycle Reference):

  • validate - validate the project is correct and all necessary information is available
  • compile - compile the source code of the project
  • test - test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
  • package - take the compiled code and package it in its distributable format, such as a JAR.
  • verify - run any checks on results of integration tests to ensure quality criteria are met
  • install - install the package into the local repository, for use as a dependency in other projects locally
  • deploy - done in the build environment, copies the final package to the remote repository for sharing with other developers and projects.

These lifecycle phases (plus the other lifecycle phases not shown here) are executed sequentially to complete thedefaultlifecycle. Given the lifecycle phases above, this means that when the default lifecycle is used, Maven will first validate the project, then will try to compile the sources, run those against the tests, package the binaries (e.g. jar), run integration tests against that package, verify the integration tests, install the verified package to the local repository, then deploy the installed package to a remote repository.

Usual Command Line Calls

  • mvn compile: 会在项目根目录生成 target 文件夹,这个命令并不生成jar包!target包含classesmaven-status两个文件夹,其中classes存有编译好的.class文件和src/main/resources中的资源文件,如:log4j.propertieshdfs-site.xml… 注意如果资源文件夹配置在src/main目录之外的话,需要在pom.xml文件指定resource目录才可以!maven-status存有 maven plugins 的信息。
  • mvn clean: 将根目录下的target文件夹删除
  • mvn package:target文件夹中生成jar文件和maven-archiver文件夹(存放pom.properties)
  • mvn install: 会将target中的jar包安装到local maven repository中,就是.m2/repository中。这样,本地其他项目就可以通过配置<dependency>使用这个这个项目
  • mvn deploy:target中的jar包上传到remote server(nexus)maven repository(私服),使得其他连接到这个远程库的开发者或者工程可以使用
# cleanly build and deploy artifacts into the shared repository.
$ mvn clean install

A Build Phase is Made Up of Plugin Goals

maven command

命令格式:mvn [options] [<goal(s)>] [<phase(s)>]

options

使用mvn -help查看options说明

archetype mechanism

archetype是maven的项目模版。例如通过org.scala-tools.archetypes:scala-archetype-simple模版就可以创建一个项目目录结构都已经建好的Scala的Maven项目。

settings.xml

这个文件包含了maven的各种配置,他可能出现在两个地方

  • 全局配置文件: ${MAVEN_HOME}/conf/settings.xml
  • 用户自定义配置文件: 默认在${user.home}/.m2/settings.xml 如果这两个文件同时存在,那么这两个文件中的内容会合并,并且用户自定义配置文件优先级更高。
<settings>
    <profiles>
        <profile>
            <id>xxx-repos</id>
            <repositories>
                <repository>
                    <id>xxx-snapshots</id>
                    <url>http://maven.xxx.org/repository/snapshots</url>
                </repository>
                <repository>
                    <id>xxx-releases</id>
                    <url>http://maven.xxx.org/repository/releases</url>
                </repository>
            </repositories>
        </profile>
    </profiles>

    <activeProfiles>
        <activeProfile>xxx-repos</activeProfile>
    </activeProfiles>
    <servers>
        <server>
            <id>xxx-snapshots</id>
            <username>deploy</username>
            <password>xxx123</password>
        </server>
        <server>
            <id>xxx-releases</id>
            <username>deploy</username>
            <password>xxx123</password>
        </server>
    </servers>
    <mirrors>
        <mirror>
            <id>xxx-mirror</id>
            <mirrorOf>*,!aliyun</mirrorOf>
            <url>https://maven.xxx.org/repository/public</url>
        </mirror>
        <mirror>
            <id>aliyun-mirror</id>
            <mirrorOf>aliyun</mirrorOf>
            <name>阿里云公共仓库</name>
            <url>https://maven.aliyun.com/repository/public</url>
        </mirror>
    </mirrors>
</settings>

pom.xml 解析

POM: Project Object Model,pom.xml文件中包含了所有的POM,即项目对象模型,POM是maven基本的工作单元,它包含了项目中的所有重要信息。

下面介绍几个关键的POM

  • project: pom.xml最顶级的标签,maven本质以项目(project)为中心,提供一站式服务。
  • modelVersion: 所使用POM模型的版本

dependency scope

reference

  • compile scope, all dependencies with runtime scope will be pulled in with the runtime scope, in the project and all dependencies with the compile scope will be pulled in with the compile scope, in the project
  • provided scope, both runtime and compile scope dependencies will be pulled in with the provided scope, in the project
  • test scope, both runtime and compile scope transitive dependencies will be pulled in with the test scope, in the project
  • runtime scope, both runtime and compile scope transitive dependencies will be pulled in with the runtime scope, in the project

常用命令

跳过测试

# 不执行测试用例,但编译测试用例类生成相应的class文件到target/classes下
$ mvn clean package -DskipTests
# 既不执行测试用例,也不编译测试用例类
$ mvn clean package -Dmaven.test.skip=true

查看 maven 项目依赖树

常用于查看依赖冲突,例如java.lang.NoSuchMethodError: io.netty.buffer.PooledByteBufAllocator.metric(),就是io.netty这个jar包存在conflict,可以通过输出依赖关系tree找到冲突的包

mvn dependency:tree -Dverbose > dependency.log

多 JDK 配置

生成测试报告

添加插件

<reporting>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-report-plugin</artifactId>
            <version>3.0.0-M5</version>
        </plugin>
    </plugins>
</reporting>

生成测试报告

# 以独立的方式生成测试报告到 ${basedir}/target/site 下
$ mvn surefire-report:report site -DgenerateReports=false

qin

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码支持
扫码打赏

打开支付宝扫一扫,即可进行扫码打赏哦