Showing posts with label Maven. Show all posts
In this post I would like to give you some introduction how to integrate Bower and Gulp into your Java project. On the beginning I just would like to explain briefly you what Bower and Gulp are.
Bower - package management system for client-side programming on the World Wide Web. It depends on Node.js and npm. It works with git and GitHub repositories. [Wikipedia]
Gulp - fast and intuitive streaming build tool built on Node.js.
First of all we are going to use very useful plugin called frontend-maven-plugin provided under groupId com.github.eirslett [3]. This plugin will allows us to download/install Node and NPM locally for your project, run NPM install, and then any combination of Bower, Grunt, Gulp, Jspm, Karma, or Webpack.
<plugin> <groupId>com.github.eirslett</groupId> <artifactId>frontend-maven-plugin</artifactId> <version>0.0.26</version> <executions> <execution> <id>install node and npm</id> <goals> <goal>install-node-and-npm</goal> </goals> <phase>generate-resources</phase> <configuration> <nodeVersion>v0.12.9</nodeVersion> <npmVersion>2.14.14</npmVersion> </configuration> </execution> <execution> <id>npm install</id> <goals> <goal>npm</goal> </goals> <!-- Uses package.json --> <configuration> <arguments>install</arguments> </configuration> </execution> <execution> <id>bower install</id> <goals> <goal>bower</goal> </goals> <!-- Uses bower.json --> <configuration> <arguments>install</arguments> </configuration> </execution> <execution> <id>gulp build</id> <goals> <goal>gulp</goal> </goals> <!-- Uses gulpfile.js --> <configuration> <arguments>default --env ${gulp.task}</arguments> </configuration> </execution> </executions> </plugin>
As you can see, first of all above plugin will install locally node and npm. Then npm will install all packages from package.json file. After that bower will install packages used by frontend which are defined in bower.json. The last step is gulp which performs actions defined in gulpfile.js.
Below you can find package.json file in which I have defined bower, gulp and gulp plugins versions :
{
"name": "simple-web",
"repository": "git@bitbucket.org:test/test.git",
"version": "1.0.0",
"private": true,
"description": "Simple Web",
"main": "index.js",
"author": "GarciaPL",
"dependencies": {
"bower": "1.7.0",
"gulp": "3.9.0",
"gulp-concat": "2.6.0",
"gulp-uglify": "1.5.1",
"gulp-sourcemaps": "1.6.0",
"gulp-less": "3.0.5",
"gulp-minify-css": "1.2.2",
"gulp-environments": "0.1.1"
}
}
Then let's see what bower.json contains. You can find there defined some plugins from Bower Repository which will be downloaded and installed in bower_components directory :
{
"name": "Simple Web",
"version": "1.0.0",
"private": true,
"ignore": [
"**/.*",
"node_modules",
"bower_components",
"test",
"tests"
],
"dependencies": {
"jquery": "1.11.3",
"underscore": "1.8.3",
"autoNumeric": "1.9.25"
}
}
And the last file is gulpfile.js. In this file you can define special actions which are going to be performed during maven build phase of your Java application. In definition of maven plugin which you might found at the beginning of this post, there is a command : <arguments>default --env ${gulp.task}</arguments>. Variable gulp.task means against what environment application should be build. This variable might be defined in root pom.xml in profile section as a property.
So, according to gulpfile.js for development phase, gulp will provide vendor.map for vendor.js - for this development() method is used. On the other side production phase will provide you uglified vendor.js (compressed) and minified CSS based on base.less - for this one production() method is used. Vendor.js will be placed in /src/main/webapp/static/js/vendor directory, but CSS file might be found in /src/main/webapp/static/css directory.
var gulp = require('gulp');
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
var sourcemaps = require('gulp-sourcemaps');
var less = require('gulp-less');
var minifyCSS = require('gulp-minify-css');
var environments = require('gulp-environments');
var development = environments.development;
var production = environments.production;
gulp.task('default', function () {
console.log('Preparing vendor.js for ' + (production() ? "production" : "development"));
gulp.start('vendorJS');
console.log('Preparing less for ' + (production() ? "production" : "development"));
gulp.start('less');
});
gulp.task('vendorJS', function () {
gulp.src(['bower_components/jquery/dist/jquery.min.js',
'bower_components/underscore/underscore-min.js',
'bower_components/autoNumeric/autoNumeric.js'])
.pipe(development(sourcemaps.init()))
.pipe(concat('vendor.js'))
.pipe(production(uglify()))
.pipe(development(sourcemaps.write('./')))
.pipe(gulp.dest('./src/main/webapp/static/js/vendor'));
});
gulp.task('less', function () {
return gulp.src('src/main/webapp/static/less/base.less')
.pipe(less())
.pipe(production(minifyCSS()))
.pipe(gulp.dest('./src/main/webapp/static/css'));
});
Reference :
[1] Bower.io
[2] Gulpjs.com
[3] Eirslett - frontend-maven-plugin
[4] Npmjs.com
[5] Nodejs.org
My recent project was aimed to develop standalone application written in Swing with embedded database which was in form of JSON file. During building my Jar using Maven, I was faced with problem to replace this JSON file before final application will be produced. Before you will prepare such solution you should get familiar with Maven Build Lifecycle [1]. Below I listed those LifeCycle's :
- validate
- generate-sources
- process-sources
- generate-resources
- process-resources
- compile
- process-classes
- generate-test-sources
- process-test-sources
- generate-test-resources
- process-test-resources
- test-compile
- test
- prepare-package (maven 2.1+)
- package
- pre-integration-test
- integration-test
- post-integration-test
- verify
- install
- deploy
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2</version>
<executions>
<execution>
<id>fetchJSON</id>
<phase>prepare-package</phase>
<inherited>false</inherited>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>pl.avantis.iom.rest.DrupalQueriesImpl</mainClass>
<cleanupDaemonThreads>true</cleanupDaemonThreads>
<daemonThreadJoinTimeout>30000</daemonThreadJoinTimeout>
</configuration>
</plugin>
Reference : [1] Maven Build Lifecycle [2] Codehaus - Exec Maven Plugin
I would like to share a very useful snippet of code which allows you to load image located in resources folder (we talk about project in Maven). I assume that you know how looks project in Maven. For the record it is something like this :
![]() |
| Maven project structure |
In one of my projects there was a need to store images inside in Swing application in .png and .gif format in resources directory (/src/main/resources/). At this moment I place those images manually, but in some near future I will use some library for instance JarPlug or MyJarExplorer to manipulate content of this jar file to automate this process.
Image image = Toolkit.getDefaultToolkit().getImage(getClass().getResource("/resources/dvlogo.png"));
if (image != null) {
ImageIcon icon = new ImageIcon(image);
getMainLogo().setIcon(icon);
}
Above code retrieves image using Toolkit class from Maven's resources directory and create ImageIcon object which is used next in some JFrame to display it.
Reference :
[1] Pastebin Source Code

