KISS

Keep It Simple Stupid

Source Code License Check in Jenkins job

| comments

Updated on 2015-05-03: please check the updated version of the parser setup and build step in this revised post.

When you’re working on an open source project, it’s pretty important for your project to be licensed under an open-source license. I know, picking one is not easy, so here are a couple of sites to help: http://choosealicense.com and https://tldrlegal.com/licenses/tags/Open%20Source.

So, you’ve picked a license, now you need to make sure every source file includes a mention of it. (I don’t know if that’s strictly necessary, but it is highly recommended). You can setup your editor/IDE to include it in a file template, and Jenkins is going to verify every single file has it. Following is my way to set it up (for Objective-C iOS projects, and you can easily tweak it to other languages).

Warnings Plugin

You’ll need to install the Warnings Plug-in: go to Manage Jenkins > Manage Plugins > Available > filter by warning > pick the Warnings Plug-in and hit one of the Install buttons at the bottom.

System setup

Go to Manage Jenkins > Configure System. Find the Compiler Warnings section somewhere towards the bottom, press Add to add a new parser. Here are the parameters:

1
2
3
4
5
6
7
8
9
10
Name: Source Code License Check
Link name: Missing License Warnings
Trend report name: Missing License Trend
Regular Expression: ^(.*)$
Mapping Script:
    import hudson.plugins.warnings.parser.Warning
    String filename = matcher.group(1)
    return new Warning(filename, 1, "License Check", "warning", "Couldn't find the license in the file");
Example Log Message:
    ./AppDelegate.h

Job setup

In your job, add a new Execute shell build step with the contents:

1
grep -L -r --include '*.h' --include '*.m' --include '*.strings' --include '*.xml' --include '*.json' --exclude 'Contents.json' --exclude-dir '*build' --exclude-dir './ExternalFrameworks' 'Licensed under the Apache License, Version 2.0 (the "License");' . > license.check || :

Here:

  • only *.{h,m,strings,xml,json} files are included – I was thinking about an excluding set, but that was bigger than this one. However, I do exclude Contents.json files that are located in .xcassets directories.

  • *build (various build products) and ./ExternalFrameworks (project’s third-party frameworks) directories are excluded.

  • 'Licensed under the Apache License, Version 2.0 (the "License");' is a standard line from your license of choice, Apache 2.0 in this case.

  • There is this construction || : at the end in order not to fail a build if any file is found. (http://stackoverflow.com/questions/9952177/whats-the-meaning-of-the-parameter-e-for-bash-shell-command-line/9952249#9952249)

In the Post-build Actions section, add the Scan for compiler warnings step. Press Add near Scan workspace files. For file pattern enter license.check, and select the Source Code License Check parser. Done. You can build now.

You’ll get a nice interface to check the files:

and even a nice trend graph:

Comments