在 Maven 中跳过单元测试进行本地打包或排除某个项目进行打包
·
1. Maven 跳过单元测试
主要命令
1. 完全跳过测试
mvn clean package -DskipTests
2. 忽略失败的测试继续构建
mvn clean package -Dmaven.test.failure.ignore=true
参数说明
-DskipTests- 跳过执行测试,但仍会编译测试代码-Dmaven.test.skip=true- 跳过测试并跳过测试代码编译-Dmaven.test.failure.ignore=true- 执行测试但忽略测试失败,继续构建过程
推荐用法
对于本地快速打包,推荐使用:
mvn clean package -DskipTests
这个命令会:
- 清理之前的构建结果
- 编译主代码和测试代码
- 但不执行任何测试
- 完成打包过程
其他可选命令
如果同时想跳过集成测试:
mvn clean package -DskipTests -DskipITs
2. Maven 跳过或排除某个项目进行打包
多模块项目中的模块控制
1. 跳过特定模块构建
# 只构建指定模块
mvn clean install -pl module-name
# 构建多个指定模块
mvn clean install -pl module1,module2
# 构建指定模块及其依赖的模块
mvn clean install -pl module-name -am
# 构建指定模块及使用该模块的下游模块
mvn clean install -pl module-name -amd
2. 排除特定模块构建
# 排除指定模块
mvn clean install -pl !excluded-module
# 排除多个模块
mvn clean install -pl !module1,!module2
使用 Profile 控制构建
在 pom.xml 中配置 profile
<profiles>
<profile>
<id>exclude-some-modules</id>
<modules>
<module>module-a</module>
<module>module-c</module>
<!-- 不包含 module-b -->
</modules>
</profile>
</profiles>
跳过测试的组合命令
结合模块控制与测试跳过
# 排除模块并跳过测试
mvn clean package -pl !excluded-module -DskipTests
# 只构建特定模块并跳过测试
mvn clean install -pl com.ty:module-name -DskipTests
参数说明
-pl或--projects- 指定要构建的模块列表!- 排除指定模块-am或--also-make- 同时构建依赖的模块-amd或--also-make-dependents- 同时构建依赖于指定模块的下游模块-DskipTests- 跳过单元测试执行
实际应用示例
# 只构建核心服务模块
mvn clean install -pl vntg-core
# 排除测试模块构建
mvn clean install -pl !test-module
# 跳过测试并排除特定模块
mvn clean package -pl !integration-test -DskipTests
问题根源
这通常是由以下原因造成的:
- 模块依赖版本不一致:
ows-web模块可能依赖了旧版本的vntg-core - Maven 缓存问题:本地缓存的依赖包版本过旧
- 多模块构建顺序问题
推荐解决方案
1. 清理并重新构建
mvn clean compile -U
2. 强制更新依赖
mvn clean compile -U -Dmaven.repo.local=~/.m2/repository
3. 如果需要临时跳过 ows-web 模块
mvn clean compile -pl !web
4. 完整的多模块构建命令
mvn clean install -DskipTests -U
这个问题是因为 shell 将感叹号 ! 解释为历史命令的特殊字符导致的。以下是几种解决方案:
解决方案
1. 使用引号包围模块名称
mvn -T 4C clean package -pl '!web' -amd -Dmaven.test.skip=true -Ddeploy.env=dev
2. 转义感叹号
mvn -T 4C clean package -pl \!web -amd -Dmaven.test.skip=true -Ddeploy.env=dev
3. 使用双破折号参数格式
mvn -T 4C clean package --projects '!web' -amd -Dmaven.test.skip=true -Ddeploy.env=dev
4. 只构建其他模块
如果项目有明确的模块列表,可以指定构建其他模块:
# 假设有其他模块如 core、service 等
mvn -T 4C clean package -pl core,service -Dmaven.test.skip=true -Ddeploy.env=dev
usage: mvn [options] [<goal(s)>] [<phase(s)>]
Options:
-am,--also-make If project list is specified,
also build projects required by
the list
-amd,--also-make-dependents If project list is specified,
also build projects that depend
on projects on the list
-B,--batch-mode Run in non-interactive (batch)
mode (disables output color)
-b,--builder <arg> The id of the build strategy to
use
-C,--strict-checksums Fail the build if checksums don't
match
-c,--lax-checksums Warn if checksums don't match
--color <arg> Defines the color mode of the
output. Supported are 'auto',
'always', 'never'.
-cpu,--check-plugin-updates Ineffective, only kept for
backward compatibility
-D,--define <arg> Define a user property
-e,--errors Produce execution error messages
-emp,--encrypt-master-password <arg> Encrypt master security password
-ep,--encrypt-password <arg> Encrypt server password
-f,--file <arg> Force the use of an alternate POM
file (or directory with pom.xml)
-fae,--fail-at-end Only fail the build afterwards;
allow all non-impacted builds to
continue
-ff,--fail-fast Stop at first failure in
reactorized builds
-fn,--fail-never NEVER fail the build, regardless
of project result
-gs,--global-settings <arg> Alternate path for the global
settings file
-gt,--global-toolchains <arg> Alternate path for the global
toolchains file
-h,--help Display help information
-itr,--ignore-transitive-repositories If set, Maven will ignore remote
repositories introduced by
transitive dependencies.
-l,--log-file <arg> Log file where all build output
will go (disables output color)
-llr,--legacy-local-repository UNSUPPORTED: Use of this option
will make Maven invocation fail.
-N,--non-recursive Do not recurse into sub-projects
-npr,--no-plugin-registry Ineffective, only kept for
backward compatibility
-npu,--no-plugin-updates Ineffective, only kept for
backward compatibility
-nsu,--no-snapshot-updates Suppress SNAPSHOT updates
-ntp,--no-transfer-progress Do not display transfer progress
when downloading or uploading
-o,--offline Work offline
-P,--activate-profiles <arg> Comma-delimited list of profiles
to activate
-pl,--projects <arg> Comma-delimited list of specified
reactor projects to build instead
of all projects. A project can be
specified by [groupId]:artifactId
or by its relative path
-q,--quiet Quiet output - only show errors
-rf,--resume-from <arg> Resume reactor from specified
project
-s,--settings <arg> Alternate path for the user
settings file
-t,--toolchains <arg> Alternate path for the user
toolchains file
-T,--threads <arg> Thread count, for instance 4
(int) or 2C/2.5C (int/float)
where C is core multiplied
-U,--update-snapshots Forces a check for missing
releases and updated snapshots on
remote repositories
-up,--update-plugins Ineffective, only kept for
backward compatibility
-v,--version Display version information
-V,--show-version Display version information
WITHOUT stopping build
-X,--debug Produce execution debug output
推荐命令
mvn -T 4C clean package -pl '!web' -amd -Dmaven.test.skip=true -Ddeploy.env=dev
这样就能成功排除 web 模块进行构建,同时避免 shell 解析 ! 字符的问题。
最终建议
优先解决依赖版本一致性问题,而不是跳过模块构建,以确保代码完整性。
更多推荐
所有评论(0)