Handling JavaScript Dependencies with Gradle

In the Java world and with build tools like Maven or Gradle, you are used to declarative dependency management. Simply declare which libraries and versions you need, and let the build tool download and package these libs for you. The artifacts are typically stored in an artifact repository like Archiva, Artifactory or Nexus.
What is quite common for JAR dependencies seems to be quite uncommon for JavaScript dependencies. In my experience, a lot of Java projects that have a JavaScript front end still store the JavaScript libraries in a VCS.

I recently noticed (thanks to Steffen Schäfer) that Gradle, my current build tool of choice, is capable of handling JavaScript dependencies, too.
This is how it works:

Upload your required JavaScript library with a POM to your artifact repository.

  • inside the POM, use packaging js
  • to make it a valid POM, add the javascript-maven-plugin as an extension

Here is a full sample of such a POM:

<?xml version="1.0" encoding="UTF-8"?>
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">



  <name>Some name</name>
  <description>A really nice description</description>




In your Gradle build script (build.gradle), add a new configuration js and declare your dependencies for this configuration.

configurations {

dependencies {
    js 'de.oio:mylib:1.0.0'

Using configurations.js, you can access the JavaScript files as a fileset. A typical scenario is to include these JavaScript files into a WAR archive.
You can do this as follows.

apply plugin: 'war'

war {
        from configurations.js

Transitive dependencies are also supported. If mylib has a dependency to anotherlib, simply declare it in the POM of mylib:


You could also define a POM that has no corresponding artifact itself, but it includes a lot of dependencies. A use case for this could be a POM that simply includes all the JavaScript libraries that you use in all of your projects. To do so, simply create a POM with packaging pom and a dependencies block.

Short URL for this post: https://blog.oio.de/sQ8Ku
This entry was posted in Build, config and deploy, Web as a Platform and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *