This plug-in generates trend report for
[SLOCCount](http://www.dwheeler.com/sloccount/) and
[cloc](http://cloc.sourceforge.net/) open source tools, that count
number of code lines written in many programming languages.
# Description
The SLOCCount plug-in scans for
[SLOCCount](http://www.dwheeler.com/sloccount/) or
[cloc](http://cloc.sourceforge.net/) report files in the build workspace
directory and reports the number of lines they found.
The following features are provided by this plug-in:
- Parsing of reports generated by
[SLOCCount](http://www.dwheeler.com/sloccount/) tool.
- Parsing of reports generated by [cloc](http://cloc.sourceforge.net/)
tool.
- Configuration of the files to scan after a build.
- Trend reports showing the number of lines per language.
- Trend reports showing the number of added or deleted lines per
language.
- Overview of the lines per file, folder or language.
- [Dashboard
View](https://wiki.jenkins.io/display/JENKINS/Dashboard+View) portlet
showing number of lines, files and languages per job.
- Remote access API ([REST
API](https://wiki.jenkins-ci.org/display/JENKINS/Remote+access+API)).
- Works with any project/job type.
This plug-in is developed and maintained by Nicolas Piguet. Please use
the [Mailing Lists](https://wiki.jenkins.io/display/JENKINS/Mailing+Lists)
or [issue tracker](http://issues.jenkins-ci.org/) to ask questions,
create feature request or bug reports, since I don't read the comment
section on this page regularly.
# Screenshots
[TABLE]
# Usage
If you are not sure which type of report files to use, prefer **cloc**.
It is able to detect more programming languages than SLOCCount (Scala,
Ant, CSS, ...) and is able to count also lines with comments that often
contain Javadoc or Doxygen documentation. Cloc is also better portable,
SLOCCount requires [cygwin](https://www.cygwin.com/) or similar
environment under MS Windows.
**Never switch between SLOCCount and cloc inside one job.** You would
end up with messy trend graph because they name programming languages
differently. Delete all affected builds and rebuild the job in such case
to fix the issue.
## Linux
- Install [SLOCCount](http://www.dwheeler.com/sloccount/) or
[cloc](http://cloc.sourceforge.net/) on the build slave machine.
Note they may be present in package repository of your distribution.
- Debian, Ubuntu, ...
- `apt-get install sloccount`
- `apt-get install cloc`
- Red Hat, CentOS, Fedora, ...
- `yum install sloccount`
- `yum install cloc`
- For each job configuration page:
- Add a **execute shell** build step to generate the report files,
or create them in your build scripts (Ant, Maven, Make, ...).
- `sloccount --duplicates --wide --details [source-folder] > [output-folder]/sloccount.sc`
- `cloc --by-file --xml --out=[output-folder]/cloc.xml [source-folder]`
- Activate the checkbox **Publish SLOCCount analysis results** and
provide the path to the generated report files.
## Windows
### cloc
- Download and install [cloc](http://cloc.sourceforge.net/)
([cloc.exe](http://sourceforge.net/projects/cloc/files/cloc/)) on
the build slave machine. Optionally if you have Perl installed, you
can run cloc as a standalone Perl Script.
- For each job configuration page:
- Generate the report files
- `cloc.exe --by-file --xml --out=[output-folder]/cloc.xml [source-folder]`
- `perl cloc.pl --by-file --xml --out=[output-folder]/cloc.xml [source-folder]`
- Activate the checkbox **Publish SLOCCount analysis results** and
provide the path to the generated cloc report.
### SLOCCount
Since SLOCCount does not run on a native Windows machine the setup for
Windows requires additional steps. Most of the setup is explained in
detail in the [SLOCCount
documentation](http://www.dwheeler.com/sloccount/sloccount.html).
- Install [Cygwin](https://www.cygwin.com/) on the build slave.
- Compile and install SLOCCount within the Cygwin environment.
- For each job configuration page:
- Add a build step to run Cygwin and SLOCCount. See the ANT
example below to learn how to start Cygwin and run SLOCCount
from an ANT script.
- `/usr/bin/sloccount --duplicates --wide --details [source-folder] >[output-folder]/sloccount.sc`
You must convert Windows paths that are passed as parameters
to SLOCCount utility to Linux/Cygwin style (C:\\ becomes
/cygdrive/c/).
- Convert the SLOCCount results in sloccount.sc back to windows
paths.
- Activate the checkbox **Publish SLOCCount analysis results** and
provide the path to the generated SLOCCount report.
If you uses sloccount for multiple jobs sometimes two jobs end at the
same time and sloccount is executed simultaneously. This will fail since
SLOCCount (by default) uses one temp data folder in $HOME/.slocdata.
Either change sloccount to not use a temp data directory or change the
setup explanation so novice users (like me) are explained to use the
--datadir option in order to get a datadir per hudson job.
This seems to work:
\[ -d slocdata \] \|\| mkdir slocdata
sloccount --datadir slocdata --details "your source path" \>
sloccount.sc
#### ANT example for Windows build
The target converts Windows paths to Linux style cygdrive paths (C:\\
becomes /cygdrive/c/) before SLOCCount runs. The converted paths are
passed as environment variables to Cygwin. After SLOCCount has been
executed, the results in sloccount.sc are converted back to windows
paths.
#### ANT target
Add this target to your Jenkins build:
**build.xml**
``` syntaxhighlighter-pre
```
You may have to adjust the paths used in the snippet, if your Jenkins
workspace is on another drive than C.
#### Shell script
This shell script is called by Cygwin / ANT.
**cygwin\_sloccount.sh**
``` syntaxhighlighter-pre
sloccount --duplicates --wide --details "$SLOCCOUNT_SOURCE_DIR" > "$SLOCCOUNT_OUTPUT_DIR/sloccount.sc"
```
# Remote access API ([REST API](https://wiki.jenkins-ci.org/display/JENKINS/Remote+access+API))
Use the URLs below to access the SLOCCount specific data.
Description of possibilities
- [http://SERVER/job/JOB\_NAME/BUILD\_NUMBER/sloccountResult/api](http://server/job/JOB_NAME/BUILD_NUMBER/sloccountResult/api)
Data
- [http://SERVER/job/JOB\_NAME/BUILD\_NUMBER/sloccountResult/api/xml](http://server/job/JOB_NAME/BUILD_NUMBER/sloccountResult/api/xml)
- [http://SERVER/job/JOB\_NAME/BUILD\_NUMBER/sloccountResult/api/xml?depth=1](http://server/job/JOB_NAME/BUILD_NUMBER/sloccountResult/api/xml?depth=1)
- [http://SERVER/job/JOB\_NAME/BUILD\_NUMBER/sloccountResult/api/json](http://server/job/JOB_NAME/BUILD_NUMBER/sloccountResult/api/json)
- [http://SERVER/job/JOB\_NAME/BUILD\_NUMBER/sloccountResult/api/json?depth=1](http://server/job/JOB_NAME/BUILD_NUMBER/sloccountResult/api/json?depth=1)
- etc.
Example of XML data:
7
513
xml
116
8174
java
123
2
8687
Example of JSON data:
{
"languages":
[
{"files":7,"lines":513,"name":"xml"},
{"files":116,"lines":8174,"name":"java"},
],
"totalFiles":123,
"totalLanguages":2,
"totalLines":8687
}
# Support for cloc tool
Parsing of cloc XML reports is supported natively in the plugin from
version 1.20.
[Cloc](http://cloc.sourceforge.net/) (Count Lines of Code) is a tool
similar to SLOCCount. It provides output to a XML file that can be
simply transformed to the SLOCCount format and used in this plugin. Cloc
is written in Perl and should be better portable than SLOCCount.
This howto was created and tested on Debian GNU/Linux, Jenkins 1.447,
SLOCCount plugin 1.12, cloc 1.60 and xsltproc 1.1.28.
### Install cloc and xsltproc
Download cloc from and install it. You
need also a whatever tool for XML transformations (xsltproc used) or you
can try [clokins](https://gitorious.org/clokins) tool (not tested at
all). You can install the packages present in your GNU/Linux
distribution.
``` syntaxhighlighter-pre
# Debian/Ubuntu specific
apt-get install cloc xsltproc
```
### Script for XSLT transformation
Copy-paste the script below and store it to cloc2sloccount.xsl file
accessible by your Jenkins server. If you are using master and slave
machines, store the script to all slaves where the jobs are executed.
**cloc2sloccount.xsl**
``` syntaxhighlighter-pre
For more details see:
This report has been generated by cloc .
```
### Execute cloc
Change configuration of your job to execute the following shell script.
Update the path to the directory where your source files are stored and
optionally pass [additional
parameters](http://cloc.sourceforge.net/#Options) to cloc. Then execute
XSLT transformation on cloc output to get a SLOCCount compatible report.
``` syntaxhighlighter-pre
cloc --by-file --xml -out=cloc.xml DIRECTORY
xsltproc PATH/TO/cloc2sloccount.xsl cloc.xml > sloccount.sc
```
Enable SLOCCount publisher, enter path to the generated SLOCCount report
and that's all.
![](docs/images/cloc_config.png)
![](docs/images/cloc_trend.png)
### Using the plugin within a Jenkins pipeline
Configure your pipeline in a similar way to this example:
**Jenkinsfile**
``` syntaxhighlighter-pre
node('master') {
stage ("Analyse") {
sh 'sloccount --duplicates --wide --details path-to-code/ > sloccount.sc'
}
stage ("Publish reports") {
sloccountPublish encoding: '', pattern: ''
}
}
```
# Known Issues
type
key
summary
priority
status
resolution
created
updated
Data cannot be retrieved due to an unexpected error.
[View these issues in
Jira](https://issues.jenkins-ci.org/secure/IssueNavigator.jspa?reset=true&jqlQuery=project%20=%20JENKINS%20AND%20status%20in%20%28Open,%20%22In%20Progress%22,%20Reopened%29%20AND%20component%20=%20%27sloccount-plugin%27&tempMax=1000&src=confmacro)
# Change Log
### Version 1.24 (Dec 22, 2017)
- Fixed: Concurrent builds are serialized when publishing
([JENKINS-43658](https://issues.jenkins-ci.org/browse/JENKINS-43658))
### Version 1.23 (Nov 26, 2017)
- Fixed: Lines and comments value are inversed in folders tab
([JENKINS-37056](https://issues.jenkins-ci.org/browse/JENKINS-37056))
### Version 1.22 (Nov 12, 2017)
- Implemented: Support the new workflow/pipeline model
([JENKINS-35234](https://issues.jenkins-ci.org/browse/JENKINS-35234))
### Version 1.21 (Nov 3, 2015)
- Implemented: Get total comment count in SloccountReportStatistics
([JENKINS-31221](https://issues.jenkins-ci.org/browse/JENKINS-31221)).
- Only in cloc based reports, pull request by Jim SERRA.
### Version 1.20 (Sep 6, 2014)
- Implemented: Native support for cloc tool
([JENKINS-24602](https://issues.jenkins-ci.org/browse/JENKINS-24602)).
- Implemented: Folding help in configuration
([JENKINS-24094](https://issues.jenkins-ci.org/browse/JENKINS-24094)).
- Implemented: Clean jelly code
([JENKINS-24093](https://issues.jenkins-ci.org/browse/JENKINS-24093)).
- Implemented: Show statistics from last build on main job page
([JENKINS-24021](https://issues.jenkins-ci.org/browse/JENKINS-24021)).
### Version 1.19 (Apr 15, 2014)
- Fixed: Error 404 when trying to access language details for "C/C++
Header"
([JENKINS-22597](https://issues.jenkins-ci.org/browse/JENKINS-22597)).
### Version 1.18 (Apr 5, 2014)
- Implemented: Should have option to not care about build failure
status
([JENKINS-22303](https://issues.jenkins-ci.org/browse/JENKINS-22303)).
### Version 1.17 (Apr 2, 2014)
- Unsuccessful.
### Version 1.16 (Mar 14, 2014)
- Fixed: Lazy loaded report details are never released
([JENKINS-21921](https://issues.jenkins-ci.org/browse/JENKINS-21921)).
- Fixed: Fix findings from FindBugs static analysis
([JENKINS-22160](https://issues.jenkins-ci.org/browse/JENKINS-22160)).
- Implemented: Remote access API (REST API)
([JENKINS-21922](https://issues.jenkins-ci.org/browse/JENKINS-21922)).
### Version 1.15 (Feb 8, 2014)
- Fixed: Possibly too wide table on summary page
([JENKINS-21557](https://issues.jenkins-ci.org/browse/JENKINS-21557)).
- Fixed: Sorting using Distribution column/graph doesn't work
([JENKINS-21700](https://issues.jenkins-ci.org/browse/JENKINS-21700)).
- Implemented: Configurable Graph
([JENKINS-21552](https://issues.jenkins-ci.org/browse/JENKINS-21552)).
- Implemented: New Modules panel on report page
([JENKINS-21697](https://issues.jenkins-ci.org/browse/JENKINS-21697)).
### Version 1.14 (Jan 26, 2014)
- Implemented: sloccount report formatting on build summary
([JENKINS-13235](https://issues.jenkins-ci.org/browse/JENKINS-13235)).
- Implemented: Create a dash-board view portlet that shows the SLOC of
the selected projectes
([JENKINS-6876](https://issues.jenkins-ci.org/browse/JENKINS-6876)).
- Implemented: add a sloccount portlet to dashboard
([JENKINS-12166](https://issues.jenkins-ci.org/browse/JENKINS-12166)).
- Rejected: Support multimodule sloccount report
([JENKINS-13382](https://issues.jenkins-ci.org/browse/JENKINS-13382)).
### Version 1.13 (Jan 24, 2014)
- Fixed: Unable to copy the sloccount summary when the job has been
executed on a slave with a different OS
([JENKINS-21467](https://issues.jenkins-ci.org/browse/JENKINS-21467)).
- Fixed: Link to workspace root directory is not accessible in results
([JENKINS-21500](https://issues.jenkins-ci.org/browse/JENKINS-21500)).
- Implemented: Can this plugin support CLOC for even more languages
and better Windows support
([JENKINS-10274](https://issues.jenkins-ci.org/browse/JENKINS-10274)).
- Existing information in the task tested, a howto about
[cloc](http://cloc.sourceforge.net/) support written and added
to wiki.
### Version 1.12 (Jan 18, 2014)
- Fixed: Can't open sloccount table portlet
([JENKINS-21419](https://issues.jenkins-ci.org/browse/JENKINS-21419)).
- Release for MS Windows, all long-term issues with Windows
backslashes '\\' in paths should be fixed now.
- Outdated information about plugin version \<= 1.6 removed from wiki
to clean it up.
### Version 1.11 (Jan 15, 2014)
- Fixed: Memory consumption is huge
([JENKINS-4769](https://issues.jenkins-ci.org/browse/JENKINS-4769)).
- Changes in data storage that heavily reduces memory consumption
and increases performance during a common Jenkins use.
- Stored data now contains statistics per language (just several
numbers), full report is lazy-loaded only while it is needed in
the results page. Compare with parsing of giant XML that
contains information about each source file present in your
sandbox multiplied by number of builds just to show a simple
trend graph.
- Data format is very different but the code should satisfy a
backward compatibility, migration tested with plugin versions
1.8 and 1.10.
- The plugin update will have full impact after builds from a
previous version will completely role out and only new data are
present.
- Fixed: NoClassDefFoundError exception on job configuration page
([JENKINS-19629](https://issues.jenkins-ci.org/browse/JENKINS-19629)).
- Fixed: Support for matrix projects
([JENKINS-12816](https://issues.jenkins-ci.org/browse/JENKINS-12816))
and IVY projects
([JENKINS-17024](https://issues.jenkins-ci.org/browse/JENKINS-17024)).
- All project types should be supported now.
- Unnecessary dependencies to Maven and FindBugs removed.
- Fixed: Summary is missing for the first build
([JENKINS-20646](https://issues.jenkins-ci.org/browse/JENKINS-20646)).
- Fixed: Broken link in menu on the job page before the first build
([JENKINS-21174](https://issues.jenkins-ci.org/browse/JENKINS-21174)).
- Fixed: Broken differences in build summary for failed builds
([JENKINS-21223](https://issues.jenkins-ci.org/browse/JENKINS-21223)).
- Fixed: Possible resource leak in parser
([JENKINS-21229](https://issues.jenkins-ci.org/browse/JENKINS-21229)).
- Fixed: Broken HTML code on results page
([JENKINS-21230](https://issues.jenkins-ci.org/browse/JENKINS-21230)).
- Fixed: Graphical artifacts in the trend graphs
([JENKINS-21258](https://issues.jenkins-ci.org/browse/JENKINS-21258)).
- Implemented: SLOCCount churn graph
([JENKINS-14504](https://issues.jenkins-ci.org/browse/JENKINS-14504)).
- New trend graph with differences in lines count between current
and previous build.
- Wiki updates - information, screenshots.
### Version 1.10 (Apr 16, 2013)
- fixed java.io.InvalidClassException
([JENKINS-14255](https://issues.jenkins-ci.org/browse/JENKINS-14255)).
- Made the plugin applicable for Job Generator jobs.
### Version 1.8 (Jun 16, 2012)
- sloccount trend report only works up to last failed build
([JENKINS-9309](https://issues.jenkins-ci.org/browse/JENKINS-9309)).
- SLOCCount plugin fails with Windows/Cygwin SLOCCount execution
([JENKINS-4836](https://issues.jenkins-ci.org/browse/JENKINS-4836)).
### Version 1.7 (May 19, 2012)
- String index out of range
([JENKINS-13775](https://issues.jenkins-ci.org/browse/JENKINS-13775)).
- added Japanese localization.
- use commas in numbers for readability
([JENKINS-13235](https://issues.jenkins-ci.org/browse/JENKINS-13235)).
- sort lines and files numerically.
- fixed breadcrumb move down.
### Version 1.6 (Oct 19, 2011)
- Allow Maven support
### Version 1.5 (Feb 14, 2011)
- Update link in help
- Translation updates (French, Dutch)
### Version 1.4 (Feb 10, 2010)
- Update code for more recent Hudson
### Version 1.3 (Jul 7, 2009)
- Made to work with distributed builds
([patch](http://www.nabble.com/sloccount-plugin-and-distributed-builds--%3E-exception-td22791316.html))
### Version 1.2 (Feb 28, 2009)
- Clicking on the SLOCCount icon in the left sidebar will now take you
directly to the last build's detailed sloccount report instead of
that build's general main page
### Version 1.1 (Feb 24, 2009)
- Added breadcrumbs to help in the navigation of the result report
### Version 1.0 (Feb 20, 2009)
- Initial release