• This repository has been archived on 29/Nov/2018
  • Stars
    star
    214
  • Rank 178,027 (Top 4 %)
  • Language
    Java
  • License
    Other
  • Created over 10 years ago
  • Updated about 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Grunt + Maven integration done right

grunt-maven-plugin

Deprecation note

This plugin will no longer be maintained. I can accept pull requests with some minor improvements and release new versions, but there will be no active development.

It has been almost two years since we moved from Maven to Gradle in most of our projects at Allegro. Since we don't use the plugin nor even Maven, it is impossible to notice what new features are needed as frontend tools develop.

If you are looking for a simple replacement, try frontend-maven-plugin.


grunt-maven-plugin plugin allows you to integrate Grunt tasks into the Maven build process. Grunt is the JavaScript task runner utility. grunt-maven-plugin works on both Windows and *nix systems.

grunt-maven-plugin comes with unique Maven+Grunt Integrated Workflow which removes all impediments present when trying to build project using two different build tools.

Version 1.5.0 requires grunt-maven NPM plugin 1.2.0 version for Integrated Workflow to work.

Prerequisites

The only required dependency is nodejs with npm. Globally installed grunt-cli is optional and preferred, but not necessary, as installing custom node modules can be problematic in some environments (ex. CI servers). Additional configuration is needed when using local grunt-cli.

grunt-maven-plugin can also run bower install from bower to install front-end dependencies.

grunt-maven-plugin is compatible with JDK 6+ and Maven 2.1+.

Motivation

grunt-maven-plugin came to life because I needed a good tool to integrate Maven and Grunt. By good i mean not just firing off a grunt process, but a tool that would respect rules from both backend (Maven) and frontend (Grunt) worlds. No node_modules in Maven sources, no Maven src/main/webapp/.. paths in Gruntfile.js.

grunt-maven-plugin allows you to create a usual NPM/Grunt project that could be built and understood by any Node developer, and put it somewhere inside Maven project. It can be extracted at any time and nothing should break. On the other side backend developers don't need to worry about pesky node_modules wandering around src/ - all dependencies, generated sources and deliverables live in dedicated target-grunt directory, doing this part of build the Maven way.

Usage

Add grunt-maven-plugin to application build process in your pom.xml:

<plugin>
    <groupId>pl.allegro</groupId>
    <artifactId>grunt-maven-plugin</artifactId>
    <version>1.5.0</version>
    <configuration>
        <!-- relative to src/main/webapp/, default: static -->
        <jsSourceDirectory>path_to_js_project</jsSourceDirectory>

        <!-- example options usage to get verbose output in logs -->
        <gruntOptions>
            <gruntOption>--verbose</gruntOption>
        </gruntOptions>

        <!-- example npm install env variable -->
        <npmEnvironmentVar>
            <PHANTOMJS_CDNURL>http://cnpmjs.org/downloads</PHANTOMJS_CDNURL>
        </npmEnvironmentVar>

        <!-- example options usage to filter variables in given resource -->
        <filteredResources>
            <filteredResource>maven-properties.json</filteredResource>
        </filteredResources>

    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>create-resources</goal>
                <goal>npm</goal>
                <!-- or npm-offline if npm failure is not an option -->
                <goal>bower</goal>
                <goal>grunt</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Usage with local grunt-cli

In order to use local grunt-cli, add

<gruntExecutable>node_modules/grunt-cli/bin/grunt</gruntExecutable>
<runGruntWithNode>true</runGruntWithNode>

options to plugin configuration and add grunt-cli to JS project package.json:

{
    "devDependencies": {
        "grunt-cli": "~0.1.6",
        "grunt": "~0.4.2"
        /*...*/
    }
}

Using NPM in offline mode

NPM downtimes can be painful for (some) development and (all) release builds. grunt-maven-plugin contains npm-offline goal that uses tar-ed node_modules instead of running npm install during each build.

npm-offline flow:

  • extract node_modules.tar in target-grunt
  • run npm-install --ignore-scripts in case there are any dependencies to download that were not tar-ed
  • run npm rebuild

If node_modules dir already exists in target-grunt, it is not overriden. Offline flow is based on this blogpost.

Why only tar, not gz?

  • GIT uses compression internally anyway
  • TAR is lightweight and easy to extract
  • TAR is easier to diff

If there are some compelling arguments for compressing this archive, please post an issue and we might add support in future releases.

Preparing node_modules.tar

In target-grunt:

rm -rf node_modules/
npm install --ignore-scripts
tar cf node_modules.tar node_modules/
mv node_modules.tar ../src/main/webapp/static/

Using linked node_modules

Removed in 1.3.0 release, use npm-offline instead.

Working example

Sandbox project contains simple usage example. It is used to PoC/develop/test new features, so it always stays up to date with SNAPSHOT version.

How it works?

  1. JavaScript project sources from

     sourceDirectory/jsSourceDirectory (default: src/main/webapp/static)
    

    are copied to

     gruntBuildDirectory (default: target-grunt)
    

    Copied directory has to contain package.json and Gruntfile.js files

  2. npm install is called, fetching all dependencies

  3. grunt is run to complete the build process

Because the plugin creates its own target dir, it should be added to ignored resources in SCM configuration (like .gitignore).

grunt-maven-plugin options

Plugin options available in <configuration>...</configuration> are:

misc

  • showColors : should Grunt and npm use color output; defaults to false
  • filteredResources : list of files (or expressions) that will be filtered using maven-resources-plugin when creating resources, remember to exclude those files from integrated workflow config, as else Grunt will override filtered values
  • excludedResources : list of files (or expressions) that will be excluded when creating resources, remember to exclude those files from integrated workflow config, as else Grunt will override filtered values
  • disabled : skip execution of plugin; defaults to false

environment

  • gruntBuildDirectory : path to Grunt build directory (target for Grunt); defaults to ${basedir}/target-grunt
  • sourceDirectory : path to directory containing source JavaScript project directory; defaults to ${basedir}/src/main/webapp
  • jsSourceDirectory : name of directory relative to sourceDirectory, which contents are going to be copied to gruntBuildDirectory; defaults to static
  • warTargetDirectory: name of directory relative to WAR root, where artifacts from grunt build will be copied; defaults to jsSourceDirectory, use / for root of WAR

node

  • nodeExecutable : name of globally available node executable; defaults to node

npm

  • npmExecutable : name of globally available npm executable; defaults to npm
  • npmEnvironmentVar : map of environmental variables passed down to npm install command; might be useful for npm repo customization
  • npmOptions : list of custom options passed to npm when calling npm install (defaults to empty)

offline

  • npmOfflineModulesFile : name of tar-ed node_modules file; defaults to node_modules.tar
  • npmOfflineModulesFilePath : path to node_modules file, relative to project basedir; defaults to sourceDirectory/jsSourceDirectory
  • npmRebuildOptions : list of custom options passed to npm when calling npm rebuild (defaults to empty)

bower

  • bowerExecutable : name of globally available bower executable; defaults to bower

grunt

  • target : name of Grunt target to run (defaults to null, default Grunt target is run)
  • gruntOptions : list of custom options passed to grunt (defaults to empty)
  • ignoreTasksErrors : ignore failing Grunt tasks errors and finish Maven build with success (ignoring 3 and 6 exit code, more on Grunt exit codes)
  • ignoreAllErrors : ignore all Grunt errors and finish Maven build with success (ignoring all exit codes, more on Grunt exit codes)
  • gruntExecutable : name of grunt executable; defaults to grunt
  • runGruntWithNode : if Grunt executable is a js script, it needs to be run using node, ex: node path/to/grunt; defaults to false

Execution goals

  • create-resources : copies all files and filteredResources from sourceDirectory/jsSourceDirectory to gruntBuildDirectory
  • npm : executes npm install in target directory
  • npm-offline : reuses packed node modules instead of fetching them from npm
  • bower : executes bower install in target directory
  • grunt : executes Grunt in target directory
  • clean : deletes gruntBuildDirectory

Maven+Grunt Integrated workflow

Using grunt-maven-plugin is convenient, but there is still a huge gap between frontend and backend development workflow. Various IDEs (Netbeans, IntelliJ Idea, Eclipse) try to ease webapp development by synchronizing changes made in src/webapp/ to exploded WAR directory in target/. This naive approach works as long as there is no need to minify JavaScript sources, compile less files or project does not follow "single WAR" rule and can afford using Maven profiles. Rebuilding project each time a change was made in *.js is a horrible thing. Fortunately grunt-maven-plugin is a great tool to solve this problem.

Idea is to ignore IDE mechanisms and run Grunt build each time a change in static files was made. Traditional workflow looks like:

  1. user changes src/webapp/static/hello.js
  2. IDE detects change
  3. IDE copies hello.js to target/war_name/static/hello.js

This gives little room to integrate other processes in between. Workflow utilizing grunt-maven-plugin:

  1. run Grunt watch process
  2. user changes src/webapp/static/hello.js
  3. Grunt detects changes, copies hello.js to target-grunt/hello.js
  4. run Grunt tasks, produce target-grunt/dist/hello.min.js with source map target-grunt/dist/hello.map.js
  5. Grunt copies results to target/warname/static

Now what happens inside target-grunt is for us to decide, it can be virtually anything - minification, less compilation, running JS tests, JS linting. Anything Grunt can do, just like during heavy build process.

Configuring Maven

Since we want grunt-maven-plugin to take control of what ends up in WAR, we need to tell Maven WAR plugin to ignore our statics dir when creating WAR:

<build>
    <plugins>
	<plugin>
	    <artifactId>maven-war-plugin</artifactId>
	    <version>2.3</version>
	    <configuration>
		<warSourceExcludes>jsSourceDirectory/**</warSourceExcludes>
	    </configuration>
	</plugin>
    </plugins>
</build>

Configuring Grunt

grunt-maven-plugin has a dedicated NPM Grunt multitasks that make integrated workflow work.

grunt.initConfig({
  mavenPrepare: {
    options: {
      resources: ['**']
    },
    prepare: {}
  },

  mavenDist: {
    options: {
      warName: '<%= gruntMavenProperties.warName %>',
      deliverables: ['**', '!non-deliverable.js'],
      gruntDistDir: 'dist'
    },
    dist: {}
  },

  gruntMavenProperties: grunt.file.readJSON('grunt-maven.json'),

  watch: {
    maven: {
      files: ['<%= gruntMavenProperties.filesToWatch %>'],
      tasks: 'default'
    }
  }
});


grunt.loadNpmTasks('grunt-maven');

grunt.registerTask('default', ['mavenPrepare', 'jshint', 'karma', 'less', 'uglify', 'mavenDist']);

For more information please consult documentation of grunt-maven-npm project.

Deep customization of workflow

It is possible to override inner workflow configuration during runtime. Inner properties are extracted from pom.xml and used internally inside workflow Grunt tasks. They reside in target-grunt/grunt-maven.json. After reading inner properties JSON file, workflow task seeks for grunt-maven-custom.json file in target-grunt and overrides original properties with custom ones.

Configuring IDE

Depending on IDE, synchronization processes behave differently. Having JSP files synchronized is a nice feature, so it is up to you to decide what should be turned off. In Netbeans and IntelliJ no configuration is needed, they play nicely with new workflow.

You still have to remember to run Grunt watch process in background so it can monitor changes. It can be run from IDE via grunt-maven-plugin. Define custom runner that will run:

mvn grunt:grunt -Dtarget=watch

You should see process output each time static sources change.

Configuring Eclipse

Eclipse is a special case. Unfortunately it does not read WAR from Maven target, instead it keeps own file hierarchy. Eclipse developers on the team should use deep workflow customization to override properties used by others. Proposed way is to create grunt-maven-custom.json in Maven sources and add it to .gitignore, so it will not pollute source repository. Since i have little experience with Eclipse, i would welcome a contribution of sample configuration, but most probably it is enough to override targetPath property.

Changelog

  • 1.5.9 (26.12.2014)
    • added option to change destination of all deliverables within WAR
  • 1.4.1 (02.08.2014)
    • added option to exclude custom resources in create-resources phase
  • 1.4.0 (07.07.2014)
    • changed default lifecycle bindings
    • support for disabling execution based on flag
  • 1.3.2 (19.06.2014)
    • support for defining npm command line parameters
    • fixed bug from 1.3.1 - null pointer when no npm env variables specified
  • 1.3.1 (26.05.2014)
    • support for Maven 2.1+ and 3.*
    • option to specify NPM env variables
  • 1.3.0 (22.04.2014)
    • dropped support for link-node-modules
  • 1.2.2 (31.03.2014)
    • support for npm offline mode
  • 1.2.1 (25.02.2014)
    • executing bower install
  • 1.2.0 (07.02.2014)
    • new Maven+Grunt NPM multitasks
  • 1.1.4 (05.02.2014)
    • added option to ignore Grunt task errors (failing tests etc) (#22)
  • 1.1.3 (25.01.2014)
    • fixed issue with using custom Grunt target and CLI options together (#20)
  • 1.1.2 (06.01.2014)
    • fixed compatibility issue of 1.1.x branch - once again compatible with JDK 6+
  • 1.1.1 (31.12.2013)
    • fixed bug with filtering all resources instead of only chosen
    • fixed bug with Windows paths in workflow properties
  • 1.1.0 (30.12.2013)
    • integrated workflow as a separate, auto-configured Grunt task
  • 1.0.4 (8.12.2013)
    • explicit declaration of resources filtered on create-resources goal
  • 1.0.3 (24.11.2013)
    • passing custom options to grunt executable
    • ability to use external or preinstalled node_modules
  • 1.0.2 (15.10.2013)
    • option to disable npm and grunt color output, by default no colors are shown as it looks bad in Maven logs
  • 1.0.1 (13.09.2013)
    • compatibility with Maven 3.1.x

License

grunt-maven-plugin is published under Apache License 2.0.

More Repositories

1

bigcache

Efficient cache for gigabytes of data written in Go.
Go
7,122
star
2

ralph

Ralph is the CMDB / Asset Management system for data center and back office hardware.
Python
2,121
star
3

tipboard

Tipboard - in-house, tasty, local dashboarding system
JavaScript
1,105
star
4

php-protobuf

PHP Protobuf - Google's Protocol Buffers for PHP
PHP
912
star
5

hermes

Fast and reliable message broker built on top of Kafka.
Java
787
star
6

allRank

allRank is a framework for training learning-to-rank neural models based on PyTorch.
Python
775
star
7

turnilo

Business intelligence, data exploration and visualization web application for Druid, formerly known as Swiv and Pivot
TypeScript
698
star
8

axion-release-plugin

Gradle release & version management plugin.
Groovy
538
star
9

node-worker-nodes

A node.js library to run cpu-intensive tasks in a separate processes and not block the event loop.
JavaScript
484
star
10

typescript-strict-plugin

Typescript plugin that allows turning on strict mode in specific files or directories.
TypeScript
302
star
11

json-avro-converter

JSON to Avro conversion tool designed to make migration to Avro easier.
Groovy
270
star
12

embedded-elasticsearch

Tool that ease up creation of integration tests with Elasticsearch
Java
269
star
13

vaas

VaaS
Python
229
star
14

allegro-api

Issue tracker and wiki for Allegro REST API
206
star
15

tradukisto

A Java i18n library created to convert numbers to their word representations.
Groovy
195
star
16

marathon-consul

Integrates Marathon apps with Consul service discovery.
Go
189
star
17

restapi-guideline

Allegro REST API Guideline.
CSS
174
star
18

bigflow

A Python framework for data processing on GCP.
Python
114
star
19

handlebars-spring-boot-starter

Spring Boot auto-configuration for Handlebars
Groovy
109
star
20

envoy-control

Envoy Control is a platform-agnostic, production-ready Control Plane for Service Mesh based on Envoy Proxy.
Kotlin
96
star
21

akubra

Simple solution to keep a independent S3 storages in sync
Go
85
star
22

elasticsearch-analysis-morfologik

Morfologik Polish Lemmatizer plugin for Elasticsearch
Java
81
star
23

ecto-cursor-based-stream

Elixir library that allows for cursor-based streaming of Ecto records, that does not require database transaction.
Elixir
71
star
24

opel

OPEL - asynchronous expression language
Java
66
star
25

HerBERT

HerBERT is a BERT-based Language Model trained on Polish Corpora using only MLM objective with dynamic masking of whole words.
60
star
26

fogger

Fogger - a library to create blurred background under Android's UI elements
Java
60
star
27

mesos-executor

Customizable Apache Mesos task executor
Go
49
star
28

selena

SELENA is a tool used to test website performance by measuring response times or verifying the content of replies.
JavaScript
45
star
29

elasticsearch-reindex-tool

Elasticsearch reindexing tool.
Java
44
star
30

kafka-offset-monitor-graphite

Graphite reporter for Kafka Offset Monitor.
Scala
44
star
31

cassandra-modeling-kata

Cassandra Modeling Kata
Java
37
star
32

dotnet-utils

C#
33
star
33

swift-junit

A Swift library for creating JUnit XML test results that can be interpreted by tools such as Bamboo or Jenkins. Macos and Linux ready.
Swift
29
star
34

slinger

Slinger - deep linking library for Android
Java
28
star
35

django-powerdns-dnssec

Django application managing PowerDNS database
Python
27
star
36

ralph-cli

Command-line interface for the Ralph system.
Go
26
star
37

mongo-migration-stream

Tool for online migrations of MongoDB databases.
Kotlin
26
star
38

allms

A versatile and powerful library designed to streamline the process of querying LLMs
Python
26
star
39

allegro.tech

TypeScript
23
star
40

hacktoberfest-dashboard

Allegro Hactoberfest activity dashboard
TypeScript
21
star
41

quanta

Fast image optimization as a service, based on mozjpeg, written in Swift
C
21
star
42

klejbenchmark-baselines

Fine-tuning scripts for evaluating transformer-based models on KLEJ benchmark.
Python
20
star
43

bigcache-bench

Benchmarks for BigCache project
Go
20
star
44

pyhermes

The Python interface to the Hermes message broker.
Python
19
star
45

swiftbox

SwiftBox is a package that helps building Swift/Vapor microservices.
Swift
19
star
46

json-logic-kmp

Kotlin multiplatform JsonLogic expressions evaluation engine. Targets iOS and JVM (also Android).
Kotlin
19
star
47

consul-registration-hook

Hook that can be used for synchronous registration and deregistration in Consul discovery service on Kubernetes or Mesos cluster with Allegro executor
Go
18
star
48

marathon-appcop

Marathon applications law enforcement
Go
18
star
49

newrelic-gradle-plugin

Newrelic Gradle plugin.
Groovy
17
star
50

map-with-indifferent-access

Elixir
17
star
51

grunt-maven-npm

npm tasks for grunt-maven-plugin 1.2+
JavaScript
16
star
52

leader-only-spring-boot-starter

Java
15
star
53

redux-storage-decorator-engines

Composing decorator for redux-storage to use different storage types
JavaScript
15
star
54

spunit

Spunit – Spock elegance in Kotlin JUnit 5 tests
Kotlin
14
star
55

cosmosdb-utils

A collection of useful Azure CosmosDb SDK v3 extensions and utilities, developed as part of Allegro Pay product.
C#
13
star
56

envoy-perf-pprof

Convenient Envoy on-CPU performance analysis with perf and pprof.
Dockerfile
13
star
57

prometheus-net-metrics

C#
12
star
58

dotnet-sdk

C#
12
star
59

camus-compressor

Camus Compressor merges files created by Camus and saves them in a compressed format.
Java
12
star
60

consul-recipes

Java library for interacting with Consul.
Java
12
star
61

blog

SCSS
11
star
62

ralph_pricing

A pricing module for Ralph
Python
11
star
63

solr-fast-collapsing-query-parser

Java
9
star
64

inkpy-jinja

Generate PDF documents from ODT templates.
Python
8
star
65

allegro-tech-labs-microservices

Allegro Tech Labs Microservices workshop materials
Java
8
star
66

klejbenchmark-allegroreviews

Allegro Reviews is a sentiment analysis dataset, consisting of 11,588 product reviews written in Polish and extracted from Allegro.pl - a popular e-commerce marketplace.
8
star
67

TypedListAdapter

Kotlin
7
star
68

logextractx

Python
7
star
69

solr-ids-export-plugin

Java
6
star
70

django-bob

Django bob is a set of django helpers, widgets and form filters for Ralph DCIM/CMDB project .
JavaScript
6
star
71

toper

PHP Rest client based on popular Guzzle Rest Client.
PHP
6
star
72

atm-event-app

ATM event application
JavaScript
5
star
73

application-insights

5
star
74

graphql-extended-audit-intstrumentation

Java
5
star
75

votakvot

Python
5
star
76

banana-split

JavaScript
5
star
77

warsztaty-podstawy-ml-03-2019

Machine learning basics workshop
Jupyter Notebook
4
star
78

selena-agent

Agent for Selena
Python
4
star
79

vaas-registration-hook

Go
3
star
80

swiftbox-config

Swift
3
star
81

braincode

HTML
3
star
82

oauth-mock

Kotlin
3
star
83

atm-hero-generator

JavaScript
2
star
84

swiftbox-metrics-statsd

Swift
2
star
85

eslint-plugin-test-comments

TypeScript
2
star
86

client-side-logic-dsl

Kotlin
2
star
87

parallel-test-execution-workshop

Resources for Parallel test execution workshop
Groovy
2
star
88

couchbase-commons

Kotlin
1
star
89

ml

TypeScript
1
star
90

hermes-page

Hermes OpenSource page.
HTML
1
star
91

json-cache

Java
1
star
92

swiftbox-logging

Swift
1
star
93

podcast.allegro.tech

CSS
1
star
94

axion-release-example

Kotlin
1
star
95

jobs-conf

allegro.tech jobs postings
HTML
1
star
96

allegro-tech-labs-iot

Allegro Tech Labs IoT workshop materials
Python
1
star
97

versionlens.nvim

1
star