簡體   English   中英

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

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

不知這篇文章的標題不應該是“我無知”。 這顯然是一個常見問題,我已經閱讀了多個主題,但在某處我缺少知識或......某些東西。

  • /questions/24661289/spring-boot-not-serving-static-content
  • /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

我已經確認我的問題是 static 資源沒有到達 Docker。使用 CLI,我可以在那里解壓縮 jar 並看到那里沒有 html、js 或 css 文件。

但是,我已經確認我正在使用的 jar(我想我正在使用它,無論如何。我不知道它怎么可能。)確實有它們。 就好像他們被故意提取並以某種方式排除在外。

這是我的 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=/

這是我的 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"]

這是我的 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

這是我的主要 java 文件:

//package and imports excluded for brevity

@SpringBootApplication
public class Microservice1Application {

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

}

我的 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)

編輯:添加我的 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()
}

在此先感謝您的任何見解。 如果我需要添加任何進一步的信息? 我很樂意。 我缺少的可能很簡單的東西是什么?

我的問題不在於我的配置,而在於我對 docker-compose 的工作原理一無所知。 docker-compose 命令“up”不會重新構建 docker 映像。 您需要先運行“docker-compose build”命令。 這就是我所缺乏的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM