I would rather go solution where I configure a WebMvcConfigurer
instead. Please see below for code.
Maven Configuration
Maven configuration directs frontend-maven-plugin
to build angular and copy built content into target/static
directory, so that it is available in final WAR and JAR built. Then I additionally configure a WebMvcConfigurer
to find a resource in static directory of JAR / WAR, if found - send it to client, otherwise redirect to index.html.
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>${maven-clean-plugin.version}</version>
<configuration>
<filesets>
<fileset>
<directory>src/main/webapp</directory>
<includes>
<include>*.js</include>
<include>*.txt</include>
<include>*.html</include>
<include>*.css</include>
<include>*.map</include>
<include>*.json</include>
<include>*.ico</include>
<followSymlinks>false</followSymlinks>
</includes>
</fileset>
<fileset>
<directory>src/main/webapp/assets</directory>
<includes>
<include>**</include>
<followSymlinks>false</followSymlinks>
</includes>
</fileset>
</filesets>
</configuration>
<executions>
<execution>
<id>auto-clean</id>
<phase>initialize</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-resources</id>
<phase>process-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/classes/static</outputDirectory>
<includeEmptyDirs>true</includeEmptyDirs>
<resources>
<resource>
<directory>${basedir}/src/main/${angular.project.name}/dist/${angular.project.name}</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<version>1.8.0</version>
<configuration>
<workingDirectory>src/main/${angular.project.name}</workingDirectory>
</configuration>
<executions>
<execution>
<id>install node and npm</id>
<goals>
<goal>install-node-and-npm</goal>
</goals>
<phase>generate-resources</phase>
<configuration>
<nodeVersion>v14.16.1</nodeVersion>
</configuration>
</execution>
<execution>
<id>npm build angular</id>
<goals>
<goal>npm</goal>
</goals>
<phase>generate-resources</phase>
<configuration>
<arguments>run build:prod</arguments>
</configuration>
</execution>
</executions>
</plugin>
MVC Configuration
Here you try to find if a resource exists in your static directory, if not redirect it to index.html.
You may have to change source location as per your requirement.
@Configuration
public class ApplicationWebMvcConfiguration implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**")
.addResourceLocations("classpath:/static/")
.resourceChain(true)
.addResolver(new PathResourceResolver() {
@Override
protected Resource getResource(String resourcePath, Resource location)
throws IOException {
Resource requestedResource = location.createRelative(resourcePath);
if (requestedResource.exists() && requestedResource.isReadable()) {
return requestedResource;
}
return new ClassPathResource("/static/index.html");
}
});
}
}