Search code examples
javaspringspring-boottomcattomcat9

Cannot load static file in tomcat


Firstly, I build the vuejs project in springboot by adding the following plugin:

          <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>

I have successfully built and run it when building into a jar file. But now I want to run it with tomcat so I change the build format to war file but when running it can't load static file. enter image description here

I think it's due to context path:

  • When run in tomcat: http://localhost:8080/report_system-1.0/
  • When run jar file: http://localhost:8080

But I don't know how to fix that.


Solution

  • You need to set publicPath when building the app for different environments. Something like

    module.exports = {
      publicPath: process.env.NODE_ENV === 'production'
        ? '/report_system-1.0/'
        : '/'
    }