how to disable tomcat manifest scanning with sprin

2020-08-04 02:37发布

问题:

We have a dependency (JAR) which does contain a manifest file refering to other JARs in it's Class-Path entry. These referenced other JARs are not required for the application and thus are not added as dependencies.

When the app gets started, tomcat tries to resolve that class-path entry and you ends up with lots of errors in the log. Like:

The Class-Path manifest attribute in /Users/me/.m2/repository/org/bytedeco/javacpp-presets/openblas/0.3.3-1.4.3/openblas-0.3.3-1.4.3.jar referenced one or more files that do not exist: file:/Users/me/.m2/repository/org/bytedeco/javacpp-presets/openblas/0.3.3-1.4.3/javacpp.jar
The Class-Path manifest attribute in /Users/me/.m2/repository/org/bytedeco/javacpp-presets/mkl-platform/2019.0-1.4.3/mkl-platform-2019.0-1.4.3.jar referenced one or more files that do not exist: file:/Users/me/.m2/repository/org/bytedeco/javacpp-presets/mkl-platform/2019.0-1.4.3/mkl.jar,file:/Users/me/.m2/repository/org/bytedeco/javacpp-presets/mkl-platform/2019.0-1.4.3/mkl-linux-x86.jar,file:/Users/me/.m2/repository/org/bytedeco/javacpp-presets/mkl-platform/2019.0-1.4.3/mkl-linux-x86_64.jar,file:/Users/me/.m2/repository/org/bytedeco/javacpp-presets/mkl-platform/2019.0-1.4.3/mkl-macosx-x86_64.jar,file:/Users/me/.m2/repository/org/bytedeco/javacpp-presets/mkl-platform/2019.0-1.4.3/mkl-windows-x86.jar,file:/Users/me/.m2/repository/org/bytedeco/javacpp-presets/mkl-platform/2019.0-1.4.3/mkl-windows-x86_64.jar
The Class-Path manifest attribute in /Users/me/.m2/repository/org/bytedeco/javacpp-presets/mkl/2019.0-1.4.3/mkl-2019.0-1.4.3.jar referenced one or more files that do not exist: file:/Users/me/.m2/repository/org/bytedeco/javacpp-presets/mkl/2019.0-1.4.3/javacpp.jar
The Class-Path manifest attribute in /Users/me/.m2/repository/org/bytedeco/javacpp-presets/mkl-dnn-platform/0.16-1.4.3/mkl-dnn-platform-0.16-1.4.3.jar referenced one or more files that do not exist: file:/Users/me/.m2/repository/org/bytedeco/javacpp-presets/mkl-dnn-platform/0.16-1.4.3/mkl-dnn.jar,file:/Users/me/.m2/repository/org/bytedeco/javacpp-presets/mkl-dnn-platform/0.16-1.4.3/mkl-dnn-linux-x86_64.jar,file:/Users/me/.m2/repository/org/bytedeco/javacpp-presets/mkl-dnn-platform/0.16-1.4.3/mkl-dnn-macosx-x86_64.jar,file:/Users/me/.m2/repository/org/bytedeco/javacpp-presets/mkl-dnn-platform/0.16-1.4.3/mkl-dnn-windows-x86_64.jar
The Class-Path manifest attribute in /Users/me/.m2/repository/org/bytedeco/javacpp-presets/mkl-platform/2019.0-1.4.3/mkl-platform-2019.0-1.4.3.jar referenced one or more files that do not exist: file:/Users/me/.m2/repository/org/bytedeco/javacpp-presets/mkl-platform/2019.0-1.4.3/mkl.jar,file:/Users/me/.m2/repository/org/bytedeco/javacpp-presets/mkl-platform/2019.0-1.4.3/mkl-linux-x86.jar,file:/Users/me/.m2/repository/org/bytedeco/javacpp-presets/mkl-platform/2019.0-1.4.3/mkl-linux-x86_64.jar,file:/Users/me/.m2/repository/org/bytedeco/javacpp-presets/mkl-platform/2019.0-1.4.3/mkl-macosx-x86_64.jar,file:/Users/me/.m2/repository/org/bytedeco/javacpp-presets/mkl-platform/2019.0-1.4.3/mkl-windows-x86.jar,file:/Users/me/.m2/repository/org/bytedeco/javacpp-presets/mkl-platform/2019.0-1.4.3/mkl-windows-x86_64.jar
...
WARNING: Failed to scan [file:/Users/domi/.m2/repository/org/bytedeco/javacpp-presets/hdf5-platform/1.10.3-1.4.3/hdf5.jar] from classloader hierarchy
java.io.IOException: java.lang.reflect.InvocationTargetException
    at org.apache.tomcat.util.compat.Jre9Compat.jarFileNewInstance(Jre9Compat.java:212)
    at org.apache.tomcat.util.scan.JarFileUrlJar.<init>(JarFileUrlJar.java:65)
    at org.apache.tomcat.util.scan.JarFactory.newInstance(JarFactory.java:49)
    at org.apache.tomcat.util.scan.StandardJarScanner.process(StandardJarScanner.java:374)
    at org.apache.tomcat.util.scan.StandardJarScanner.processURLs(StandardJarScanner.java:309)
...
Caused by: java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.GeneratedConstructorAccessor78.newInstance(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
    at org.apache.tomcat.util.compat.Jre9Compat.jarFileNewInstance(Jre9Compat.java:209)
    ... 49 more
Caused by: java.nio.file.NoSuchFileException: /Users/domi/.m2/repository/org/bytedeco/javacpp-presets/hdf5-platform/1.10.3-1.4.3/hdf5.jar
    at java.base/sun.nio.fs.UnixException.translateToIOException(UnixException.java:92)
    at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:111)
    at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:116)
    at java.base/sun.nio.fs.UnixFileAttributeViews$Basic.readAttributes(UnixFileAttributeViews.java:55)
    at java.base/sun.nio.fs.UnixFileSystemProvider.readAttributes(UnixFileSystemProvider.java:145)
    at java.base/java.nio.file.Files.readAttributes(Files.java:1763)
    at java.base/java.util.zip.ZipFile$Source.get(ZipFile.java:1222)
    at java.base/java.util.zip.ZipFile$CleanableResource.<init>(ZipFile.java:726)
...

As described here, Tomcat has implemented a switch to disable the scanning of the manifest to prevent this issues: https://bz.apache.org/bugzilla/show_bug.cgi?id=59961

In a normal tomcat installation we could just do this (https://tomcat.apache.org/tomcat-8.0-doc/config/jar-scanner.html):

<Context>
  ...
  <JarScanner scanManifest="false"/>
</Context>

how to disable tomcat manifest scanning with spring boot?

回答1:

You can like bottom

@Bean
public TomcatServletWebServerFactory tomcatFactory() {
   return new TomcatServletWebServerFactory() {
      @Override
      protected void postProcessContext(Context context) {
         ((StandardJarScanner) context.getJarScanner()).setScanManifest(false);
   }};
}