简体   繁体   English

提取 jar 时,spring boot 运行 JarLauncher

[英]spring boot run JarLauncher when extracting jar

I'm building a fat jar using spring boot , the fat jar works using我正在使用 spring boot 构建一个胖罐子,胖罐子使用

java -jar app.jar

the problem is that I'm using docker and I want to expand the jar contents for better usability , I do the following to extract :问题是我正在使用 docker 并且我想扩展 jar 内容以获得更好的可用性,我执行以下操作来提取:

unzip app.jar

now I run the jar with the following :现在我使用以下命令运行 jar:

java -cp "." org/springframework/boot/loader/JarLauncher

I get the following error :我收到以下错误:

 Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jaxRsServer' defined in class path resource 

so it couldn't find my bean altough it's configured :所以它虽然配置了但找不到我的bean:

@SpringBootApplication(exclude = SecurityAutoConfiguration.class)
@ImportResource("classpath:spring/beans_context.xml")
public class SpringBootJaxrsApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootJaxrsApplication.class, args);
    }

}

beans_context.xml : beans_context.xml :

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
          http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config />


<import resource="classpath:META-INF/spring/*_context.xml"/>

Which Spring Boot version are you using?您使用的是哪个 Spring Boot 版本? Since 2.3.0, a new layered mode is available for packaging fat JARs.从 2.3.0 开始,可以使用新的分层模式来打包胖 JAR。 Once you enable the layered mode, you can then do something like this to build the Docker image.启用分层模式后,您可以执行类似操作来构建 Docker 映像。

FROM adoptopenjdk:11-jre-hotspot as builder
WORKDIR application
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} application.jar
RUN java -Djarmode=layertools -jar application.jar extract

FROM adoptopenjdk:11-jre-hotspot
WORKDIR application
COPY --from=builder application/dependencies/ ./
COPY --from=builder application/snapshot-dependencies/ ./
COPY --from=builder application/resources/ ./
COPY --from=builder application/application/ ./
ENTRYPOINT ["java", "org.springframework.boot.loader.JarLauncher"]

Notice that the command is java org.springframework.boot.loader.JarLauncher .请注意,该命令是java org.springframework.boot.loader.JarLauncher

The example is fully explained in this post from the Spring blog.该示例在 Spring 博客的这篇文章中得到了完整的解释。 For more information on how layering works in Spring Boot 2.3, you can refer to this post , again from the Spring blog.有关 Spring Boot 2.3 中分层如何工作的更多信息,您可以参考这篇文章,同样来自 Spring 博客。

Instead, if you're using an older version of Spring Boot, you can build your Dockerfile as explained in this post .相反,如果你使用Spring启动的旧版本,你可以建立自己的Dockerfile在此解释 In this case, the entrypoint will be java -cp app:app/lib/* hello.Application .在这种情况下,入口点将是java -cp app:app/lib/* hello.Application

FROM openjdk:8-jdk-alpine
RUN addgroup -S spring && adduser -S spring -G spring
USER spring:spring
ARG DEPENDENCY=target/dependency
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/*","hello.Application"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM