Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle - Create jar only if tests pass

Tags:

java

gradle

I am new to Gradle. I would like to manipulate the following build.gradle contents to do this. Instead of separately running the tests then building the jar via separate commands, I'd like to do both in one command, except that the jar does not get created if one of the tests fail (it will not even try to build the jar).

apply plugin: 'java'
apply plugin: 'eclipse'

version = '1.0'
sourceCompatibility = 1.6
targetCompatibility = 1.6

// Create a single Jar with all dependencies
jar {
    manifest {
        attributes 'Implementation-Title': 'Gradle Jar File Example',  
            'Implementation-Version': version,
            'Main-Class': 'com.axa.openam'
    }

    baseName = project.name

    from {
        configurations.compile.collect {
            it.isDirectory() ? it : zipTree(it) 
        }
    }
}

// Get dependencies from Maven central repository
repositories {
    mavenCentral()
}

test {
    testLogging {
        showStandardStreams = true
    }
}

// Project dependencies
dependencies {
    compile 'com.google.code.gson:gson:2.5'
    testCompile 'junit:junit:4.12'
}

Thanks!

like image 369
Wes Avatar asked Jul 28 '16 15:07

Wes


People also ask

How do you exclude test cases in build Gradle?

To skip any task from the Gradle build, we can use the -x or –exclude-task option. In this case, we'll use “-x test” to skip tests from the build.

Does Gradle build also run tests?

In the Gradle project, you can create and run tests the same way you do in any other project.

How do you run a test case using Gradle command?

You can do gradle -Dtest. single=ClassUnderTestTest test if you want to test single class or use regexp like gradle -Dtest. single=ClassName*Test test you can find more examples of filtering classes for tests under this link.


1 Answers

The simplest solution is to place all the tasks you want gradle to execute in order. So you may use the following:

gradle clean test jar

Tasks Breakout

  • clean: this is used mainly just to safely remove the last outdated jar (this is not mandatory);
  • test: execute the tests;
  • jar: create the jar artifact.

Key point: if one of the task fails for some reason gradle stops its execution.

So if just a single test fails for some reason an exception is thrown and the jar file is not created at all.


Alternative solution: add 'test' as dependency of 'jar'

Just to explore some other possibilities: modify the build.gralde file as follows:

[...]
jar {
    dependsOn 'test'
    [...]
}
[...]

Now every time you run gradle jar the test task is automatically executed before.


Emulate the pure command line solution using 'dependsOn'

To emulate the first command line approach (i.e., gradle clean test jar) using the dependency method you have to further modify the build.gradle. This is because is not assured that multiple dependsOn statements are evaluated in order:

[...]
jar {
    dependsOn 'clean'
    dependsOn 'test'
    tasks.findByName('test').mustRunAfter 'clean'
    [...]
}
[...]

Now you can use:

gradle jar

and both the tasks clean and test are executed (in the right order) before the actual jar task.

like image 173
Giuseppe Ricupero Avatar answered Oct 18 '22 04:10

Giuseppe Ricupero