Skip to content

Instantly share code, notes, and snippets.

@starstuck
Created July 25, 2013 07:20
Show Gist options
  • Save starstuck/6077536 to your computer and use it in GitHub Desktop.
Save starstuck/6077536 to your computer and use it in GitHub Desktop.
Example of maven project using npm for managing js dependencies, lessc for CSS preprocessing and r.js for js modules assembly.
#!/usr/bin/env node
var exec = require('child_process').exec,
mkdirp = require('mkdirp'),
projectPath = __dirname + '/../..',
targetPath = __dirname + '/../../target/assets';
// Task execution helpers
// ----------------------
function Task(msg, fn) {
if (typeof msg === 'function') {
this.fn = msg;
} else {
this.msg = msg;
this.fn = fn;
}
};
Task.prototype.run = function () {
var msg = this.msg,
fn = this.fn,
next = this.next;
if (msg) {
console.log('Running ' + msg + '...');
}
fn(function(err, out) {
if (err) {
console.error('ERROR: ' + err.toString());
if (out) {
console.info(out);
}
process.exit(1);
return;
}
if (msg) {
console.log('Done\n');
}
if (next) {
next.run();
}
});
};
Task.prototype.than = function (msg, fn) {
this.next = new Task(msg, fn);
return this.next;
};
Task.run = function(msg, fn) {
var task = new Task(msg, fn);
process.nextTick(function() {
task.run();
});
return task;
};
// Build task definitions
// ----------------------
Task.run(function (cb) {
mkdirp(targetPath, cb);
}).than('Less compilation', function (cb) {
exec('node ' + [
projectPath + '/node_modules/.bin/lessc',
'-x',
'-O2',
'--include-paths=' + __dirname + '/../main/css',
projectPath + '/src/main/css/style.less',
targetPath + '/css/style.css'
].join(' '), cb);
}).than('Require.js dependency resolution and minification', function (cb) {
exec('node ' + [
projectPath + '/lib/r.js',
'-o',
projectPath + '/src/main/config/r-js.json',
'baseUrl=' + projectPath + '/src/main/js',
'dir=' + targetPath + '/js'
].join(' '), cb);
});
{
"name": "...",
"descrition": "...",
"version": "1.0.0",
"private": true,
"devDependencies": {
"express": "3.2.x",
"less": "1.3.x",
"mocha": "1.11.x",
"socket.io": "0.9.x",
"mkdirp": "0.3.x"
},
"config": {
},
"scripts": {
"test": "mocha test",
"prepublish": "node build.js"
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1.1</version>
<executions>
<!-- Install all npm dependencies and call prepublish script in maven generate-resources pahase -->
<execution>
<id>npm-install</id>
<phase>generate-resources</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>npm</executable>
<arguments>
<argument>install</argument>
</arguments>
</configuration>
</execution>
<!-- Run npm tests in maven test phase -->
<execution>
<id>npm-test</id>
<phase>test</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>npm</executable>
<arguments>
<argument>test</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment