我有嵌入式Tomcat运行的Sping Boot 应用程序。我在Docker容器中运行它。当我尝试在容器中遵循关于Spring Boot的page并构建分层映像时,我在尝试启动容器时收到了下面提到的错误。我知道最好的方法是从我的依赖项中排除旧版本的servlet-api,但这是不可能的,因为在我这样做时,此依赖项停止工作。不幸的是,我也无法摆脱这种依赖。有没有办法强制Sping Boot 使用类路径中的特定实现?我已经尝试过Jetty和Undertow,并成功启动了docker,但使用旧版本的lib无法正常工作。
另一个问题是,当我只是复制jar并启动它时,为什么它还能工作?
我尝试构建的Dockerfile:
FROM adoptopenjdk:11-jre-hotspot
ARG DEPENDENCY=target/dep
COPY ${DEPENDENCY}/BOOT-INF/lib /app/lib
COPY ${DEPENDENCY}/META-INF /app/META-INF
COPY ${DEPENDENCY}/BOOT-INF/classes /app
ENTRYPOINT ["java","-cp","app:app/lib/*","com.test.App"]
工作的停靠文件:
FROM adoptopenjdk:11-jre-hotspot
COPY /target/application.jar /app/application.jar
COPY /target/lib /app/lib
ENTRYPOINT ["java", "-jar", "app/application.jar"]
工作的方法需要pom.xml中的附加插件才能实现:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>build-info</id>
<goals>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<finalName>ttom-osm-converter</finalName>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.tomtom.mep.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
来自POM的相关性:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency> <!-- lib contains servlet-api-2.5 -->
<groupId>com.test.lib</groupId>
<artifactId>client</artifactId>
<version>${model.client.version}</version>
</dependency>
错误:
java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [NonLoginAuthenticator[StandardEngine[Tomcat].StandardHost[localhost].TomcatEmbeddedContext[/path]
***************************
APPLICATION FAILED TO START
***************************
Description:
An attempt was made to call a method that does not exist. The attempt was made from the following location:
org.apache.catalina.authenticator.AuthenticatorBase.startInternal(AuthenticatorBase.java:1220)
The following method did not exist:
'java.lang.String javax.servlet.ServletContext.getVirtualServerName()'
The method's class, javax.servlet.ServletContext, is available from the following locations:
jar:file:/app/lib/servlet-api-2.5.jar!/javax/servlet/ServletContext.class
jar:file:/app/lib/javax.servlet-api-4.0.1.jar!/javax/servlet/ServletContext.class
jar:file:/app/lib/tomcat-embed-core-9.0.29.jar!/javax/servlet/ServletContext.class
It was loaded from the following location:
file:/app/lib/servlet-api-2.5.jar
Action:
Correct the classpath of your application so that it contains a single, compatible version of javax.servlet.ServletContext
2条答案
按热度按时间a8jjtwal1#
从关于'javax.servlet.servletContext.getVirtualServerName().的错误中,我们可以看到它是在Servlet 3.1中添加的:您应该排除servlet-api:2.5。
请使用以下命令:
这将列出所有模块/依赖项,包括
servlet-api
。然后从依赖项中排除错误版本:
com.test.lib
客户端甚至不应该将其包括在第一位置,这意味着还应该在其中提供依赖性。请注意,
servlet-api
groupId会随着时间的推移而更改:这可能是Maven不选择“好的”servlet-api
的一个原因。我建议你使用maven-enforcer-plugin来锁定这些坏的依赖项:
如需详细信息,请参阅http://maven.apache.org/enforcer/enforcer-rules/bannedDependencies.html。
现在,您提到您的lib(
com.test.lib
)在没有servlet-api 2.5的情况下似乎无法工作,这意味着它使用的代码可能在Servlet 2.5和3.1之间被删除:您唯一行动是升级您的库,使其不依赖于上述代码:zd287kbt2#
您可以访问路径:
并清除文件夹serverlet。例如:
最后,您重新运行应用程序,一切正常。