跳到主要内容

Maven

常用 pom.xml 设置

1、设置编译级别

<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<groupId>org.apache.maven.plugins</groupId>
<version>3.8.0</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>

2、SpringBoot 打包插件

<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

mvn 常用命令

1、跳过 Test 测试

mvn clean package -DskipTests

2、镜像仓库没有指定版本的 jar

可以自行安装 jar 到本地 Maven 仓库。

mvn install:install-file\    # Maven 命令,用于将本地文件安装到本地仓库
-DgroupId=com.infiniteautomation\ # 包名
-DartifactId=modbus4j\ # 项目名
-Dversion=3.1.0\ # 版本号
-Dpackaging=jar\ # 打包方式,这里是 jar
-Dfile=modbus4j-3.1.0.jar # jar 文件所在路径

mvn install:install-file  -DgroupId=com.infiniteautomation -DartifactId=modbus4j -Dversion=3.1.0 -Dpackaging=jar -Dfile=modbus4j-3.1.0.jar  

3、下载 pom.xml 所引用的 jar 包

需要当前目录下存在 pom.xml 文件和 copy-dependencies 文件夹。

mvn dependency:copy-dependencies -DoutputDirectory=copy-dependencies

Maven 插件

1、基本介绍

maven is - at its heart - a plugin execution framework. maven 本质上是一个插件执行框架。

  • build plugins: 在 build 期间执行,在 POM 的 中进行配置
  • reporting plugins:在站点生成期间执行,在 POM 的 中进行配置

Maven 问题处理

1、IDEA 打包失败

时间:2023.01.13

关键字: maven.test.jvmargs

操作:在使用 idea maven package 的时候报错

IDEA 错误提示: 找不到或无法加载主类 ${maven.test.jvmargs}

报错信息:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.18.1:test (default-test) on project xm-scjtsjzycj: Execution default-test of goal org.apache.maven.plugins:maven-surefire-plugin:2.18.1:test failed: The forked VM terminated without properly saying goodbye. VM crash or System.exit called?
[ERROR] Command was /bin/sh -c cd /Users/wangzhy/wangzhydev/workspace_esen/yunzheng/xm-scjtsjzycj && /Library/Java/JavaVirtualMachines/jdk1.8.0_341.jdk/Contents/Home/jre/bin/java '${maven.test.jvmargs}' -javaagent:/Users/wangzhy/.m2/repository/org/jacoco/org.jacoco.agent/0.7.9/org.jacoco.agent-0.7.9-runtime.jar=destfile=/Users/wangzhy/wangzhydev/workspace_esen/yunzheng/xm-scjtsjzycj/target/coverage-reports/jacoco.exec -jar /Users/wangzhy/wangzhydev/workspace_esen/yunzheng/xm-scjtsjzycj/target/surefire/surefirebooter3619867521681928116.jar /Users/wangzhy/wangzhydev/workspace_esen/yunzheng/xm-scjtsjzycj/target/surefire/surefire262818664530513298tmp /Users/wangzhy/wangzhydev/workspace_esen/yunzheng/xm-scjtsjzycj/target/surefire/surefire_05672635147949061902tmp
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginExecutionExceptionΩΩ

解决方案:

在 pom.xml 的properties 标签添加

<maven.test.jvmargs></maven.test.jvmargs>

2、Maven 下载 jar 包一直不成功

阿里云的镜像地址有变化

<mirror>
<id>aliyun</id>
<mirrorOf>central</mirrorOf>
<name>aliyun</name>
<url>https://maven.aliyun.com/repository/central</url>
</mirror>

3、Maven 项目配置 jdk 的版本

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>17</source> <!-- JDK 版本 -->
<target>17</target> <!-- 目标 JDK 版本 -->
</configuration>
</plugin>
</plugins>
</build>

4、Maven 添加指定目录的 jar

<dependency>
<groupId>com.example</groupId>
<artifactId>example</artifactId>
<version>1.0</version>
<scope>system</scope>
<!--${project.basedir} 是 Maven 内置的一个变量,即 pom.xml 所在的目录-->
<systemPath>${project.basedir}/lib/your-jar.jar</systemPath>
</dependency>