Gradle is a wonderful build tool for building projects of all languages, shapes, and sizes. It it is built on Java, runs on the JVM, and traditionally has provided an excellent Groovy DSL for creating robust and dynamic build scripts. Recently, Gradle has also announced they will support Kotlin as a language for constructing these build scripts.
But what about supporting the most popular and least controversial language of all time? Why can’t our builds have the same levels of complexity and non-deterministic failures as our front end code does? What gives, Gradle?!
This is why I have taken it upon myself to Make Gradle Great Again™ and provide second class support for JavaScript as a build language. Now, you can write your build logic using the same confusing bullshit you throw into your index.html
files.
Easy as fuck.
-
You still need a
build.gradle
file in your project root.
buildscript {
repositories { jcenter() }
dependencies { classpath 'com.danveloper.github:gradle-js:1.0.2' }
}
apply plugin: 'js-build'
Boom that’s it.
JavaScript the fuck out of your project.
-
You need a
gradle.js
file in your project root.
project.buildscript(function(bs) { // (1)
bs.repositories(function(repos) { // (2)
repos.jcenter() // (3)
});
bs.dependencies(function(deps) {
deps.classpath('xml-apis:xml-apis:2.0.2') // (4)
});
});
project.apply({plugin: 'java'}); // (5)
project.repositories(function(repos) { // (6)
repos.mavenCentral();
});
project.dependencies(function(deps) { // (7)
deps.compile('com.mycompany:more-java-javascript-code:1.0.2'); // (8)
});
project.task('myJavaScriptTask', function(task) { // (9)
task.action(function(task) { // (10)
print("I am a task.");
});
task.doLast(function(task) { // (11)
print("I do everything too lmao");
});
});
-
Your terrible JavaScript build file is given access to the
Project
through theproject
variable. -
You can configure your build script’s repos and shit.
-
You can even use the shortcuts like
jcenter
and if you’re feeling awful about yourselfmavenCentral
andmavenLocal
too! -
Add dependencies to your build script!
-
Apply plugins!
-
Configure the repositories for your project using JavaScript functions.
-
More functions!! Project dependencies!
-
Add shit to your project configurations!
-
Even add tasks to your build!
-
Special use case:
task.action(function(task) {…})
adds an explicit action. -
Even do things like
doLast
and all that otherTask
shit you do.
Just run that shit.
$ ./gradlew myJavaScriptTask
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:compileTestJava UP-TO-DATE
:myJavaScriptTask
I am a task.
I do everything too lmao
BUILD SUCCESSFUL
Total time: 31.306 secs
This build could be faster, please consider using the Gradle Daemon: https://docs.gradle.org/2.14/userguide/gradle_daemon.html
$
There you go.