Recently I have joined a new company as Java developer. One of my first tasks was to rewrite UI for one of the web applications.
Technology stack
Project was built with AngularJS, jQuery, Bootstrap, with manual dependency management and no resource optimizations. In other words, there were a lot of work to do.
In this article, I will show how to setup Maven project for frontend development. So let’s get started.
A Maven plugin that downloads/installs Node and NPM locally, runs NPM install, Grunt, Gulp and/or Karma. It’s supposed to work on Windows, OS X, and Linux.
Include the Maven plugin to your project dependencies:
Inside <executions>...</executions> tag we define goals to install Node.js, npm with dependencies, Bower packages and run Grunt. Resulting pom.xml:
Don’t forget to update .gitignore, unless you actually want to commit node and dependencies, e.g.:
Next we’ll set up project regeneration on file changes. One way to achieve this is to use grunt-contrib-watch, but if you’re using Eclipse, it’s not going to work out of the box. We’ll also need to setup M2E life-cycle mappings to support Eclipse incremental builds.
For this purpose, there are configuration options:
srcdir (optional, for M2Eclipse integration) - a directory to check for changed files before executing in an incremental build
triggerfiles (optional, for M2Eclipse integration) - a list of files to check for changes before executing in an incremental build
outputdir (optional, for M2Eclipse integration) - a directory to refresh when the build completes, so Eclipse sees changed files
Example:
${target.environment} variable depends on your Maven profile. For instance, we are using <target.environment>serve</target.environment> for development and <target.environment>build</target.environment> for production.
Grunt and Bower configuration
We have the next project structure:
With Bower, there is no need to update frontend libraries manually. All this boring work Bower is doing for you. Here is sample Bower configuration file:
To perform all tasks define npm dependencies in package.json:
Declare Grunt tasks in Gruntfile.js. For instance:
With this hack, you can include a regular .css file in your .scss.
Hooray! We are ready to start developing UI without headache. Maybe, it is not the best example, but it shows you how to use frontend maven plugin, install dependencies and define tasks for frontend development.
Feel free to comment this article and express your opinion about this.