Spring boot 如何在构建项目时识别 target/classes 文件夹中的静态资源?
How Spring boot recognize static resources in target/classes folder when build project?
我正在尝试构建 React 应用程序并将其作为静态资源包含在我的 spring 启动中。
我使用 maven-antrun-plugin
.
将 React 构建文件夹复制到我的 spring 引导项目的 target/classes/public
中
我尝试了两个名字,target/classes/public
和 target/classes/static
。
这是执行后我的pj/target/classes结构$mvn package
C:\Users\tlatl\Desktop\pj1\target\classes>tree /F
OS 볼륨에 대한 폴더 경로의 목록입니다.
볼륨 일련 번호는 722D-1A0E입니다.
C:.
│ application.properties
│
├─com
│ └─example
│ └─demo
│ │ DemoApplication.class
│ │
│ ├─controller
│ │ Controller.class
│ │ UrlRestController.class
│ │
│ ├─domain
│ │ Url.class
│ │
│ ├─repository
│ │ UrlRepository.class
│ │
│ └─service
│ UrlService.class
│
└─public
│ asset-manifest.json
│ favicon.ico
│ index.html
│ logo192.png
│ logo512.png
│ manifest.json
│ precache-manifest.5260fb84a466ee86d0bc9ba13f9afc09.js
│ robots.txt
│ service-worker.js
│
└─static
├─css
│ main.500ce7b4.chunk.css
│ main.500ce7b4.chunk.css.map
│
└─js
2.c878f2b2.chunk.js
2.c878f2b2.chunk.js.LICENSE
2.c878f2b2.chunk.js.map
main.1b913fda.chunk.js
main.1b913fda.chunk.js.map
runtime-main.e1737718.js
runtime-main.e1737718.js.map
但是在我执行生成的 jar 文件后,localhost:8080 只显示一个空白页。
在我提到的博客中,https://blogg.kantega.no/webapp-with-create-react-app-and-spring-boot/,作者说:
Spring Boot applications can serve static content if you put it into the classes/public directory of the application jar file.
Anything that's in the public directory on the classpath is served as a static resource by the default servlet, so you do not need to create an MVC controller for this.
所以我没有用“/”路径设置任何控制器。我设置的其他其余 API 在 URL.
中运行良好
为什么我的 spring 引导没有在 target/classes/public
文件夹中显示静态包?
下面是我的 pom.xml 文件。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<node.version>v12.3.1</node.version>
<yarn.version>v1.16.0</yarn.version>
</properties>
<dependencies>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<version>1.6</version>
<configuration>
<workingDirectory>src/main/frontend</workingDirectory>
<installDirectory>target</installDirectory>
</configuration>
<executions>
<execution>
<id>install node and npm</id>
<goals>
<goal>install-node-and-npm</goal>
</goals>
<configuration>
<nodeVersion>v8.9.4</nodeVersion>
<npmVersion>5.6.0</npmVersion>
</configuration>
</execution>
<execution>
<id>npm install</id>
<goals>
<goal>npm</goal>
</goals>
<configuration>
<arguments>install</arguments>
</configuration>
</execution>
<execution>
<id>npm run build</id>
<goals>
<goal>npm</goal>
</goals>
<configuration>
<arguments>run build</arguments>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>generate-resources</phase>
<configuration>
<target>
<copy todir="${project.build.directory}/classes/public">
<fileset dir="${project.basedir}/src/main/frontend/build"/>
</copy>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
我会 put/copy 它们在 src/resources 中,Maven 构建然后将它们复制到根文件夹
的应用程序 jar 中
1. Spring blog 说:
Spring Boot will automatically add static web resources located within
any of the following directories:
- /META-INF/resources/
- /资源/
- /静态/
- /public/
我读了同一个博客并遇到了同样的问题。只需修改 maven-antrun-plugin 中的配置:
<copy todir="src/main/resources/static">
然后就可以了
我正在尝试构建 React 应用程序并将其作为静态资源包含在我的 spring 启动中。
我使用 maven-antrun-plugin
.
target/classes/public
中
我尝试了两个名字,target/classes/public
和 target/classes/static
。
这是执行后我的pj/target/classes结构$mvn package
C:\Users\tlatl\Desktop\pj1\target\classes>tree /F
OS 볼륨에 대한 폴더 경로의 목록입니다.
볼륨 일련 번호는 722D-1A0E입니다.
C:.
│ application.properties
│
├─com
│ └─example
│ └─demo
│ │ DemoApplication.class
│ │
│ ├─controller
│ │ Controller.class
│ │ UrlRestController.class
│ │
│ ├─domain
│ │ Url.class
│ │
│ ├─repository
│ │ UrlRepository.class
│ │
│ └─service
│ UrlService.class
│
└─public
│ asset-manifest.json
│ favicon.ico
│ index.html
│ logo192.png
│ logo512.png
│ manifest.json
│ precache-manifest.5260fb84a466ee86d0bc9ba13f9afc09.js
│ robots.txt
│ service-worker.js
│
└─static
├─css
│ main.500ce7b4.chunk.css
│ main.500ce7b4.chunk.css.map
│
└─js
2.c878f2b2.chunk.js
2.c878f2b2.chunk.js.LICENSE
2.c878f2b2.chunk.js.map
main.1b913fda.chunk.js
main.1b913fda.chunk.js.map
runtime-main.e1737718.js
runtime-main.e1737718.js.map
但是在我执行生成的 jar 文件后,localhost:8080 只显示一个空白页。
在我提到的博客中,https://blogg.kantega.no/webapp-with-create-react-app-and-spring-boot/,作者说:
Spring Boot applications can serve static content if you put it into the classes/public directory of the application jar file.
Anything that's in the public directory on the classpath is served as a static resource by the default servlet, so you do not need to create an MVC controller for this.
所以我没有用“/”路径设置任何控制器。我设置的其他其余 API 在 URL.
中运行良好为什么我的 spring 引导没有在 target/classes/public
文件夹中显示静态包?
下面是我的 pom.xml 文件。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<node.version>v12.3.1</node.version>
<yarn.version>v1.16.0</yarn.version>
</properties>
<dependencies>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<version>1.6</version>
<configuration>
<workingDirectory>src/main/frontend</workingDirectory>
<installDirectory>target</installDirectory>
</configuration>
<executions>
<execution>
<id>install node and npm</id>
<goals>
<goal>install-node-and-npm</goal>
</goals>
<configuration>
<nodeVersion>v8.9.4</nodeVersion>
<npmVersion>5.6.0</npmVersion>
</configuration>
</execution>
<execution>
<id>npm install</id>
<goals>
<goal>npm</goal>
</goals>
<configuration>
<arguments>install</arguments>
</configuration>
</execution>
<execution>
<id>npm run build</id>
<goals>
<goal>npm</goal>
</goals>
<configuration>
<arguments>run build</arguments>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>generate-resources</phase>
<configuration>
<target>
<copy todir="${project.build.directory}/classes/public">
<fileset dir="${project.basedir}/src/main/frontend/build"/>
</copy>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
我会 put/copy 它们在 src/resources 中,Maven 构建然后将它们复制到根文件夹
的应用程序 jar 中1. Spring blog 说:
Spring Boot will automatically add static web resources located within any of the following directories:
- /META-INF/resources/
- /资源/
- /静态/
- /public/
我读了同一个博客并遇到了同样的问题。只需修改 maven-antrun-plugin 中的配置:
<copy todir="src/main/resources/static">
然后就可以了