Maven随记

如何保持依赖的多个jar保持版本一致

在引入依赖的时候常常需要依赖多个独立的模块, 譬如Spring的content, aop等等, 为了保持版本一致, 可以设置<spring.version>属性, 之后便可以复用该属性, 从而保持多个模块的版本号一致

    <properties>
        <lucene.version>4.10.2</lucene.version>
        <poi.version>3.9</poi.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.lucene</groupId>
            <artifactId>lucene-core</artifactId>
            <version>${lucene.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.lucene</groupId>
            <artifactId>lucene-queryparser</artifactId>
            <version>${lucene.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.lucene</groupId>
            <artifactId>lucene-analyzers-common</artifactId>
            <version>${lucene.version}</version>
        </dependency>
        </dependencies>

Maven的默认编译级别是java 1.5, 因此需要设置默认的编译级别需要以下配置

<build>
        <finalName>north</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>

传递依赖管理 -- >"nearest definition"

"nearest definition" means that the version used will be the closest one to your project in the tree of dependencies, eg. if dependencies for A, B, and C are defined as A -> B -> C -> D 2.0 and A -> E -> D 1.0, then D 1.0 will be used when building A because the path from A to D through E is shorter. You could explicitly add a dependency to D 2.0 in A to force the use of D 2.0

编译时打包WEB-INF/lib目录

可以在maven-complier-plugin下添加如下配置

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.1</version>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
        <encoding>UTF-8</encoding>
        <compilerArguments>
            <extdirs>srcmainwebappWEB-INFlib</extdirs>
        </compilerArguments>
    </configuration>
</plugin>

 新增阿里云Maven中央仓库镜像

<mirror>
    <id>nexus-aliyun</id>
    <mirrorOf>*</mirrorOf>
    <name>Nexus aliyun</name
    <url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror> 

Maven项目构建的生命周期

一个Maven项目的构建过程就像人从童年, 青年再到中年一样有自己的生命周期, 以下我们直接称为lifecycle.

Maven项目的构建包含三个生命周期:  clean  default 和 site . 每一个lifecycle是由一组连续的阶段组成的, 例如 default 生命周期为包含以下阶段:

  • validate - 校验
  • compile - 编译
  • test - 单元测试
  • package - 打包
  • verify - 集成测试
  • install - 安装到本地仓库
  • deploy - 安装到远成仓库

每个生命周期内的阶段都是按照一定的顺序执行的, 执行install阶段时, 总会依次执行validate, complier, test, package, verify阶段. 我么可以直接在命令行里面直接执行构建生命周期的特定阶段, 例如

#直接执行到install阶段
mvn install
#先执行到clean阶段 再从validate执行到install阶段
mvn clean install

每个阶段的列表可以参加Maven Lifecycle.

插件目标goal

Maven项目的构建周期是由一组阶段组成的, 然而每个阶段任务的实现方式可能是不一样的, 因此一个阶段(phase)可以更进一步由插件目标(goals)组成. 一个插件目标可以绑定到0个或多个phase. 在不绑定到任何phase的情况下, 插件目标可以单独执行, 例如以下命令:

mvn clean dependency:copy-dependencies package
原文地址:https://www.cnblogs.com/ayning/p/4181719.html