Maven & mvnd

Maven 几乎是所有 Java 开发者的构建工具,优点是稳定可靠,缺点是构建太慢。而 maven-mvnd 是 Apache Maven 团队借鉴了 Gradle 和 Takari(Maven 生命周期优化器)后衍生出的更快的构建工具。

一,Linux 下安装 Maven

1,下载:wget http://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.zip

2,解压:unzip apache-maven-3.6.3-bin.zip

3,配置:vim /etc/profile,追加:

export MAVEN_HOME=/home/apache-maven-3.6.3

export PATH=$PATH:$JAVA_HOME/bin:$MAVEN_HOME/bin

4,生效:source /etc/profile

5,验证:mvn -v


二,Mac 下 Maven 的安装 1

1,下载 bin.zip 格式,http://maven.apache.org/download.cgi

2,将下载后的 Maven 文件夹拷贝到某目录

3,配置环境变量:vim .zshrc

export M2_HOME=/Users/young/apache-maven-3.6.3

export PATH=$PATH:$M2_HOME/bin

4,验证:mvn -v


二,Mac 下 Maven 的安装 2

1,下载:brew install maven

2,配置文件:vim /usr/local/Cellar/maven/3.5.0/libexec/conf/settings.xml


四,Maven 的基本配置

1,修改maven仓库的存储位置,添加:<localRepository>/Users/young/repo</localRepository>

2,修改 Maven 仓库镜像:

<mirror>

<id>alimaven</id>

<name>aliyun maven</name>

<url>http://maven.aliyun.com/nexus/content/groups/public/</url>

<mirrorOf>central</mirrorOf>

</mirror>


五,Maven 的使用

1,mvn clean 删除 target 构建文件

2,mvn compile 编译项目

3,mvn install 生成 jar 包到本地仓库目录

4,mvn clean source:jar package 生成源代码

5,mvn test 跑单元测试

6,mvn clean deploy 打包后发布到私服

7,mvn site 生成文档站点

8,忽略测试和文档1:mvn clean package -D maven.test.skip=true -D maven.javadoc.skip=true

9,忽略测试和文档2:mvn clean package -DskipTests -DskipDocs(推荐)




六,Maven 项目打包后的代码与依赖分离 - 1,将 war 改成:jar

<packaging>jar</packaging>

六,Maven 项目打包后的代码与依赖分离 - 2,注释下面通用的打包工具

<plugin>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-maven-plugin</artifactId>

</plugin>

六,Maven 项目打包后的代码与依赖分离 - 3,

<plugin>

        <groupId>org.apache.maven.plugins</groupId>

        <artifactId>maven-jar-plugin</artifactId>

        <configuration>

                <archive>

                        <manifest>

                                <addClasspath>true</addClasspath>

                                <classpathPrefix>lib</classpathPrefix>

                                <mainClass>com.company.Application</mainClass>

                        </manifest>

                </archive>

        </configuration>

</plugin>


<plugin>

        <groupId>org.apache.maven.plugins</groupId>

        <artifactId>maven-dependency-plugin</artifactId>

        <executions>

                <execution>

                        <id>copy-dependencies</id>

                        <phase>prepare-package</phase>

                        <goals>

                                <goal>copy-dependencies</goal>

                        </goals>

                        <configuration>

                                <outputDirectory>${project.build.directory}/lib</outputDirectory>

                                <overWriteReleases>false</overWriteReleases>

                                <overWriteSnapshots>false</overWriteSnapshots>

                                <overWriteIfNewer>true</overWriteIfNewer>

                        </configuration>

                </execution>

        </executions>

</plugin>


六,Maven 项目打包后的代码与依赖分离 - 4,其他方法

mvn clean dependency:copy-dependencies -DoutputDirectory=target/lib package


七,mvnd 安装

1,GitHub 上的 Apache 仓库地址:https://github.com/apache/maven-mvnd/releases

2,下载后解压,然后将 bin 目录添加到 PATH

export PATH=$PATH:/home/mvnd-0.7.1/bin

3,验证:mvnd --version

4,如果 Windows 系统出现:VCRUNTIME140.dll was not found,到 https://support.microsoft.com/en-us/help/2977003/the-latest-supported-visual-c-downloads. 下载:VC_redist.x64.exe,然后安装

5,默认情况下,mvnd 使用多个 CPU 内核并行构建

6,使用:

mvnd clean package -Dquickly


八,macOS Monterey 下执行会出现:

1,授权:chmod +x /Users/yxx/mvnd-0.7.1/bin/mvnd(给执行权限)

2,运行:mvnd --version 会出现:"mvnd" cannot be opened becuase the developer cannot be verified.

3,解决:System Preferences -> Security & Privacy -> General -> Allow apps downloaded from:App Store and idenetified developers 点:Allow Anyway


九,IDEA 或 VSCode 下如何使用 mvnd ?

1,在 Terminal 中直接输入:

mvnd clean install/compile/package -Dquickly


十,Jenkins 如何配置 mvnd ?

1,创建一个自由风格的项目

2,Discard old builds -> 1 -> 1

3,配置 Git

4,构建 -> Execute shell

BUILD_ID=DONTKILLME

/bin/bash /home/abc.sh

5,在服务器上编辑:vim /home/abc.sh


Posted: 2022-02-12 10:02:18