Coming Up for Air

2013

September

  • Gradle Tip: Running a Single Test

    Using Maven, to run a single test (class), you would issue mvn -Dtest=MyTest. Gradle has similar functionality (gradle -Dtest.single=MyTest), though it seems to be much more powerful. You can get all the details here.

  • Gradle Tip: Better Test Debugging

    In a recent post, I showed how to attach a debugger to tests run from the command line via Gradle. While it worked, it turns out that it’s a bit over kill. Try this instead:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    $ gradle -Dtest.debug test
    :compileJava
    :processResources UP-TO-DATE
    :classes
    :compileTestJava
    :processTestResources
    :testClasses
    :test
    Listening for transport dt_socket at address: 5005
    

    Attach your debugger to port 5005, and off you go. No need to modify your build. Kudos to the NetBeans debugger output for helping me find that. ;)

  • Gradle + Arquillian + GlassFish Embedded

    I’ve recently been migrating all of my personal projects to Gradle. Since I use Arquillian, that means migrating that part of the build as well. However, being still fairly new to Gradle, how to handle that integration wasn’t immediately obvious. Thanks to Benjamin Muschko and Aslak Knutsen, I’ve finally gotten a working setup.

  • Gradle Tip: Seeing Standard Streams During Tests

    I’m not a real big fan of using standard out as a debugging strategy (I prefer an IDE and break points, for what it’s worth), but there are times when it’s either necessary or just convenient. The standard Gradle configuration, though, makes this a bit more difficult than it probably should be. Fortunately, Gradle also makes it easy to change:

    build.gradle
    1
    2
    3
    
    test {
        testLogging.showStandardStreams = true
    }
    

    If you’d like to make this change globally, that’s also easy:

    $HOME/.gradle/init.gradle
    1
    2
    3
    4
    5
    
    allprojects {
        tasks.withType(Test) {
            testLogging.showStandardStreams = true
        }
    }
    
  • Gradle Tip: Attaching a Debugger

    Maven offers a nice script to allow for attaching a debugger to your build, mvnDebug. Gradle does not. Again, though, Gradle makes it pretty easy to add this to your build.

  • Building \"Fat Jars\" with Gradle

    Sometimes, such as when building command line Java apps, it would be nice to bundle all of the app’s dependencies in a single jar so that the user need not collect and manage these. With Gradle, that can be easily accomplished with the following lines:

    build.gradle
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    
    jar {
        from {
            configurations.compile.collect {
                it.isDirectory() ? it : zipTree(it)
            }
            configurations.runtime.collect {
                it.isDirectory() ? it : zipTree(it)
            }
        }
    }
    

    When you run gradle assemble, you should find your now very hefty jar in build/libs.

August

  • Gradle, 'provided' scope, and Java EE 7

    Maven has a dependency scope, provided, that indicates that the dependency should not be in the archive. Gradle does not provide such a scope out of the box, but it’s easy enough to add. The following Gradle build demonstrates a very bare-bones Java EE 7 web application setup:

    build.gradle
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    
    apply plugin: 'war'
    
    repositories {
        mavenCentral()
        mavenLocal()
    }
    
    configurations {
        provided
    }
    sourceSets {
        main { compileClasspath += configurations.provided }
    }
    
    dependencies {
        provided 'javax:javaee-api:7.0'
    }
    
  • A Quick-start for Scala and Gradle

    For those interested, here’s a quick and simple project to get you started using Gradle and Scala together:

    build.gradle
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    
    apply plugin: 'scala'
    
    repositories{
        mavenCentral()
        mavenLocal()
    }
    
    dependencies{
        compile 'org.slf4j:slf4j-api:1.7.5'
        compile "org.scala-lang:scala-library:2.10.1"
        testCompile "junit:junit:4.11"
    }
    
    task run(type: JavaExec, dependsOn: classes) {
        main = 'Main'
        classpath sourceSets.main.runtimeClasspath
        classpath configurations.runtime
    }
    
    src/main/scala/Main.scala
    1
    2
    3
    
    object Main extends App {
      println("Hello, world")
    }
    

    You can run the app using the custom task run:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    
    $ gradle run
    :compileJava
    :compileScala
    :processResources
    :classes
    :run
    Hello, world
    
    BUILD SUCCESSFUL
    
    Total time: 9.79 secs
    

    Remember to add --daemon for faster startup times for your Gradle builds.

    Have fun!

My Links

Quotes

Sample quote

Quote source

Publications