Maven

Maven详解、配置、常见问题

Posted by Sun8min on May 11, 2019

Maven-3.6.0

maven构建生命周期

参考:maven构建生命周期

maven的三个标准的生命周期

clean周期的各阶段

Phase Description
pre-clean execute processes needed prior to the actual project cleaning
clean remove all files generated by the previous build
post-clean execute processes needed to finalize the project cleaning

default周期的各阶段

Phase Description
validate validate the project is correct and all necessary information is available.
initialize initialize build state, e.g. set properties or create directories.
generate-sources generate any source code for inclusion in compilation.
process-sources process the source code, for example to filter any values.
generate-resources generate resources for inclusion in the package.
process-resources copy and process the resources into the destination directory, ready for packaging.
compile compile the source code of the project.
process-classes post-process the generated files from compilation, for example to do bytecode enhancement on Java classes.
generate-test-sources generate any test source code for inclusion in compilation.
process-test-sources process the test source code, for example to filter any values.
generate-test-resources create resources for testing.
process-test-resources copy and process the resources into the test destination directory.
test-compile compile the test source code into the test destination directory
process-test-classes post-process the generated files from test compilation, for example to do bytecode enhancement on Java classes. For Maven 2.0.5 and above.
test run tests using a suitable unit testing framework. These tests should not require the code be packaged or deployed.
prepare-package perform any operations necessary to prepare a package before the actual packaging. This often results in an unpacked, processed version of the package. (Maven 2.1 and above)
package take the compiled code and package it in its distributable format, such as a JAR.
pre-integration-test perform actions required before integration tests are executed. This may involve things such as setting up the required environment.
integration-test process and deploy the package if necessary into an environment where integration tests can be run.
post-integration-test perform actions required after integration tests have been executed. This may including cleaning up the environment.
verify run any checks to verify the package is valid and meets quality criteria.
install install the package into the local repository, for use as a dependency in other projects locally.
deploy done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.

site周期的各阶段

Phase Description
pre-site execute processes needed prior to the actual project site generation
site generate the project’s site documentation
post-site execute processes needed to finalize the site generation, and to prepare for site deployment
site-deploy deploy the generated site documentation to the specified web server

执行规则

命令行执行一个生命周期中的某个阶段,它之前的所有阶段都会被执行。 例如maven clean相当于mvn pre-clean clean

maven常用命令

## 重新打包并安装到本地maven仓库
mvn clean install
## 重新打包
mvn clean package
## 重新打包安装并跳过测试
mvn clean install -DskipTests

附: 为什么打包命令需要加clean

附:构建配置默认跳过测试

settings.xml相关

自定义本地仓库

settings.xml添加

<!--本地仓库。该值表示构建系统本地仓库的路径。其默认值为${user.home}/.m2/repository。  -->
<localRepository>/Volumes/mac_data/maven/repository</localRepository>

配置国内镜像替代中央仓库central地址

settings.xml添加

<!-- 国内镜像 -->
<mirrors>
    <mirror>
        <id>aliyun-maven</id>
        <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
        <mirrorOf>central</mirrorOf>
    </mirror>
</mirrors>

全局声明maven编译版本

settings.xml添加

<profiles>
    <profile>
        <id>development</id>
        <activation>
            <jdk>1.8</jdk>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <maven.compiler.source>1.8</maven.compiler.source>
            <maven.compiler.target>1.8</maven.compiler.target>
            <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
        </properties>
    </profile>
</profiles>

pom.xml相关

pom.xml详解

声明构建使用的java版本

pom.xml添加

<properties>
    <!-- java版本 -->
    <java.version>1.8</java.version>
    <java.source.version>${java.version}</java.source.version>
    <java.target.version>${java.version}</java.target.version>
</properties>

声明maven编码防止乱码

pom.xml添加

<properties>
    <!-- maven编码 -->
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>

构建配置默认跳过测试

pom.xml添加

<properties>
    <!-- 相当于-Dmaven.test.skip=true,表示跳过测试,与-DskipTests的区别就是skipTests如果测试类有错误会提示,而skip=true直接跳过 -->
    <maven.test.skip>true</maven.test.skip>
    <maven.javadoc.skip>true</maven.javadoc.skip>
</properties>

项目远程仓库配置

pom.xml添加

<repositories>  
  <repository>  
    <id>aliyun-maven</id>  
    <url>http://maven.aliyun.com/nexus/content/groups/public/</url>  
  </repository>  
</repositories>

springboot与pom.xml的相关

依赖管理不使用声明 parent 实现

pom.xml添加

<dependencyManagement>
    <dependencies>
        <!-- Spring Boot(pom) -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>${spring-boot.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
<dependencyManagement>

使application配置文件与pom.xml同步,能用通配符获取pom的值

pom.xml添加

<build>
    <!-- 使application配置文件与pom.xml同步,能用通配符获取pom的值 -->
    <!-- Example:application.version=@project.version@ -->
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
</build>

其他

仓库配置的读取顺序

pom文件仓库 > 中央仓库

仓库包查询

多模块版本号修改

例如将版本统一修改为指定版本 1.0.0

## 先执行该命令,将版本号改为 1.0.0
mvn versions:set -DoldVersion=* -DnewVersion=1.0.0 -DprocessAllModules=true -DallowSnapshots=true
## 然后提交
mvn versions:commit
## 或者回滚
mvn versions:revert

## 相关解释:
1. 参数 -DprocessAllModules=true 表示处理所有的子模块,即该模块下的子模块及依赖其的版本号都修改为指定版本。
2. 参数 -DallowSnapshots=true 表示允许快照版本,即允许release版修改为snapshot版。

SnapShot与Release区别

snapshot版本即使版本号一样,也会去maven仓库查看是否有更新,而且这个频率可以设置,而release同版本号只会下载一次


最终配置与基础构建文件

配置文件settings.xml

<?xml version="1.0" encoding="UTF-8"?>

<settings   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/settings-1.0.0.xsd">

    <!--本地仓库。该值表示构建系统本地仓库的路径。其默认值为${user.home}/.m2/repository。  -->
    <localRepository>/Volumes/mac_data/maven/repository</localRepository>

    <!-- 国内镜像 -->
    <mirrors>
        <mirror>
            <id>aliyun-maven</id>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
            <mirrorOf>central</mirrorOf>
        </mirror>
    </mirrors>

    <profiles>
        <profile>
            <id>downloadSourcesAndJavadocs</id>
            <properties>
                <downloadSources>true</downloadSources>
                <downloadJavadocs>true</downloadJavadocs>
            </properties>
        </profile>
        <!-- settings配置maven编译版本为1.8,默认1.5 -->
        <!-- 否则需要在pom.xml文件配置maven.compiler.source与target属性 -->
        <!-- 或者在pom.xml文件配置maven-compiler-plugin插件的source与target属性 -->
        <profile>
            <id>development</id>
            <activation>
                <jdk>1.8</jdk>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <maven.compiler.source>1.8</maven.compiler.source>
                <maven.compiler.target>1.8</maven.compiler.target>
                <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
            </properties>
        </profile>
    </profiles>

    <activeProfiles>
        <!--      <activeProfile>downloadSourcesAndJavadocs</activeProfile>-->
    </activeProfiles>
</settings>

基础构建文件pom.xml

附:pom.xml详解

<?xml version="1.0" encoding="UTF-8"?>
<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.sun8min</groupId>
    <artifactId>sun8min</artifactId>
    <version>2.0.0-RELEASE</version>

    <packaging>pom</packaging>

    <name>sun8min</name>
    <description>This is sun8min's personal project.</description>

    <developers>
        <developer>
            <name>sun8min</name>
            <email>sun8min@gmail.com</email>
            <url>http://sun8min.github.io</url>
        </developer>
    </developers>

    <properties>
        <!-- java版本 -->
        <java.version>1.8</java.version>
        <java.source.version>${java.version}</java.source.version>
        <java.target.version>${java.version}</java.target.version>
        <!-- maven编码 -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <!-- maven配置-->
        <!-- 相当于-Dmaven.test.skip=true,表示跳过测试,与-DskipTests的区别就是skipTests如果测试类有错误会提示,而skip=true直接跳过 -->
        <maven.test.skip>true</maven.test.skip>
        <maven.javadoc.skip>true</maven.javadoc.skip>
        <!-- springboot -->
        <spring-boot.version>2.1.3.RELEASE</spring-boot.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <!-- Spring Boot(pom) -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
    </dependencyManagement>

    <repositories>  
      <repository>  
        <id>aliyun-maven</id>  
        <url>http://maven.aliyun.com/nexus/content/groups/public/</url>  
      </repository>  
    </repositories>

    <build>
        <!-- 使application配置文件与pom.xml同步,能用通配符获取pom的值 -->
        <!-- Example:application.version=@project.version@ -->
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>
</project>

常见问题

项目打包后编码异常

查看项目构建文件pom.xml是否未声明编码,附:声明编码的方法

jar包下载失败

查看仓库该jar包的pom文件,parent的pom文件是否在仓库上

jar包更新失败

查看pom文件中该jar包的版本号是否未发生改变,附:SnapShot与Release区别

mirror和repository区别

mirror 配置的 mirrorOf 项为 Repository ID,被匹配到 Repository ID 的仓库地址由镜像地址代替

为什么打包命令需要加clean

如果手动修改了文件,而maven认为没有发生改动,即认为jar包是最新的,打包的时候jar包就不会被更新, 保险起见,加clean