Travis

Development
Topics
Overview
Philosophy
Architecture
Source code
Project management
Coding style
Debugging
Tools
GitHub
Git
Maven
IDEs
Jenkins
Travis
Dotfiles
Guides
Writing plugins
ImageJ Ops
Contributing to a plugin
Distributing your plugins
Development lifecycle
Building a POM
Developing with Eclipse
Hands-on debugging
Adding new ops
Adding new formats
Using native libraries
Tips for developers
Tips for C++ developers
ImageJ 1.x plugins
Versioning
Logging
Uber-JARs
Travis CI: Build your code in the cloud!
Travis is a tool for continuous integration. It has excellent integration with GitHub, making it a viable alternative to Jenkins for automating tasks.

Automatic Deployment of Maven Artifacts

Deploying your library to Maven makes it available for other developers. It is also a contribution requirement for the Fiji project.

Requirements

  • Host your open-source project on GitHub.
  • Log in to Travis CI with your corresponding GitHub account and enable your repository.
  • Contact an ImageJ admin in Gitter or the ImageJ forum and request that they file a PR which adds Travis support to your repository.

Instructions


  1. Use the following code for your repository's .travis.yml:
    language: java
    jdk: oraclejdk8
    branches:
      only: master
    install: true
    script: "./travis/build.sh"
    after_success: "./travis/notify.sh Travis-Success"
    after_failure: "./travis/notify.sh Travis-Failure"
    
  2. From the command line in your repository, create an encrypted environment variable for MAVEN_PASS which defines the Maven password of the ImageJ Maven repository's travis account. Then do the same for the TOKEN_NAME of the ImageJ Jenkins Travis-Success and Travis-Failure jobs.
    $ travis encrypt MAVEN_PASS=travis_maven_password --add env.global
    $ travis encrypt TOKEN_NAME=imagej_jenkins_token --add env.global
    

    Note that this will automatically modify your .travis.yml file.

  3. Add the following script as .travis/build.sh:
    #!/bin/sh
    dir="$(dirname "$0")"
    test "$TRAVIS_SECURE_ENV_VARS" = true \
      -a "$TRAVIS_PULL_REQUEST" = false \
      -a "$TRAVIS_BRANCH" = master &&
      mvn -Pdeploy-to-imagej deploy --settings "$dir/settings.xml" ||
      mvn install
    
  4. Add the following script as .travis/notify.sh:
    #!/bin/sh
    curl -fs "https://jenkins.imagej.net/job/$1/buildWithParameters?token=$TOKEN_NAME&repo=$TRAVIS_REPO_SLUG&commit=$TRAVIS_COMMIT&pr=$TRAVIS_PULL_REQUEST"
    
  5. Create a settings.xml in your repository with the following contents:
    <settings>
      <servers>
        <server>
          <id>imagej.releases</id>
          <username>travis</username>
          <password>${env.MAVEN_PASS}</password>
        </server>
        <server>
          <id>imagej.snapshots</id>
          <username>travis</username>
          <password>${env.MAVEN_PASS}</password>
        </server>
      </servers>
    </settings>
    
  6. Commit all your files with Git and push them back to your repository. If everything works as intended, you should start seeing new builds in your Travis dashboard with each new commit, with corresponding SNAPSHOT artifacts automatically deployed to maven.imagej.net.
  1. Add the following code to your build.gradle file:
    // this _must_ be at the top, with the other plugins
    apply plugin: 'maven'
    apply plugin: 'maven-publish'
    
    // more towards the end
    uploadArchives {
        repositories {
            mavenDeployer {
                repository(url: "http://maven.imagej.net/content/repositories/releases") {
                    authentication(userName: "$System.env.MAVEN_USER", password: "$System.env.MAVEN_PASS")
                }
                snapshotRepository(url: "http://maven.imagej.net/content/repositories/snapshots") {
                    authentication(userName: "$System.env.MAVEN_USER", password: "$System.env.MAVEN_PASS")
                }
            }
        }
    }
    

    If you also want your Javadoc JAR to be published, also add the following:

    task javadocJar(type: Jar, dependsOn:javadoc) {
            classifier = 'javadoc'
            from javadoc.destinationDir
    }
    
    artifacts
    {
            archives sourcesJar
            archives javadocJar
        archives testsJar
    }
    
    publishing {
            publications {
                    maven(MavenPublication) {
                            from components.java
                            artifact sourcesJar { classifier "sources" }
                    }
            }
    }
    
  2. From the command line in your repository, create an encrypted environment variable for your maven.imagej.net username and password—be careful with escaping special characters correctly:
    $ travis encrypt MAVEN_USER=my_maven_username --add env.matrix
    $ travis encrypt MAVEN_PASS=my_maven_password --add env.matrix
    

    Note that this will automatically modify your .travis.yml file.

  3. Commit all your files with Git and push them back to your repository. If everything works as intended, you should start seeing new builds in your Travis dashboard with each new commit, with corresponding SNAPSHOT and release artifacts immediately deployed automatically to maven.imagej.net.

Testing things which cannot run headless

If your tests require a display (i.e.: they do not pass when run headless), you can use Xvfb as follows:

before_script:
  - "export DISPLAY=:99.0"
  - "sh -e /etc/init.d/xvfb start"
  - sleep 3 # give xvfb some time to start

Of course, you should do this only as a last resort, since the best unit tests should not require a display in the first place.