Skip to content

Instantly share code, notes, and snippets.

@donhenton
Last active April 27, 2017 14:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save donhenton/7c7ccce6f808d2fc5074 to your computer and use it in GitHub Desktop.
Save donhenton/7c7ccce6f808d2fc5074 to your computer and use it in GitHub Desktop.
gulpfile to update tomcat when changes take place
/**
*
* @type Module gulp|Module gulp
* gulp file that will monitor files in the src tree and then move them
* to the tomcat instance. In effect a poor-man's JRebel
*
* Your tomcat server needs to be running and to see the changes
* This script will also refresh one page of your choice specified in the
* refresh page. Refreshing the page requires installation of the chrome
* live reload plugin
*
* this file must be in src/main/resources/assets it
* assumes it is only working with css, js in the assets folder and below
*
* usage: gulp watch-assets
*
* modules below must be installed globally
*
* the live reload plugin page may have to be the only page up in chrome?
*
*/
var gulp = require('gulp');
var tap = require('gulp-tap');
var watch = require('gulp-watch');
var livereload = require('gulp-livereload');
/**
* this is the list of files to watch will have to change this each usage
* @type Array
*/
var watchItems = [
'css/alpha.css',
'js/beta.js',
'js/gamma.js'];
/**
* the page that you want livereload to refresh
*
*/
var pageURL = 'http://localhost:8080/myapp/mypage';
/* html items are handled separately */
var htmlItem = '../pages/MyPage.html';
/* system needs to have TOMCAT_HOME properly defined */
var tomcatHome = process.env.TOMCAT_HOME;
gulp.task('watch-assets', function () {
livereload.listen();
watch(watchItems, function (events, done) {
gulp.start(['copy-assets', 'refresh']);
});
watch(htmlItem, function (events, done) {
gulp.start(['copy-html','reload-html']);
});
});
gulp.task('copy-assets', function () {
return gulp.src(watchItems, {'base': './../'})
.pipe(tap(function (file, t)
{
console.log('updating ' + file.relative);
}))
.pipe(gulp.dest(tomcatHome + '/webapps/myapp/WEB-INF/classes'));
});
gulp.task('reload-html', function()
{
//apparently needs to be done twice
function reloadHTML()
{
livereload.reload(pageURL);
livereload.reload(pageURL);
}
setTimeout( reloadHTML,1000);
});
gulp.task('copy-html', function () {
return gulp.src(htmlItem, {'base': './..'})
.pipe(tap(function (file, t)
{
console.log('updating html ' + file.relative);
}))
.pipe(gulp.dest(tomcatHome + '/myapp/tools/WEB-INF/classes'))
});
/**
* the location of the page to refresh will have to be changed as well
*/
gulp.task('refresh', function () {
console.log('refresh')
livereload.reload(pageURL);
});
@donhenton
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment