Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building NodeJS using Gradle

Tags:

node.js

gradle

I'm very new to Gradle. I started reading about it yesterday. I found an example build.gradle that builds a node application. I'm a little bit confused in the contents of the file. I'm not sure which ones are reserved or predefined words. One of the strings is node. It wasn't used somewhere but I figured out it was needed by the node plugin.

    buildscript {
        repositories {
            mavenCentral()
            maven {
                url 'https://plugins.gradle.org/m2/'
            }
        }

        dependencies {
            classpath 'com.moowork.gradle:gradle-node-plugin:1.2.0'
        }
    }

    apply plugin: 'base'
    apply plugin: 'com.moowork.node' // gradle-node-plugin

    node {
        /* gradle-node-plugin configuration
        https://github.com/srs/gradle-node-plugin/blob/master/docs/node.md

        Task name pattern:
        ./gradlew npm_<command> Executes an NPM command.
        */

        // Version of node to use.
        version = '10.14.1'

        // Version of npm to use.
        npmVersion = '6.4.1'

        // If true, it will download node using above parameters.
        // If false, it will try to use globally installed node.
        download = true
    }

    npm_run_build {
        // make sure the build task is executed only when appropriate files change
        inputs.files fileTree('public')
        inputs.files fileTree('src')

        // 'node_modules' appeared not reliable for dependency change detection (the task was rerun without changes)
        // though 'package.json' and 'package-lock.json' should be enough anyway
        inputs.file 'package.json'
        inputs.file 'package-lock.json'

        outputs.dir 'build'
    }

    // pack output of the build into JAR file
    task packageNpmApp(type: Zip) {
        dependsOn npm_run_build
        baseName 'npm-app'
        extension 'jar'
        destinationDir file("${projectDir}/build_packageNpmApp")
        from('build') {
            // optional path under which output will be visible in Java classpath, e.g. static resources path
            into 'static'
        }
    }

    // declare a dedicated scope for publishing the packaged JAR
    configurations {
        npmResources
    }

    configurations.default.extendsFrom(configurations.npmResources)

    // expose the artifact created by the packaging task
    artifacts {
        npmResources(packageNpmApp.archivePath) {
            builtBy packageNpmApp
            type 'jar'
        }
    }

    assemble.dependsOn packageNpmApp

    String testsExecutedMarkerName = "${projectDir}/.tests.executed"

    task test(type: NpmTask) {
        dependsOn assemble

        // force Jest test runner to execute tests once and finish the process instead of starting watch mode
        environment CI: 'true'

        args = ['run', 'test']

        inputs.files fileTree('src')
        inputs.file 'package.json'
        inputs.file 'package-lock.json'

        // allows easy triggering re-tests
        doLast {
            new File(testsExecutedMarkerName).text = 'delete this file to force re-execution JavaScript tests'
        }
        outputs.file testsExecutedMarkerName
    }

    check.dependsOn test

    clean {
        delete packageNpmApp.archivePath
        delete testsExecutedMarkerName
    }

Also, how is the build.gradle parsed? I'm also wondering how it is able to magically download node and npm tools.

like image 286
devwannabe Avatar asked May 11 '19 00:05

devwannabe


People also ask

Is Gradle like npm?

Like npm, Gradle also allows developers to specify the scope of dependencies. Dependencies can be separated into those that are only used for testing purposes and those that get bundled with the code for release to the production environment. Projects using Gradle can also track all necessary dependencies in a file.

Can I use Gradle for JavaScript?

Kotlin/JS projects use Gradle as a build system. To let developers easily manage their Kotlin/JS projects, we offer the kotlin. js Gradle plugin that provides project configuration tools together with helper tasks for automating routines typical for JavaScript development.

Can you install Gradle with npm?

gradle directory and use them from there. It also automatically installs npm when installing Node. js. It is also able to install Yarn by downloading it from a npm registry.

What is Gradle node plugin for?

This plugin enables you to use a lot of Node. js-based technologies as part of your build without having Node. js installed locally on your system.


1 Answers

This is a very general synopsis:

  • Gradle aims to hide away the logic from developers.
  • Most *.gradle files contain configuration blocks (closures) to specify HOW logic should run.
  • Plugins augment gradle with more configurable logic.
  • Also, 'convention over configuration' is a practice emphasize in gradle and its plugins, providing sensible defaults to minimize developers' configuration efforts.
  • The com.moowork.node plugin is configured through the node extension block.
  • Extension blocks are gradle's way to allow plugins to add more 'reserved' words to the standard gradle model.
  • The download = true configuration tells the plugin to download node (version = '10.14.1') and nmp (npmVersion = '6.4.1') in your project's root (unless you override its defaults as well).
  • The download of these tools will occur when any of the plugin's task is invoked.

Hope this helps.

like image 62
Marco R. Avatar answered Nov 15 '22 09:11

Marco R.