我想知道什么可能导致Tomcat或本机Java ZipFile.open 方法声称文件实际上不存在?在过去一个月中,这对我的某些工作一直是一个阻碍性的问题。尝试运行tomcat7-maven- plugin时发生这种情况。它在包括我的(OSX)在内的大多数机器上都能正常工作,但在我们的构建服务器(LINUX)和我的一个同事的盒子(OSX,与我的笔记本电脑型号相同)上失败。这是在Maven构建中看到的错误:
ZipFile.open
[INFO] --- tomcat7-maven-plugin:2.2:run (start-tomcat) @ PROJECT --- [INFO] Running war on http://localhost:8080/contentmain [INFO] Using existing Tomcat server configuration at /WORKSPACE/PROJECT/tomcat7 Feb 05, 2015 11:17:53 PM org.apache.catalina.core.AprLifecycleListener init INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib Feb 05, 2015 11:17:54 PM org.apache.coyote.AbstractProtocol init INFO: Initializing ProtocolHandler ["http-bio-8080"] Feb 05, 2015 11:17:54 PM org.apache.coyote.AbstractProtocol init INFO: Initializing ProtocolHandler ["ajp-bio-8009"] Feb 05, 2015 11:17:54 PM org.apache.coyote.AbstractProtocol init INFO: Initializing ProtocolHandler ["http-bio-8443"] Feb 05, 2015 11:17:54 PM org.apache.catalina.startup.Catalina load INFO: Initialization processed in 651 ms Feb 05, 2015 11:17:54 PM org.apache.catalina.core.StandardService startInternal INFO: Starting service Catalina Feb 05, 2015 11:17:54 PM org.apache.catalina.core.StandardEngine startInternal INFO: Starting Servlet Engine: Apache Tomcat/7.0.53 Feb 05, 2015 11:17:54 PM org.apache.tomcat.util.scan.StandardJarScanner scan WARNING: Failed to scan JAR [file:/WORKSPACE/tomcat7/webapps/../../target/PROJECT/WEB-INF/lib/openws-1. 5.1.jar] from WEB-INF/lib java.io.FileNotFoundException: /WORKSPACE/PROJECT/tomcat7/webapps/../../target/PROJECT/WEB-INF/lib/openws- 1.5.1.jar (No such file or directory) at java.util.zip.ZipFile.open(Native Method) at java.util.zip.ZipFile.<init>(ZipFile.java:215) at java.util.zip.ZipFile.<init>(ZipFile.java:145) at java.util.jar.JarFile.<init>(JarFile.java:154) at java.util.jar.JarFile.<init>(JarFile.java:91) at sun.net.www.protocol.jar.URLJarFile.<init>(URLJarFile.java:93) at sun.net.www.protocol.jar.URLJarFile.getJarFile(URLJarFile.java:69) at sun.net.www.protocol.jar.JarFileFactory.get(JarFileFactory.java:99) at sun.net.www.protocol.jar.JarURLConnection.connect(JarURLConnection.java:122 ) at sun.net.www.protocol.jar.JarURLConnection.getJarFile(JarURLConnection.java: 89) at org.apache.tomcat.util.scan.FileUrlJar.<init>(FileUrlJar.java:41) at org.apache.tomcat.util.scan.JarFactory.newInstance(JarFactory.java:34) at org.apache.catalina.startup.ContextConfig$FragmentJarScannerCallback.scan(C ontextConfig.java:2612) at org.apache.tomcat.util.scan.StandardJarScanner.process(StandardJarScanner.j ava:259) at org.apache.tomcat.util.scan.StandardJarScanner.scan(StandardJarScanner.java :178) at org.apache.catalina.startup.ContextConfig.processJarsForWebFragments(Contex tConfig.java:1868) at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1256 ) at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java :873) at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java :371) at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSuppo rt.java:117) at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.jav a:90) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java :5355) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1 559) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1 549) at java.util.concurrent.FutureTask.run(FutureTask.java:262) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1 145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java: 615) at java.lang.Thread.run(Thread.java:745)
对于从Maven依赖项中提取的项目中的每个JAR文件,都会重复发生此错误。有数百种。
请注意,我ZipFile.open在一台故障机器上的呼叫处设置了一个断点,并在单独的终端中执行以下操作:
cd /WORKSPACE/PROJECT/tomcat7/webapps/../../target/PROJECT/WEB-INF/lib/ ls -la
我可以确认在open调用本机方法之前,所有“丢失的” JAR文件都已经存在,并且引发了异常声明,这些异常声明文件不存在。这使我怀疑JAR文件可能已损坏,因此我从同事的计算机中复制了一个失败的JAR文件,并与成功构建和执行成功后的本地副本进行了比较。它们是相同的,因此似乎出现了“腐败的JAR”理论。
open
我也尝试了以下方法(没有成功):
我在这里机智。我已经过了午夜好几天了,只是想让这个东西工作。还剩下什么要看?
更新(2/10/2015): 建议这是由于运行Jenkins的计算机上缺少文件许可权引起的。我没有访问发生这种情况的服务器的权限,但是我确实有一个脚本可以在其中运行:
echo Displaying JAR files with current permissions... ls -la ./target/MyProject/WEB-INF/lib/ echo Adding read, write, and execute permissions to JAR files... chmod -R 777 ./target/MyProject/WEB-INF/lib/ echo Displaying JAR files with updated permissions... ls -la ./target/MyProject/WEB-INF/lib/
产生如下输出:
[INFO] --- exec-maven-plugin:1.3.2:exec (update_jar_file_permissions) @ MyProject --- Displaying JAR files with current permissions... total 124556 drwxr-xr-x 2 jenkins users 20480 Feb 10 17:26 . drwxr-xr-x 6 jenkins users 4096 Feb 10 17:26 .. -rw-r--r-- 1 jenkins users 62983 Jan 22 00:11 activation-1.1.jar -rw-r--r-- 1 jenkins users 351656 Jan 22 00:25 amqp-client-3.1.3.jar -rw-r--r-- 1 jenkins users 74080 Jan 22 00:25 annotations-2.0.0.jar -rw-r--r-- 1 jenkins users 445288 Jan 22 00:25 antlr-2.7.7.jar -rw-r--r-- 1 jenkins users 895124 Jan 22 00:25 antlr-3.2.jar Adding read, write, and execute permissions to JAR files... Displaying JAR files with updated permissions... total 124556 drwxrwxrwx 2 jenkins users 20480 Feb 10 17:26 . drwxr-xr-x 6 jenkins users 4096 Feb 10 17:26 .. -rwxrwxrwx 1 jenkins users 62983 Jan 22 00:11 activation-1.1.jar -rwxrwxrwx 1 jenkins users 351656 Jan 22 00:25 amqp-client-3.1.3.jar -rwxrwxrwx 1 jenkins users 74080 Jan 22 00:25 annotations-2.0.0.jar -rwxrwxrwx 1 jenkins users 445288 Jan 22 00:25 antlr-2.7.7.jar -rwxrwxrwx 1 jenkins users 895124 Jan 22 00:25 antlr-3.2.jar
正如你所看到的, 是 没有权限。但是,这并没有解决问题:
Feb 10, 2015 5:27:54 PM org.apache.tomcat.util.scan.StandardJarScanner scan WARNING: Failed to scan JAR [file:/opt/jenkins/workspace/MY_JENKINS_JOB/tomcat7/webapps/../../target/MY_PROJECT/WEB-INF/lib/activation-1.1.jar] from WEB-INF/lib java.io.FileNotFoundException: /opt/jenkins/workspace/MY_JENKINS_JOB/tomcat7/webapps/../../target/MY_PROJECT/WEB-INF/lib/activation-1.1.jar (No such file or directory) at java.util.zip.ZipFile.open(Native Method)
该脚本在tomcat7-maven-plugin之前运行。权限应该已经就位,并且JAR文件应该可以解压缩。我仍然不明白为什么这不起作用。
事实证明,这根本不是权限问题。出于某种原因,将JAR文件从目录中复制出来并重新回到目录中会导致它们被拾取(我在某处的建议中阅读了此信息)。读取cp的’man’条目后,我看到它不会保留“访问控制列表(ACL)和扩展属性(EA),包括资源派生”,除非设置了- p标志(使用mv时默认情况下处于启用状态) )。我的猜测是,删除此“访问控制”信息会使tomcat7-maven- plugin可以访问这些文件。我不太清楚问题的根本原因,这似乎有些粗略,但我很高兴现在已解决。
如果有人可以明确地解释为什么这样做有效,那么我将接受它作为答案,而不是这个答案。