无法在 tomcat 中加载静态文件
Cannot load static file in tomcat
首先,我通过添加以下插件在springboot中构建vuejs项目:
<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<version>${frontend-maven-plugin.version}</version>
<configuration>
<workingDirectory>src/main/frontend</workingDirectory>
</configuration>
<executions>
<execution>
<id>install node and yarn</id>
<goals>
<goal>install-node-and-yarn</goal>
</goals>
<configuration>
<nodeVersion>${nodejs.version}</nodeVersion>
<yarnVersion>v1.22.4</yarnVersion>
</configuration>
</execution>
<execution>
<id>yarn install</id>
<goals>
<goal>yarn</goal>
</goals>
<phase>generate-resources</phase>
<configuration>
<arguments>install</arguments>
</configuration>
</execution>
<execution>
<id>yarn build</id>
<goals>
<goal>yarn</goal>
</goals>
<configuration>
<arguments>build</arguments>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-frontend</id>
<phase>generate-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>target/classes/resources/</outputDirectory>
<overwrite>true</overwrite>
<resources>
<resource>
<directory>src/main/frontend/dist</directory>
<includes>
<include>static/</include>
<include>index.html</include>
<include>favicon.ico</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
我已经成功构建并且 运行 在构建到 jar 文件时。
但是现在我想 运行 它与 tomcat 所以我将构建格式更改为 war 文件但是当 运行ning 它无法加载静态文件。
我认为这是由于上下文路径:
- 当运行在tomcat时:http://localhost:8080/report_system-1.0/
- 当运行jar文件:http://localhost:8080
但我不知道如何解决。
在为不同的环境构建应用程序时,您需要设置 publicPath
。像
module.exports = {
publicPath: process.env.NODE_ENV === 'production'
? '/report_system-1.0/'
: '/'
}
首先,我通过添加以下插件在springboot中构建vuejs项目:
<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<version>${frontend-maven-plugin.version}</version>
<configuration>
<workingDirectory>src/main/frontend</workingDirectory>
</configuration>
<executions>
<execution>
<id>install node and yarn</id>
<goals>
<goal>install-node-and-yarn</goal>
</goals>
<configuration>
<nodeVersion>${nodejs.version}</nodeVersion>
<yarnVersion>v1.22.4</yarnVersion>
</configuration>
</execution>
<execution>
<id>yarn install</id>
<goals>
<goal>yarn</goal>
</goals>
<phase>generate-resources</phase>
<configuration>
<arguments>install</arguments>
</configuration>
</execution>
<execution>
<id>yarn build</id>
<goals>
<goal>yarn</goal>
</goals>
<configuration>
<arguments>build</arguments>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-frontend</id>
<phase>generate-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>target/classes/resources/</outputDirectory>
<overwrite>true</overwrite>
<resources>
<resource>
<directory>src/main/frontend/dist</directory>
<includes>
<include>static/</include>
<include>index.html</include>
<include>favicon.ico</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
我已经成功构建并且 运行 在构建到 jar 文件时。
但是现在我想 运行 它与 tomcat 所以我将构建格式更改为 war 文件但是当 运行ning 它无法加载静态文件。
我认为这是由于上下文路径:
- 当运行在tomcat时:http://localhost:8080/report_system-1.0/
- 当运行jar文件:http://localhost:8080
但我不知道如何解决。
在为不同的环境构建应用程序时,您需要设置 publicPath
。像
module.exports = {
publicPath: process.env.NODE_ENV === 'production'
? '/report_system-1.0/'
: '/'
}