简体   繁体   English

Spring Boot static 资源在我建的 jar 但不在 jar 部署到 Docker

[英]Spring Boot static resources are in my built jar but not in jar deployed to Docker

I wonder if the title of this post shouldn't be "I'm ignorant."不知这篇文章的标题不应该是“我无知”。 This is obviously a common problem and I've read multiple threads on it but somewhere I'm missing knowledge or... something.这显然是一个常见问题,我已经阅读了多个主题,但在某处我缺少知识或......某些东西。

  • /questions/24661289/spring-boot-not-serving-static-content /questions/24661289/spring-boot-not-serving-static-content
  • /questions/54735747/spring-boot-not-serving-static-files-from-inside-jar /questions/54735747/spring-boot-not-serving-static-files-from-inside-jar
  • /questions/63061943/how-to-load-static-files-with-spring-boot-inside-a-docker-container-hosted-on-aw /questions/63061943/how-to-load-static-files-with-spring-boot-inside-a-docker-container-hosted-on-aw

I've confirmed that my problem is that the static resources aren't making it to Docker. Using CLI, I can unzip the jar there and see that no html, js, or css files are there.我已经确认我的问题是 static 资源没有到达 Docker。使用 CLI,我可以在那里解压缩 jar 并看到那里没有 html、js 或 css 文件。

However, I've confirmed that the jar I'm using (I think I'm using it, anyway. I don't know how it couldn't be.) does have them in it.但是,我已经确认我正在使用的 jar(我想我正在使用它,无论如何。我不知道它怎么可能。)确实有它们。 It's as if they're getting deliberately extracted and excluded somehow.就好像他们被故意提取并以某种方式排除在外。

Here's my application.properties:这是我的 application.properties:

#FOR LOCAL
#spring.datasource.url=jdbc:postgresql://localhost:5432/library

#FOR DOCKER
spring.datasource.url=jdbc:postgresql://postgresqldb:5432/library

spring.datasource.username=postgres
spring.datasource.password=password
spring.datasource.driver-class-name=org.postgresql.Driver

#The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect

#Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto=create
spring.datasource.initialization-mode=always

#This enables app to serve the static content. Not sure why I need it, but it makes it work locally.
server.contextPath=/

Here's my Dockerfile这是我的 Dockerfile

FROM openjdk:8-jdk-alpine

ARG JAR_FILE=build/libs/microservice1-0.0.1-SNAPSHOT.jar
COPY ${JAR_FILE} bookws.jar

ENTRYPOINT ["java","-jar","/bookws.jar"]

And here's my docker-compose.yaml这是我的 docker-compose.yaml

version: '3.1'
services:
  app:
    container_name: bookws
    image: bookws
    build: ./
    ports:
      - "8080:8080"
    depends_on:
      - postgresqldb
  postgresqldb:
    image: postgres
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_PASSWORD=password
      - POSTGRES_USER=postgres
      - POSTGRES_DB=library

Here's my main java file:这是我的主要 java 文件:

//package and imports excluded for brevity

@SpringBootApplication
public class Microservice1Application {

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

}

My static resources are stored in src/main/resources/static我的 static 资源存放在 src/main/resources/static

static/index.html
static/js/book-crud.js
static/js/jquery-3.5.0.min.js
static/js/jquery-ui-1.12.1.min.js
static/css/main.css
static/img/ (some .pngs and some .jpgs)

EDIT: Adding my build.gradle file:编辑:添加我的 build.gradle 文件:

plugins {
    id 'org.springframework.boot' version '2.3.3.RELEASE'
    id 'io.spring.dependency-management' version '1.0.10.RELEASE'
    id 'java'
}

group = 'com.familam.learning'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-hateoas'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    compileOnly 'org.projectlombok:lombok'
    runtimeOnly 'org.postgresql:postgresql'
    annotationProcessor 'org.projectlombok:lombok'
//  annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
}

test {
    useJUnitPlatform()
}

Thanks in advance for any insights,.在此先感谢您的任何见解。 If I need to add any further info?如果我需要添加任何进一步的信息? I'm happy to.我很乐意。 What is the probably simple thing I'm missing?我缺少的可能很简单的东西是什么?

My problem was not with my configuration, but with my ignorance of how docker-compose works.我的问题不在于我的配置,而在于我对 docker-compose 的工作原理一无所知。 The docker-compose command 'up' does not re-build the docker image. docker-compose 命令“up”不会重新构建 docker 映像。 You need to run the 'docker-compose build' command first.您需要先运行“docker-compose build”命令。 That was all I was lacking.这就是我所缺乏的。

暂无
暂无

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

相关问题 已部署 spring 引导 jar 文件到 Docker - 如何访问和更改 ZA81259CEF45E959C3D9D2AC40Z 资源 - Deployed spring boot jar file to Docker - how to access and change static resources 如何动态地将静态资源添加到spring-boot jar应用程序? - How to dynamically add static resources to spring-boot jar application? Spring Boot应用程序打包到jar中后不提供静态资源 - Spring boot app does not serve static resources after packaging into jar 部署 JAR 文件后更新 spring 引导 static 资源 - Updating spring boot static resources after deploying JAR file Spring Boot Jar应用程序在无效目录中搜索静态资源 - Spring boot jar app searches for static resources in invalid directory 为什么Spring Boot会在源资源中修改jar? - Why spring boot modify jar in source resources? Spring 在 docker 上启动无法从资源中读取属性文件,但是可以在没有 Docker 的情况下使用 Z93F725A4jar423D21C28 - Spring boot on docker not able to read a property file from resources, but the can without Docker with java -jar 当应用程序部署为后台服务(jar)时,spring-boot提供静态内容 - spring-boot serve static content when app is deployed as a background service (jar) Spring Boot 部署的 JAR 在部署目录中找不到属性文件 - Spring Boot deployed JAR not finding properties file in deployed directory 我在服务器上部署了带有 jar 文件的 Spring Boot 项目,但无法访问它 - I deployed my spring boot project with jar file on my server and can't access it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM