有几种方法和工具可以实现您想要实现的目标。
我将建议一种使用我在类似项目中获得的一些工具的方法。虽然这个答案可能看起来是以意见为导向的,但它可能会给你一些想法,让你开始并做出自己的选择。
如果下面描述的任何步骤需要更多详细信息,请告诉我,我会更新它们。
如何打包,使用 Maven?
您需要做的第一件事是将您的项目分成至少两个模块:
- 带有 REST 服务的战争模块
- 带有静态文件的客户端模块。
要构建客户端模块,您可以使用Gulp。它将帮助您管理静态 JS/HTML/CSS 文件上的简单任务。
您可以使用 frontend-maven-plugin by com.github.eirslett 启动 gulp 任务:
<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<id>install node and npm</id>
<goals>
<goal>install-node-and-npm</goal>
</goals>
<configuration>
<nodeVersion>v6.9.1</nodeVersion>
<npmVersion>3.10.9</npmVersion>
</configuration>
</execution>
<execution>
<id>npm install</id>
<goals>
<goal>npm</goal>
</goals>
<configuration>
<arguments>install</arguments>
</configuration>
</execution>
<execution>
<id>npm rebuild node-sass</id>
<goals>
<goal>npm</goal>
</goals>
<configuration>
<arguments>rebuild node-sass</arguments>
</configuration>
</execution>
<execution>
<id>gulp build</id>
<goals>
<goal>gulp</goal>
</goals>
<configuration>
<arguments>build --no-notification</arguments>
</configuration>
</execution>
<execution>
<id>gulp test</id>
<phase>test</phase>
<goals>
<goal>gulp</goal>
</goals>
<configuration>
<arguments>test - - no-notification</arguments>
</configuration>
</execution>
</executions>
</plugin>
在本例中,插件启动所需的步骤:
- npm install -> gulp 需要
- gulp build -> 你的客户端模块构建
- gulp 测试 -> 客户端单元测试
然后你需要一个 gulpfile 在你的模块的根目录。
gulp 现在需要做的最少的事情就是将其余服务后端的 URL 提供给您的客户端应用程序。您正在使用角度,所以我建议您使用gulp-ng-constant 在构建阶段动态修改角度常数:
const ngConstant = require('gulp-ng-constant');
gulp.task('build', gulp.series('ngconstant:prod', gulp.parallel('other', 'webpack:dist')));
gulp.task('ngconstant:prod', function () {
return ngConstant({
name: 'yourApp',
constants: {
BASE_URL : 'http://www.exemple.com:8080'
},
template: conf.constantTemplate,
stream: true
})
.pipe(rename('app.constants.js'))
.pipe(gulp.dest(conf.app + 'app/'));
});
在上面的 gulpfile 中,webpack 用于进行实际的构建。
如何使用 Tomcat 部署和启动它?
- localhost:8080:application/ 用于 HTML/JS 部分
- localhost:8080:application/rest/ 用于在下运行的 Web 服务
SpringBoot。
我不会这样做。让您的 webapp 和客户端应用程序位于 Tomcat 的同一上下文根中似乎很复杂。
相反,我建议您将静态文件部署到不同的上下文根或不同的 Http 服务器,例如 Apache http 服务器。从技术上讲,您的客户端应用程序不需要应用程序服务器。将它部署到像 Apache 这样的常规、更轻量和更快的 http 服务器将允许它在您重新启动 Tomcat 服务器时仍然运行。
然后你会得到:
现在您的客户端应用程序与您的服务 web 应用程序位于不同的主机上。并且会导致跨域错误。因此,您将不得不配置CORS 以允许您的客户端应用程序向您的服务webapp 发送请求。
另一种解决方案可能是将您的 Apache 设置为您的 tomcat 的代理。您可以使用定义一个将所有请求从端口 8080 转发到 tomcat 服务器的 VirtualHost。