mvn命令跳过单元测试

1
2
3
4
5
6
7

mvn 命令加上 -DskipTests #,不执行测试用例,但编译测试用例类生成相应的class文件至target/test-classes下。

mvn 命令加上 -Dmaven.test.skip=true #,不执行测试用例,也不编译测试用例类。


其中-D的意思是 -D,--define <arg> Define a system property

执行特定的测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
mvn test -Dtest=[ClassName]


mvn -Dtest=com.mamh.aais.aais.TriggerAndroidBuildTest test

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.mamh.aais.aais.TriggerAndroidBuildTest
[ info]lastbuild file: [/dailybuild/android/sdm660/LAST_BUILD.sdm660_nougat_20170308]
last manifest file path: /dailybuild/android/sdm660/2017-08-03_sdm660_nougat_20170308/manifest.xml
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.121 sec - in com.mamh.aais.aais.TriggerAndroidBuildTest

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

使用逗号分割要测试的类

1
2
3
4
5
6

mvn -Dtest=com.mamh.aais.aais.TriggerAndroidBuildTest,com.mamh.aais.aais.AaisJenkinsTest test

# 也可以支持通配符的形式
mvn -Dtest=com.mamh.aais.aais.*Test test

使用#指定测试方法,使用*通配测试方法

1
2
3
4
5
mvn test -Dtest=[ClassName]#[MethodName] 

mvn -Dtest=com.mamh.aais.aais.TriggerAndroidBuildTest#testGetLastBuildManifestFile test


使用+号指定一个类中的多个测试方法

1
2
3
4
5
6
mvn -Dtest=com.mamh.aais.aais.AaisGitTest#testLog+testRevParse test


mvn -Dtest=com.mamh.aais.aais.AaisGitTest#testLog+testRevParse,com.mamh.aais.aais.TriggerAndroidBuildTest#testGetLastBuildManifestFile test


maven使用过程遇到的问题之案例分析 1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
https://issues.apache.org/jira/browse/MNG-6700

maven 3.6.1

[MNG-6700] - Equal compile source roots are added multiple times


mvn -DskipTests=true -DdistributionTargetDir=/tmp/maven clean package


run.sh

rm -rf /tmp/maven && mvn -DskipTests=true -DdistributionTargetDir=/tmp/maven clean package && cd /home/mamh/github/ssh-slaves-plugin && /tmp/maven/bin/mvn -DskipTests=true clean package && cd -

rm -rf /tmp/maven && mvn -DskipTests=true -DdistributionTargetDir=/tmp/maven clean package && cd /home/mamh/github/ssh-slaves-plugin && /tmp/maven/bin/mvn -DskipTests=true clean package && cd -

bash -xc 'rm -rf /tmp/maven && mvn -DskipTests=true -DdistributionTargetDir=/tmp/maven clean package && cd /home/mamh/github/ssh-slaves-plugin && /tmp/maven/bin/mvn -DskipTests=true clean package && cd -'
git bisect bad maven-3.6.1
git bisect good maven-3.6.0

git bisect run bash -xc 'rm -rf /tmp/maven && mvn -DskipTests=true -DdistributionTargetDir=/tmp/maven clean package && cd /home/mamh/github/ssh-slaves-plugin && /tmp/maven/bin/mvn -DskipTests=true clean package && cd -'

git bisect reset

maven使用过程遇到的问题之案例分析2

在编译ssh-slave的时候遇到了报错

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /home/mamh/github/ssh-slaves-plugin/target/target/generated-sources/localizer/hudson/plugins/sshslaves/Messages.java:[20,8] 类重复: hudson.plugins.sshslaves.Messages
[INFO] 1 error
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 7.996 s
[INFO] Finished at: 2019-12-11T12:59:56+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project ssh-slaves: Compilation failure
[ERROR] /home/mamh/github/ssh-slaves-plugin/target/target/generated-sources/localizer/hudson/plugins/sshslaves/Messages.java:[20,8] 类重复: hudson.plugins.sshslaves.Messages
[ERROR]
[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/MojoFailureException



通过测试发现 maven-3.6.1的版本有问题, 然后试着换了个 maven-3.6.3的版本是好的

最后通过测试脚本发现是如下的一个patch修复了这个报错, 但是还没有找到是哪个patch引入的这个问题.

1
2
* 0940c7c - [MNG-6405] Fix basedir in MavenProject.deepCopy (#225)                                                                   — Jesse Glick - (8 个月前)| 

通过如下脚本批量测试 发现修复问题的patch

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66

$ cat ../run.sh
#!/bin/bash
TAG1="maven-3.6.2"
TAG2="maven-3.6.1"
DIFF_FILE=/tmp/diff.patch
WORKSPACE="/home/mamh/github/maven"

cd $WORKSPACE
git lc ${TAG1}...${TAG2} > $DIFF_FILE
git co $TAG1

while read line
do
cd $WORKSPACE
rev=$(echo $line | awk -F " " '{print $3}')
echo "will test this rev is good or bad : $rev"
git co $rev
git ll -1

mvn -DskipTests=true -DdistributionTargetDir=/tmp/maven clean package > ../log.txt 2>&1
/tmp/maven/bin/mvn --version

cd /home/mamh/github/ssh-slaves-plugin

./test.sh
if [[ $? -eq 0 ]]
then
echo "this $rev is good $?"
else
echo "this $rev is bad $?"
fi
done < $DIFF_FILE

#mvn -DskipTests=true -DdistributionTargetDir=/tmp/maven clean package > ../log.txt 2>&1
#/tmp/maven/bin/mvn --version
#cd /home/mamh/github/ssh-slaves-plugin && ./test.sh && cd -

## 编译ssh-salve脚本如下,
#!/bin/bash -x
command="/tmp/maven/bin/mvn -DskipTests=true -s $PWD/../jenkins-core/settings-azure.xml clean package hpi:run"
log="prog.log"
match="信息: Jenkins is fully up and running"
match2="BUILD FAILURE"

$command > "$log" 2>&1 &
pid=$!

while sleep 10
do
if fgrep --quiet "$match" "$log"
then
kill $pid
exit 0
fi
if fgrep --quiet "$match2" "$log"
then

exit 1
fi
done
#应该编译jenkins插件 只有在执行mvn hpi:run的时候才报错了, 执行mvn package等不会报错,
# 但是如果执行 hpi:run 的话命令行会阻塞到那里,执行成功时候会阻塞到那里,
# 因为启动了个jenkins后台服务器,所以终端一直阻塞在那里, 所以通过上面的脚本来判断
# 是否执行成功来帮助我们排除 maven哪个patch修复了问题