Giter VIP home page Giter VIP logo

gradle-webjar-plugin's Introduction

Webjar Gradle Plugin

Build Coverage Status Gradle Plugin Portal Join the chat at https://gitter.im/coditory/gradle-webjar-plugin

Plugin that makes it easy to create SPA applications with Java back end. It connects JVM and Node.js based projects.

It maps typical java tasks to npm tasks (build, test, clean, etc). The build result is packed into a jar file, so it can be imported as a dependency by a java project.

  • This plugin builds frontend submodules. It can be also used to build a webjar libraries. See sample usage.
  • Tested with gradle version >= 7.1

Enabling the plugin

Add to your build.gradle:

plugins {
  id 'com.coditory.webjar' version '1.3.1'
}

When the project is build (./gradlew buid), produced jar contains all front end resources.

Tasks

Gradle Task Npm Task Runs before Description
webjarClean clean gradle clean Cleans output directory
webjarLint lint gradle check Checkstyle sources
webjarTest test java test Run tests
webjarBuild build java processResources Build Project
webjarWatch watch - Run in watch mode. Should be run with --no-daemon in order to stop the process on ctrl+c

There is also webjarInit that:

  • downloads Node and NPM
  • creates package.json and package-lock.json if missing

Why should I use gradle tasks instead of npm tasks Gradle tasks runs using embedded node.

  • npm run watch uses system Node and NPM
  • ./gradlew webjarWatch --no-daemon uses project Node and NPM

Skipping webjar build

Frontend projects take a lot of time to build. You can skip frontend build with:

  • ./gradlew build -PskipWebjar - project property
  • SKIP_WEBJAR=true ./gradlew build - system environment variable

Configuration

Configuring Webjar plugin

All presented values are defaults.

webjar {
    // Directory where npm puts the result
    distDir = "dist"
    // Directory with npm results in the jar
    webjarDir = "static"

    // NPM Task names
    taskNames {
        clean = "clean"
        build = "build"
        test = "test"
        lint = "lint"
        watch = "watch"
    }

    // Caching options
    cache {
        enabled = true
        cacheTest = true
        cacheLint = true
        // Some timestamp files used for gradle caching
        testTimestampFile = "test/timestamp"
        lintTimestampFile = "lint/timestamp"
        // Location of src and dest input files
        src = listOf("src")
        test = listOf("tests")
    }
}

Configuring Node and NPM

Webjar plugin uses great gradle-node-plugin. You can configure Node and NPM with:

node {
  // Version of node to use.
  version = '16.5.0'
  // Version of npm to use.
  npmVersion = '7.19.1'
  // Base URL for fetching node distributions (change if you have a mirror).
  // Or set to null if you want to add the repository on your own.
  distBaseUrl = 'https://nodejs.org/dist'
  // If true, it will download node using above parameters.
  // If false, it will try to use globally installed node.
  download = true
  // Set the work directory for unpacking node
  workDir = file("${project.buildDir}/.node/node")
  // Set the work directory for NPM
  npmWorkDir = file("${project.buildDir}/.node/npm")
  // Set the work directory where node_modules should be located
  nodeModulesDir = file("${project.projectDir}")
}

All values from above example are defaults setup by webjar plugin.

Using for a front end submodule

There is a sample project with two submodules:

my-project
 |- backend
 |- frontend

Backend project depends on frontend project:

dependencies {
    implementation(project(":frontend"))
}

Frontend project uses webjar plugin to map npm tasks to gradle. When frontend is built all frontend resources are available on backend classpath under /static folder.

Creating a webjar library for webjars.org, requires specifying standardized webjarDir

webjar {
  webjarDir = "META-INF/resources/webjars/${project.name}/${project.version}"
}

gradle-webjar-plugin's People

Contributors

coditory-bot avatar daiyuang avatar dependabot[bot] avatar pmendelski avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.