Spring 引导,gradle,angularJS 和一个胖罐子
Spring boot, gradle, angularJS and a fat jar
我的spring-boot工程是这样的:
project
|__ build.gradle
|__ settings.gradle
|__ application
|__ library
|__ web
|__ application
|__ library
|__ web
应用程序包含主要的和实现 WebMvcConfigurer 的 MvcConfig class:
@Configuration
public class MvcConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers (ResourceHandlerRegistry registry) {
String workingDirectory = System.getProperty("user.dir");
registry.addResourceHandler("/**")
.addResourceLocations("file:///" + workingDirectory + "/../web/dist/");
}
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("forward:/index.html");
}
}
当我 运行 : gradlew :application:bootRun => 一切正常。
我的 Web 目录是一个 angularJS 应用程序,gulp 在 dist 目录中构建它。 (我正在使用 moowork 节点和 gulp gradle 插件)
但现在我想把我所有的应用程序都放在一个用 bootJar 生成的 fat jar 中。
java -jar application.jar (will run all the app)
application.jar 包含 web.jar 其中包含 dist 目录中的所有内容。
gradlew :application:bootJar 生成一个包含所有内容(库、spring-boot web)的 jar,但是当我启动它时,它找不到我的 index.html
我知道我必须更改 addResourceLocations 参数,但我需要一些帮助才能完成。
我试过了(没有成功):
.addResourceLocations("classpath:/web/");
好的
答案很简单:
由于 web.jar 包含 dist 中的所有内容,我只需要这样做:
.addResourceLocations("classpath:/");
我的spring-boot工程是这样的:
project
|__ build.gradle
|__ settings.gradle
|__ application
|__ library
|__ web
|__ application
|__ library
|__ web
应用程序包含主要的和实现 WebMvcConfigurer 的 MvcConfig class:
@Configuration
public class MvcConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers (ResourceHandlerRegistry registry) {
String workingDirectory = System.getProperty("user.dir");
registry.addResourceHandler("/**")
.addResourceLocations("file:///" + workingDirectory + "/../web/dist/");
}
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("forward:/index.html");
}
}
当我 运行 : gradlew :application:bootRun => 一切正常。
我的 Web 目录是一个 angularJS 应用程序,gulp 在 dist 目录中构建它。 (我正在使用 moowork 节点和 gulp gradle 插件)
但现在我想把我所有的应用程序都放在一个用 bootJar 生成的 fat jar 中。
java -jar application.jar (will run all the app)
application.jar 包含 web.jar 其中包含 dist 目录中的所有内容。
gradlew :application:bootJar 生成一个包含所有内容(库、spring-boot web)的 jar,但是当我启动它时,它找不到我的 index.html
我知道我必须更改 addResourceLocations 参数,但我需要一些帮助才能完成。 我试过了(没有成功):
.addResourceLocations("classpath:/web/");
好的 答案很简单: 由于 web.jar 包含 dist 中的所有内容,我只需要这样做:
.addResourceLocations("classpath:/");