在使用Jenkins自动构建Java项目时,出现以下的错误。错误日志如下: 
+ /opt/maven/apache-maven-3.5.4/bin/mvn clean package -U -e -B
-Dmaven.test.skip=true [INFO] Error stacktraces are turned on. [INFO] Scanning
for projects... [INFO]
------------------------------------------------------------------------ [INFO]
BUILD FAILURE [INFO]
------------------------------------------------------------------------ [INFO]
Total time: 0.096 s [INFO] Finished at: 2018-10-17T19:09:42+08:00 [INFO]
------------------------------------------------------------------------
[ERROR] The goal you specified requires a project to execute but there is no
POM in this directory (/root/.jenkins/workspace). Please verify you invoked
Maven from the correct directory. -> [Help 1]
org.apache.maven.lifecycle.MissingProjectException: The goal you specified
requires a project to execute but there is no POM in this directory
(/root/.jenkins/workspace/). Please verify you invoked Maven from the correct
directory. at org.apache.maven.lifecycle.internal.LifecycleStarter.execute
(LifecycleStarter.java:85) at org.apache.maven.DefaultMaven.doExecute
(DefaultMaven.java:305) at org.apache.maven.DefaultMaven.doExecute
(DefaultMaven.java:192) at org.apache.maven.DefaultMaven.execute
(DefaultMaven.java:105) at org.apache.maven.cli.MavenCli.execute
(MavenCli.java:954) at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:288)
at org.apache.maven.cli.MavenCli.main (MavenCli.java:192) at
sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method) at
sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke
(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke
(Method.java:498) at
org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced
(Launcher.java:289) at org.codehaus.plexus.classworlds.launcher.Launcher.launch
(Launcher.java:229) at
org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode
(Launcher.java:415) at org.codehaus.plexus.classworlds.launcher.Launcher.main
(Launcher.java:356) [ERROR] [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/MissingProjectException
<http://cwiki.apache.org/confluence/display/MAVEN/MissingProjectException>
Build step '执行 shell' marked build as failure SSH: Current build result is
[FAILURE], not going to run. Finished: FAILURE

去网上搜了一遍发现各种解决办法,但是没有什么作用。仔细分析了下文章中日志中的意思无非就是找不到POM文件。我去Jenkins的workspace里去查看发现下面多了一级子目录,也就是说pom文件不在workspace中的工程目录中。所以就会导致maven找不到pom文件。所以,我在构建的时候直接在构建前加一个进入工程实际目录的动作,在进行构建,就不会有问题了。

如下图:



就能直接构建成功了。这是最简便的方式。

事后分析了下造成这种情况:大概是拉取代码分支,代码分支裹了一层目录,所以拉过来就多了一级目录。