这可能对您有用,也可能不适用,因为我不确定您是否希望所有内容都完全集成。
此方法将让您运行 Tomcat 服务器,允许您通过自动重新加载(以及您可能想要运行的任何其他任务)进行前端更改,并且无需在 Tomcat 中重新编译/重新加载 Java。我使用它,前端开发速度非常快。
总结思路:
使用任何方式(maven tomcat 插件、Eclipse 服务器选项卡等)运行您的 Java 应用程序。在标准端口(例如 8080)上运行它。我假设你知道怎么做。
使用 grunt、grunt-contrib-watch、grunt-contrib-connect、grunt-contrib-proxy。在与 tomcat 不同的端口上运行 grunt-contrib-connect,例如9000. 设置 grunt-contrib-proxy 以将来自 grunt-contrib-connect 服务器的请求代理到不同端口上的后端 tomcat,如有必要,重写 URL。
为了帮助您入门,这里是 Gruntfile.js 的相关部分(修改为您的应用程序):
// Watches files for changes and runs tasks based on the changed files
watch: {
js: {
files: ['<%= my.app %>/scripts/**/*.js'],
tasks: [],
options: {
livereload: '<%= connect.options.livereload %>'
}
},
livereload: {
options: {
livereload: '<%= connect.options.livereload %>'
},
files: [
'<%= my.app %>/**/*.html',
'<%= my.app %>/.tmp/styles/**/*.css',
'<%= my.app %>/images/**/*.{png,jpg,jpeg,gif,webp,svg}'
]
}
},
// The actual grunt server settings
connect: {
options: {
port: 9000,
// Change this to '0.0.0.0' to access the server from outside.
hostname: 'localhost',
livereload: 35729
},
proxies: [{
context: '/rest',
host: 'localhost',
port: 8080,
https: false,
changeOrigin: true,
rewrite: {
'^/rest': '/myapp/rest'
}
}],
livereload: {
options: {
open: true,
middleware: function(connect) {
return [
require('grunt-connect-proxy/lib/utils').proxyRequest,
connect.static('src/main/webapp/.tmp'),
connect.static('src/main/webapp/'),
];
}
}
},
要进行开发,请启动后端 Tomcat 应用程序,然后使用控制台运行:grunt serve。进行您想要的任何前端更改,它们将自动重新加载,并且任何相关的 grunt 任务都将随着每次更改而运行。您的应用将正常运行,因为所有后端请求都被代理到 Tomcat,并且前端请求由 grunt-contrib-connect 处理。