Skip to content

Commit 7c9d6e1

Browse files
committed
Grunt for serving the javascript app without having to redeploy the servlet container
1 parent bbfc4ad commit 7c9d6e1

File tree

3 files changed

+490
-0
lines changed

3 files changed

+490
-0
lines changed

Gruntfile.js

Lines changed: 399 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,399 @@
1+
// Generated on 2015-06-27 using generator-jhipster 2.16.1
2+
'use strict';
3+
var fs = require('fs');
4+
5+
var parseString = require('xml2js').parseString;
6+
// Returns the second occurence of the version number
7+
var parseVersionFromPomXml = function() {
8+
var version;
9+
var pomXml = fs.readFileSync('pom.xml', "utf8");
10+
parseString(pomXml, function (err, result){
11+
version = result.project.version[0];
12+
});
13+
return version;
14+
};
15+
16+
// usemin custom step
17+
var useminAutoprefixer = {
18+
name: 'autoprefixer',
19+
createConfig: function(context, block) {
20+
if(block.src.length === 0) {
21+
return {};
22+
} else {
23+
return require('grunt-usemin/lib/config/cssmin').createConfig(context, block) // Reuse cssmins createConfig
24+
}
25+
}
26+
};
27+
28+
module.exports = function (grunt) {
29+
require('load-grunt-tasks')(grunt);
30+
require('time-grunt')(grunt);
31+
32+
grunt.initConfig({
33+
yeoman: {
34+
// configurable paths
35+
app: require('./bower.json').appPath || 'app',
36+
dist: 'src/main/webapp/dist'
37+
},
38+
watch: {
39+
bower: {
40+
files: ['bower.json'],
41+
tasks: ['wiredep']
42+
},
43+
ngconstant: {
44+
files: ['Gruntfile.js', 'pom.xml'],
45+
tasks: ['ngconstant:dev']
46+
}
47+
},
48+
autoprefixer: {
49+
// src and dest is configured in a subtask called "generated" by usemin
50+
},
51+
wiredep: {
52+
app: {
53+
src: ['src/main/webapp/index.html'],
54+
exclude: [
55+
/angular-i18n/, // localizations are loaded dynamically
56+
/swagger-ui/
57+
]
58+
},
59+
test: {
60+
src: 'src/test/javascript/karma.conf.js',
61+
exclude: [/angular-i18n/, /swagger-ui/, /angular-scenario/],
62+
ignorePath: /\.\.\/\.\.\//, // remove ../../ from paths of injected javascripts
63+
devDependencies: true,
64+
fileTypes: {
65+
js: {
66+
block: /(([\s\t]*)\/\/\s*bower:*(\S*))(\n|\r|.)*?(\/\/\s*endbower)/gi,
67+
detect: {
68+
js: /'(.*\.js)'/gi
69+
},
70+
replace: {
71+
js: '\'{{filePath}}\','
72+
}
73+
}
74+
}
75+
}
76+
},
77+
browserSync: {
78+
dev: {
79+
bsFiles: {
80+
src : [
81+
'src/main/webapp/**/*.html',
82+
'src/main/webapp/**/*.json',
83+
'src/main/webapp/assets/styles/**/*.css',
84+
'src/main/webapp/scripts/**/*.js',
85+
'src/main/webapp/assets/images/**/*.{png,jpg,jpeg,gif,webp,svg}',
86+
'tmp/**/*.{css,js}'
87+
]
88+
}
89+
},
90+
options: {
91+
watchTask: true,
92+
proxy: "localhost:8080"
93+
}
94+
},
95+
clean: {
96+
dist: {
97+
files: [{
98+
dot: true,
99+
src: [
100+
'.tmp',
101+
'<%= yeoman.dist %>/*',
102+
'!<%= yeoman.dist %>/.git*'
103+
]
104+
}]
105+
},
106+
server: '.tmp'
107+
},
108+
jshint: {
109+
options: {
110+
jshintrc: '.jshintrc'
111+
},
112+
all: [
113+
'Gruntfile.js',
114+
'src/main/webapp/scripts/app.js',
115+
'src/main/webapp/scripts/app/**/*.js',
116+
'src/main/webapp/scripts/components/**/*.js'
117+
]
118+
},
119+
concat: {
120+
// src and dest is configured in a subtask called "generated" by usemin
121+
},
122+
uglifyjs: {
123+
// src and dest is configured in a subtask called "generated" by usemin
124+
},
125+
rev: {
126+
dist: {
127+
files: {
128+
src: [
129+
'<%= yeoman.dist %>/scripts/**/*.js',
130+
'<%= yeoman.dist %>/assets/styles/**/*.css',
131+
'<%= yeoman.dist %>/assets/images/**/*.{png,jpg,jpeg,gif,webp,svg}',
132+
'<%= yeoman.dist %>/assets/fonts/*'
133+
]
134+
}
135+
}
136+
},
137+
useminPrepare: {
138+
html: 'src/main/webapp/**/*.html',
139+
options: {
140+
dest: '<%= yeoman.dist %>',
141+
flow: {
142+
html: {
143+
steps: {
144+
js: ['concat', 'uglifyjs'],
145+
css: ['cssmin', useminAutoprefixer] // Let cssmin concat files so it corrects relative paths to fonts and images
146+
},
147+
post: {}
148+
}
149+
}
150+
}
151+
},
152+
usemin: {
153+
html: ['<%= yeoman.dist %>/**/*.html'],
154+
css: ['<%= yeoman.dist %>/assets/styles/**/*.css'],
155+
js: ['<%= yeoman.dist %>/scripts/**/*.js'],
156+
options: {
157+
assetsDirs: ['<%= yeoman.dist %>', '<%= yeoman.dist %>/assets/styles', '<%= yeoman.dist %>/assets/images', '<%= yeoman.dist %>/assets/fonts'],
158+
patterns: {
159+
js: [
160+
[/(assets\/images\/.*?\.(?:gif|jpeg|jpg|png|webp|svg))/gm, 'Update the JS to reference our revved images']
161+
]
162+
},
163+
dirs: ['<%= yeoman.dist %>']
164+
}
165+
},
166+
imagemin: {
167+
dist: {
168+
files: [{
169+
expand: true,
170+
cwd: 'src/main/webapp/assets/images',
171+
src: '**/*.{jpg,jpeg}', // we don't optimize PNG files as it doesn't work on Linux. If you are not on Linux, feel free to use '**/*.{png,jpg,jpeg}'
172+
dest: '<%= yeoman.dist %>/assets/images'
173+
}]
174+
}
175+
},
176+
svgmin: {
177+
dist: {
178+
files: [{
179+
expand: true,
180+
cwd: 'src/main/webapp/assets/images',
181+
src: '**/*.svg',
182+
dest: '<%= yeoman.dist %>/assets/images'
183+
}]
184+
}
185+
},
186+
cssmin: {
187+
options: {
188+
root: 'src/main/webapp' // Replace relative paths for static resources with absolute path
189+
}
190+
// src and dest is configured in a subtask called "generated" by usemin
191+
},
192+
ngtemplates: {
193+
dist: {
194+
cwd: 'src/main/webapp',
195+
src: ['scripts/app/**/*.html', 'scripts/components/**/*.html',],
196+
dest: '.tmp/templates/templates.js',
197+
options: {
198+
module: 'bookstoreApp',
199+
usemin: 'scripts/app.js',
200+
htmlmin: {
201+
removeCommentsFromCDATA: true,
202+
// https://github.com/yeoman/grunt-usemin/issues/44
203+
collapseWhitespace: true,
204+
collapseBooleanAttributes: true,
205+
conservativeCollapse: true,
206+
removeAttributeQuotes: true,
207+
removeRedundantAttributes: true,
208+
useShortDoctype: true,
209+
removeEmptyAttributes: true
210+
}
211+
}
212+
}
213+
},
214+
htmlmin: {
215+
dist: {
216+
options: {
217+
removeCommentsFromCDATA: true,
218+
// https://github.com/yeoman/grunt-usemin/issues/44
219+
collapseWhitespace: true,
220+
collapseBooleanAttributes: true,
221+
conservativeCollapse: true,
222+
removeAttributeQuotes: true,
223+
removeRedundantAttributes: true,
224+
useShortDoctype: true,
225+
removeEmptyAttributes: true,
226+
keepClosingSlash: true
227+
},
228+
files: [{
229+
expand: true,
230+
cwd: '<%= yeoman.dist %>',
231+
src: ['*.html'],
232+
dest: '<%= yeoman.dist %>'
233+
}]
234+
}
235+
},
236+
// Put files not handled in other tasks here
237+
copy: {
238+
dist: {
239+
files: [{
240+
expand: true,
241+
dot: true,
242+
cwd: 'src/main/webapp',
243+
dest: '<%= yeoman.dist %>',
244+
src: [
245+
'*.html',
246+
'scripts/**/*.html',
247+
'assets/images/**/*.{png,gif,webp,jpg,jpeg,svg}',
248+
'assets/fonts/*'
249+
]
250+
}, {
251+
expand: true,
252+
cwd: '.tmp/assets/images',
253+
dest: '<%= yeoman.dist %>/assets/images',
254+
src: [
255+
'generated/*'
256+
]
257+
}]
258+
},
259+
generateOpenshiftDirectory: {
260+
expand: true,
261+
dest: 'deploy/openshift',
262+
src: [
263+
'pom.xml',
264+
'src/main/**'
265+
]
266+
}
267+
},
268+
karma: {
269+
unit: {
270+
configFile: 'src/test/javascript/karma.conf.js',
271+
singleRun: true
272+
}
273+
},
274+
ngAnnotate: {
275+
dist: {
276+
files: [{
277+
expand: true,
278+
cwd: '.tmp/concat/scripts',
279+
src: '*.js',
280+
dest: '.tmp/concat/scripts'
281+
}]
282+
}
283+
},
284+
buildcontrol: {
285+
options: {
286+
commit: true,
287+
push: false,
288+
connectCommits: false,
289+
message: 'Built %sourceName% from commit %sourceCommit% on branch %sourceBranch%'
290+
},
291+
openshift: {
292+
options: {
293+
dir: 'deploy/openshift',
294+
remote: 'openshift',
295+
branch: 'master'
296+
}
297+
}
298+
},
299+
ngconstant: {
300+
options: {
301+
name: 'bookstoreApp',
302+
deps: false,
303+
wrap: '"use strict";\n// DO NOT EDIT THIS FILE, EDIT THE GRUNT TASK NGCONSTANT SETTINGS INSTEAD WHICH GENERATES THIS FILE\n{%= __ngModule %}'
304+
},
305+
dev: {
306+
options: {
307+
dest: 'src/main/webapp/scripts/app/app.constants.js'
308+
},
309+
constants: {
310+
ENV: 'dev',
311+
VERSION: parseVersionFromPomXml()
312+
}
313+
},
314+
prod: {
315+
options: {
316+
dest: '.tmp/scripts/app/app.constants.js'
317+
},
318+
constants: {
319+
ENV: 'prod',
320+
VERSION: parseVersionFromPomXml()
321+
}
322+
}
323+
}
324+
});
325+
326+
grunt.registerTask('serve', [
327+
'clean:server',
328+
'wiredep',
329+
'ngconstant:dev',
330+
'browserSync',
331+
'watch'
332+
]);
333+
334+
grunt.registerTask('server', function (target) {
335+
grunt.log.warn('The `server` task has been deprecated. Use `grunt serve` to start a server.');
336+
grunt.task.run([target ? ('serve:' + target) : 'serve']);
337+
});
338+
339+
grunt.registerTask('test', [
340+
'clean:server',
341+
'wiredep:test',
342+
'ngconstant:dev',
343+
'karma'
344+
]);
345+
346+
grunt.registerTask('build', [
347+
'clean:dist',
348+
'wiredep:app',
349+
'ngconstant:prod',
350+
'useminPrepare',
351+
'ngtemplates',
352+
'imagemin',
353+
'svgmin',
354+
'concat',
355+
'copy:dist',
356+
'ngAnnotate',
357+
'cssmin',
358+
'autoprefixer',
359+
'uglify',
360+
'rev',
361+
'usemin',
362+
'htmlmin'
363+
]);
364+
365+
grunt.registerTask('appendSkipBower', 'Force skip of bower for Gradle', function () {
366+
367+
if (!grunt.file.exists(filepath)) {
368+
// Assume this is a maven project
369+
return true;
370+
}
371+
372+
var fileContent = grunt.file.read(filepath);
373+
var skipBowerIndex = fileContent.indexOf("skipBower=true");
374+
375+
if (skipBowerIndex != -1) {
376+
return true;
377+
}
378+
379+
grunt.file.write(filepath, fileContent + "\nskipBower=true\n");
380+
});
381+
382+
grunt.registerTask('buildOpenshift', [
383+
'test',
384+
'build',
385+
'copy:generateOpenshiftDirectory',
386+
]);
387+
388+
grunt.registerTask('deployOpenshift', [
389+
'test',
390+
'build',
391+
'copy:generateOpenshiftDirectory',
392+
'buildcontrol:openshift'
393+
]);
394+
395+
grunt.registerTask('default', [
396+
'test',
397+
'build'
398+
]);
399+
};

0 commit comments

Comments
 (0)