Skip to content

Commit 6b0f6e9

Browse files
committed
Apply Gradle Test Retry plugin
The plugin is configured to detect flaky tests and retry them 3 times when running on the CI, but still reports failures. This will provide a standard way to detect flaky tests as failures and successful attempts are shown in the tests report.
1 parent b8243e6 commit 6b0f6e9

File tree

7 files changed

+112
-12
lines changed

7 files changed

+112
-12
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ configure([rootProject] + javaProjects) { project ->
4646
apply plugin: "java"
4747
apply plugin: "java-test-fixtures"
4848
apply plugin: "checkstyle"
49-
apply plugin: 'org.springframework.build.compile'
49+
apply plugin: 'org.springframework.build.conventions'
5050
apply from: "${rootDir}/gradle/toolchains.gradle"
5151
apply from: "${rootDir}/gradle/ide.gradle"
5252

buildSrc/README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ They are declared in the `build.gradle` file in this folder.
55

66
## Build Conventions
77

8-
### Compiler conventions
8+
The `org.springframework.build.conventions` plugin applies all conventions to the Framework build:
9+
10+
* Configuring the Java compiler, see `CompilerConventions`
11+
* Configuring testing in the build with `TestConventions`
912

10-
The `org.springframework.build.compile` plugin applies the Java compiler conventions to the build.
1113

1214
## Build Plugins
1315

buildSrc/build.gradle

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ repositories {
99

1010
dependencies {
1111
implementation "me.champeau.gradle:japicmp-gradle-plugin:0.3.0"
12+
implementation "org.gradle:test-retry-gradle-plugin:1.4.1"
1213
}
1314

1415
gradlePlugin {
@@ -17,9 +18,9 @@ gradlePlugin {
1718
id = "org.springframework.build.api-diff"
1819
implementationClass = "org.springframework.build.api.ApiDiffPlugin"
1920
}
20-
compileConventionsPlugin {
21-
id = "org.springframework.build.compile"
22-
implementationClass = "org.springframework.build.compile.CompilerConventionsPlugin"
21+
conventionsPlugin {
22+
id = "org.springframework.build.conventions"
23+
implementationClass = "org.springframework.build.ConventionsPlugin"
2324
}
2425
optionalDependenciesPlugin {
2526
id = "org.springframework.build.optional-dependencies"
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -14,14 +14,15 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.build.compile;
17+
package org.springframework.build;
1818

1919
import java.util.ArrayList;
2020
import java.util.Arrays;
2121
import java.util.List;
2222

2323
import org.gradle.api.Plugin;
2424
import org.gradle.api.Project;
25+
import org.gradle.api.plugins.JavaBasePlugin;
2526
import org.gradle.api.plugins.JavaLibraryPlugin;
2627
import org.gradle.api.plugins.JavaPlugin;
2728
import org.gradle.api.tasks.compile.JavaCompile;
@@ -33,7 +34,7 @@
3334
* @author Sam Brannen
3435
* @author Sebastien Deleuze
3536
*/
36-
public class CompilerConventionsPlugin implements Plugin<Project> {
37+
public class CompilerConventions {
3738

3839
private static final List<String> COMPILER_ARGS;
3940

@@ -58,9 +59,8 @@ public class CompilerConventionsPlugin implements Plugin<Project> {
5859
"-Xlint:-deprecation", "-Xlint:-unchecked"));
5960
}
6061

61-
@Override
6262
public void apply(Project project) {
63-
project.getPlugins().withType(JavaLibraryPlugin.class, javaPlugin -> applyJavaCompileConventions(project));
63+
project.getPlugins().withType(JavaBasePlugin.class, javaPlugin -> applyJavaCompileConventions(project));
6464
}
6565

6666
/**
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2002-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.build;
18+
19+
import org.gradle.api.Plugin;
20+
import org.gradle.api.Project;
21+
import org.gradle.api.plugins.JavaBasePlugin;
22+
23+
/**
24+
* Plugin to apply conventions to projects that are part of Spring Framework's build.
25+
* Conventions are applied in response to various plugins being applied.
26+
*
27+
* When the {@link JavaBasePlugin} is applied, the conventions in {@link TestConventions}
28+
* are applied.
29+
* When the {@link JavaBasePlugin} is applied, the conventions in {@link CompilerConventions}
30+
* are applied.
31+
*
32+
* @author Brian Clozel
33+
*/
34+
public class ConventionsPlugin implements Plugin<Project> {
35+
36+
@Override
37+
public void apply(Project project) {
38+
new TestConventions().apply(project);
39+
new CompilerConventions().apply(project);
40+
}
41+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2002-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.build;
18+
19+
import org.gradle.api.Project;
20+
import org.gradle.api.plugins.JavaBasePlugin;
21+
import org.gradle.api.tasks.testing.Test;
22+
import org.gradle.testretry.TestRetryPlugin;
23+
import org.gradle.testretry.TestRetryTaskExtension;
24+
25+
/**
26+
* Conventions that are applied in the presence of the {@link JavaBasePlugin}. When the
27+
* plugin is applied:
28+
* <ul>
29+
* <li>The {@link TestRetryPlugin Test Retry} plugins is applied so that flaky tests
30+
* are retried 3 times when running on the CI.
31+
* </ul>
32+
*
33+
* @author Brian Clozel
34+
* @author Andy Wilkinson
35+
*/
36+
class TestConventions {
37+
38+
void apply(Project project) {
39+
project.getPlugins().withType(JavaBasePlugin.class, (java) -> configureTestConventions(project));
40+
}
41+
42+
private void configureTestConventions(Project project) {
43+
project.getPlugins().apply(TestRetryPlugin.class);
44+
project.getTasks().withType(Test.class,
45+
(test) -> project.getPlugins().withType(TestRetryPlugin.class, (testRetryPlugin) -> {
46+
TestRetryTaskExtension testRetry = test.getExtensions().getByType(TestRetryTaskExtension.class);
47+
testRetry.getFailOnPassedAfterRetry().set(true);
48+
testRetry.getMaxRetries().set(isCi() ? 3 : 0);
49+
}));
50+
}
51+
52+
private boolean isCi() {
53+
return Boolean.parseBoolean(System.getenv("CI"));
54+
}
55+
56+
}

gradle/spring-module.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
apply plugin: 'java-library'
2-
apply plugin: 'org.springframework.build.compile'
2+
apply plugin: 'org.springframework.build.conventions'
33
apply plugin: 'org.springframework.build.optional-dependencies'
44
// Uncomment the following for Shadow support in the jmhJar block.
55
// Currently commented out due to ZipException: archive is not a ZIP archive

0 commit comments

Comments
 (0)