task cleanAll { group = 'minify' doFirst { tasks.cleanCSS.execute() tasks.cleanJS.execute() } }
task cleanCSS { group = 'minify' doLast { String basePath = 'src/main/resources/static/stylesheets/site'; FileTree tree = project.fileTree(dir: basePath); tree.include "**/*.css" tree.exclude "**/*.scss" tree.exclude "**/*.css.map" tree.visit { FileVisitDetails element -> if (!element.isDirectory()) { element.file.delete() } } } }
task cleanJS { group = 'minify' doLast { String basePath = 'src/main/resources/static/javascripts/site'; FileTree tree = project.fileTree(dir: basePath); tree.include "**/*.min.js" tree.exclude "**/*.js.map" tree.exclude "**/*.ts" tree.exclude "**/*.d.ts" tree.visit { FileVisitDetails element -> if (!element.isDirectory()) { element.file.delete() } } } }
task sass { group = 'minify' doLast { String basePath = 'src/main/resources/static/stylesheets/site'; FileTree tree = project.fileTree(dir: basePath); tree.include "**/*.scss" tree.exclude "**/*.css" tree.exclude "**/*.css.map" tree.visit { FileVisitDetails element -> if (!element.isDirectory()) { String filePath = basePath + "/" + element.getPath(); int pos = filePath.lastIndexOf("."); String newFilePath = ""; if (pos > 0) { newFilePath = filePath.substring(0, pos) + ".css"; } if (project.file(newFilePath).exists()) { project.file(newFilePath).delete(); } String cmd = String.format("node-sass $filePath $newFilePath"); java.lang.Runtime.getRuntime().exec(cmd); } } } }
task csso { group = 'minify' doLast { String basePath = 'src/main/resources/static/stylesheets/site'; FileTree tree = project.fileTree(dir: basePath); tree.include "**/*.css" tree.exclude "**/*.min.css" tree.exclude "**/*.css.map" tree.exclude "**/*.scss" tree.visit { FileVisitDetails element -> if (!element.isDirectory()) { String filePath = basePath + "/" + element.getPath(); int pos = filePath.lastIndexOf("."); String newFilePath = ""; if (pos > 0) { newFilePath = filePath.substring(0, pos) + ".min.css"; } String cmd = String.format("csso -i $filePath -o $newFilePath"); java.lang.Runtime.getRuntime().exec(cmd); } } } }
task uglify { group = 'minify' doLast { String basePath = 'src/main/resources/static/javascripts/site'; FileTree tree = project.fileTree(dir: basePath); tree.include "**/*.js" tree.exclude "**/*.min.js" tree.exclude "**/*.js.map" tree.exclude "**/*.ts" tree.exclude "**/*.d.ts" tree.visit { FileVisitDetails element -> if (!element.isDirectory()) { String filePath = basePath + "/" + element.getPath(); int pos = filePath.lastIndexOf("."); String newFilePath = ""; if (pos > 0) { newFilePath = filePath.substring(0, pos) + ".min.js"; } String cmd = String.format("uglifyjs $filePath -o $newFilePath"); java.lang.Runtime.getRuntime().exec(cmd); } } } }
sass.mustRunAfter cleanCSS csso.mustRunAfter sass uglify.mustRunAfter cleanJS
task minify{ group = 'minify' doFirst{ tasks.cleanCSS.execute() tasks.cleanJS.execute() } doLast{ tasks.sass.execute() tasks.csso.execute() tasks.uglify.execute() } }
References
https://docs.gradle.org/current/userguide/working_with_files.html
https://docs.gradle.org/current/userguide/tutorial_using_tasks.html
https://docs.gradle.org/current/userguide/custom_tasks.html
https://docs.gradle.org/current/userguide/more_about_tasks.html