gulp-sonar2
v3.2.0
Published
Sonar runner for gulp.
Maintainers
Readme
gulp-sonar2
Run SonarQube analysis from a gulp task. This plugin bundles the SonarScanner CLI and drives it for you, so all you need to provide is your project configuration.
Legacy line (3.x). This release keeps the original, battle-tested behaviour: it ships the bundled SonarScanner CLI 3.1 and runs it via Java. It is intended for existing users who want a maintained drop-in with up-to-date dependencies. If you are starting fresh or targeting a modern SonarQube server, consider the next-generation version (see Roadmap).
Requirements
- Node.js >= 12
- A Java Runtime Environment on the
PATH(the bundled SonarScanner CLI 3.1 is a Java tool and needsjavato run) - Access to a running SonarQube server
Install
npm install --save-dev gulp-sonar2Usage
const gulp = require('gulp');
const sonar = require('gulp-sonar2');
const log = require('fancy-log');
gulp.task('sonar', function () {
const options = {
sonar: {
host: {
url: 'http://localhost:9000'
},
// Authentication token for the SonarQube server (recommended).
login: process.env.SONAR_TOKEN,
projectKey: 'my-project',
projectName: 'My Project',
projectVersion: '1.0.0',
// Comma-delimited string of source directories.
sources: 'src/app/js,src/libs/js',
language: 'js',
sourceEncoding: 'UTF-8',
javascript: {
lcov: {
reportPath: 'test/sonar_report/lcov.info'
}
},
exec: {
// Passed straight to child_process.exec (see:
// https://nodejs.org/api/child_process.html#child_processexeccommand-options-callback ).
// Raise this if the scanner produces a lot of output; exceeding
// maxBuffer kills the child process and fails the task.
maxBuffer: 1024 * 1024
}
}
};
// The gulp source is ignored; every file the scanner needs is referenced
// through the options object above.
return gulp.src('thisFileDoesNotExist.js', { read: false })
.pipe(sonar(options))
.on('error', log.error);
});Options
Everything under options.sonar is flattened into sonar.* analysis
properties and written to the scanner's properties file. Nested objects become
dotted keys, so:
host: { url: 'http://localhost:9000' }becomes sonar.host.url=http://localhost:9000.
Defaults applied for you:
sonar.sourceEncodingdefaults toUTF-8sonar.host.urldefaults tohttp://localhost:9000
The exec key is special: it is not sent to the scanner, but forwarded to
Node's child_process.exec so you can tune things like maxBuffer, cwd, or
env.
For the full list of analysis parameters, see the SonarScanner documentation.
Error handling
The plugin emits errors on the stream rather than throwing, so attach an
error handler (as shown above). The task fails if the properties file cannot
be written, if java cannot be launched, or if the scanner exits with a
non-zero code.
Roadmap
A next-generation rewrite (gulp-sonar-next) is planned that drops the bundled
Java scanner in favour of the officially maintained Node scanner, which
downloads the correct SonarScanner automatically and works with current
SonarQube servers. The 3.x line documented here remains available for existing
integrations.
Credit
Inspired by grunt-sonar-runner by Suresh Khatri. Forked from gulp-sonar by carsdotcom.
