Category Archives: maven

Notification when maven build finishes

Maven builds often take some time. This often causes me to start doing something else (read mail, browse web), which makes me unaware of when the build completes and I can continue working.

There should be a way to fix this. On Ubuntu, you get notifications when mail arrives, when network is up/down etc. Why not use this same system to get notifications when the maven build completes.

Maven notification window

Maven notification window

You can do this by adding the following in your ~/.bash_aliases file:

alias maven="command mvn"
notified_maven() {
  maven $* ; notify-send --icon=message-im "mvn" "build finished"    
}
alias mvn=notified_maven

Update
Thanks to Adrien, you can also use a more advanced version, showing the build result and maven parameters.

alias maven="command mvn"
notified_maven() {
  maven $* | \
  perl -pe'$m|=/BUILD .*SUCCESS/; END {exit!$m}' && \
  notify-send --icon=face-cool "`basename $(pwd)`: mvn $*" "Build SUCCESS" || \
  notify-send --icon=face-crying "`basename $(pwd)`: mvn $*" "Build FAILED"
}
alias mvn=notified_maven

Adding colour to maven

Thanks to @bertvanhooff, I found a link to a script to get colorized maven output in bash, see https://github.com/builddoctor/maven-antsy-color. Very nice.

I did have quite a bit of trouble to get this working in Ubuntu using gnome-terminal.

  • For some reason neither copy/paste of the original script or file copying after a git clone worked. I had to replace the escape characters in the script by “\x1b” (without the quotes).
  • gnome-terminal by on Ubuntu runs as a login shell, meaning that .bashrc is not read at startup. This can be changed in the “default” profile, on the “title and command” tab.

Note however that this can play havoc with some plug-ins. Specifically, the maven release plug-in will hang when releasing from a git repository when maven output is colourized. Starting a new console using bash -login can do the trick.

Running PostgreSQL database tests on DEV@CloudBees

In our project, we drop and recreate the database for doing our tests. This is controlled by our maven build and can happen for any module in the build. This is done using the following maven configuration:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>sql-maven-plugin</artifactId>
    <version>1.5</version>

    <dependencies>
        <!-- specify the dependent jdbc driver here -->
        <dependency>
            <groupId>postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>${postgresql-version}</version>
        </dependency>
    </dependencies>

    <!-- common configuration shared by all executions -->
    <configuration>
        <driver>org.postgresql.Driver</driver>
        <url>jdbc:postgresql://${dbHostPort}/postgres</url>
        <username>${dbAdminUser}</username>
        <password>${dbAdminPassword}</password>
    </configuration>

    <executions>
        <execution>
            <id>drop and create</id>
            <goals>
                <goal>execute</goal>
            </goals>
            <phase>generate-test-resources</phase>
            <configuration>
                <!-- need another database to drop/create the targeted one -->
                <url>jdbc:postgresql://${dbHostPort}/postgres</url>
                <autocommit>true</autocommit>
                <sqlCommand>
                    drop database myproject_test;
                    create database myproject_test encoding 'UTF8';
                    CREATE ROLE myproject LOGIN ENCRYPTED PASSWORD 'myproject'
                        SUPERUSER INHERIT CREATEDB NOCREATEROLE;
                    alter database myproject_test OWNER TO myproject;
                    grant all on database myproject_test to myproject;
                </sqlCommand>
                <onError>continue</onError>
            </configuration>
        </execution>
    </executions>
</plugin>

When trying to run this build on CloudBees, there were some problems. Our project uses PostgreSQL. This may not be immediately clear, but it is supported as documented on their site.

You basically include a pre-build step to assure that PostgreSQL is installed and (according to their page) that your database is up. However, my build script created the database itself and need the postgres superuser for access.

So I adapted the script to assure that the postgres users can be used from the maven db plugin:

######### POSTGRES INSTAL(first time) AND LAUNCH #########
curl -s -o use-postgresql https://repository-cloudbees.forge.cloudbees.com/distributions/ci-addons/use-postgresql
POSTGRESQL_VERSION=9.0.4 \
source ./use-postgresql

### create "postgres" user
psql -d postgres -c "DROP USER IF EXISTS postgres;"
psql -d postgres -c "CREATE ROLE postgres LOGIN ENCRYPTED PASSWORD 'postgres' SUPERUSER INHERIT CREATEDB;"

######### THIS CODE NOW EXITS. #########
## If you have other 'M2 PreBuild' steps following this 
## in your configuration, they will now execute
##
## Eventually, control is given to the main build and
## the PostgreSQL server will still be running, ready
## to use!