英文:
spring boot run JarLauncher when extracting jar
问题
我正在使用 Spring Boot 构建一个"fat jar",这个"fat jar"可以通过以下方式运行:
java -jar app.jar
问题是我正在使用 Docker,并且我想要展开 jar 文件的内容以提高可用性。我按照以下步骤进行解压:
unzip app.jar
现在我使用以下命令运行 jar 文件:
java -cp "." org/springframework/boot/loader/JarLauncher
我遇到了以下错误:
在上下文初始化期间遇到异常 - 取消刷新尝试: org.springframework.beans.factory.BeanCreationException: 在类路径资源中定义的名为 "jaxRsServer" 的 Bean 创建时出错
尽管我进行了配置,但它无法找到我的 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 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" />
</beans>
英文:
I'm building a fat jar using spring boot , the fat jar works using
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 :
unzip app.jar
now I run the jar with the following :
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 :
@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 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"/>
</beans>
答案1
得分: 6
以下是您提供的内容的翻译部分:
你正在使用哪个 Spring Boot 版本?从 2.3.0 版本开始,新增了一种分层模式用于打包 fat JARs(即包含所有依赖的 JAR 文件)。启用分层模式后,您可以像这样构建 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"]
请注意,命令为 java org.springframework.boot.loader.JarLauncher
。
这个示例在 Spring 博客的这篇文章中有详细解释。关于 Spring Boot 2.3 中分层工作原理的更多信息,您可以参考这篇文章,同样来自 Spring 博客。
另外,如果您正在使用旧版本的 Spring Boot,您可以根据这篇文章的说明构建 Dockerfile。在这种情况下,入口点将是 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"]
英文:
Which Spring Boot version are you using? Since 2.3.0, a new layered mode is available for packaging fat JARs. Once you enable the layered mode, you can then do something like this to build the Docker image.
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
.
The example is fully explained in this post from the Spring blog.
For more information on how layering works in Spring Boot 2.3, you can refer to this post, again from the Spring blog.
Instead, if you're using an older version of Spring Boot, you can build your Dockerfile as explained in this post. In this case, the entrypoint will be 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"]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论