Skip to content

Commit 575268d

Browse files
author
Vincent Potucek
committed
[rewrite] Add GradleBestPractices
Signed-off-by: Vincent Potucek <[email protected]>
1 parent 6b5c08a commit 575268d

File tree

10 files changed

+142
-16
lines changed

10 files changed

+142
-16
lines changed

.github/workflows/precommit.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
java-version: ${{ matrix.java }}
2222
distribution: temurin
2323
cache: gradle
24-
- name: Run Gradle (precommit)
24+
- name: Run Gradle (pre-commit)
2525
continue-on-error: ${{ matrix.experimental }}
2626
shell: bash
2727
run: |

.github/workflows/sanity-check.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: Gradle SanityCheck
2+
on: [pull_request]
3+
4+
jobs:
5+
precommit:
6+
if: github.repository == 'opensearch-project/OpenSearch'
7+
runs-on: ${{ matrix.os }}
8+
strategy:
9+
matrix:
10+
java: [ 21, 25 ]
11+
os: [ubuntu-latest, windows-latest, macos-latest, macos-13, ubuntu-24.04-arm]
12+
include:
13+
- java: 21
14+
os: 'windows-2025'
15+
experimental: true
16+
steps:
17+
- uses: actions/checkout@v5
18+
- name: Set up JDK ${{ matrix.java }}
19+
uses: actions/setup-java@v5
20+
with:
21+
java-version: ${{ matrix.java }}
22+
distribution: temurin
23+
cache: gradle
24+
- name: Run Gradle (sanity-check)
25+
continue-on-error: ${{ matrix.experimental }}
26+
shell: bash
27+
run: ./gradlew rewriteDryRun -Dorg.gradle.jvmargs=-Xmx8G
28+
timeout-minutes: 90

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
2020
- Add build-tooling to run in FIPS environment ([#18921](https://github.com/opensearch-project/OpenSearch/pull/18921))
2121
- Add SMILE/CBOR/YAML document format support to Bulk GRPC endpoint ([#19744](https://github.com/opensearch-project/OpenSearch/pull/19744))
2222
- Implement GRPC ConstantScoreQuery, FuzzyQuery, MatchBoolPrefixQuery, MatchPhrasePrefix, PrefixQuery, MatchQuery ([#19854](https://github.com/opensearch-project/OpenSearch/pull/19854))
23+
- [rewrite] Add `GradleBestPractices` ([#19885](https://github.com/opensearch-project/OpenSearch/pull/19885))
2324

2425
### Changed
2526
- Faster `terms` query creation for `keyword` field with index and docValues enabled ([#19350](https://github.com/opensearch-project/OpenSearch/pull/19350))

build.gradle

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,24 +52,27 @@ import groovy.xml.XmlParser;
5252
import static org.opensearch.gradle.util.GradleUtils.maybeConfigure
5353

5454
plugins {
55+
id 'com.diffplug.spotless' version '6.25.0' apply false
56+
id 'jacoco-report-aggregation'
5557
id 'lifecycle-base'
58+
id 'net.ltgt.errorprone' version '4.3.0' apply false
5659
id 'opensearch.docker-support'
5760
id 'opensearch.global-build-info'
58-
id "com.diffplug.spotless" version "6.25.0" apply false
59-
id "org.gradle.test-retry" version "1.6.2" apply false
60-
id "test-report-aggregation"
61-
id 'jacoco-report-aggregation'
61+
id 'org.gradle.test-retry' version '1.6.2' apply false
62+
id 'org.openrewrite.rewrite' version '7.19.0' apply false
63+
id 'test-report-aggregation'
6264
}
6365

6466
apply from: 'gradle/build-complete.gradle'
65-
apply from: 'gradle/runtime-jdk-provision.gradle'
66-
apply from: 'gradle/ide.gradle'
67+
apply from: 'gradle/code-coverage.gradle'
6768
apply from: 'gradle/forbidden-dependencies.gradle'
6869
apply from: 'gradle/formatting.gradle'
70+
apply from: 'gradle/ide.gradle'
6971
apply from: 'gradle/local-distribution.gradle'
70-
apply from: 'gradle/run.gradle'
7172
apply from: 'gradle/missing-javadoc.gradle'
72-
apply from: 'gradle/code-coverage.gradle'
73+
apply from: 'gradle/rewrite.gradle'
74+
apply from: 'gradle/run.gradle'
75+
apply from: 'gradle/runtime-jdk-provision.gradle'
7376

7477
// Apply FIPS configuration to all projects
7578
allprojects {

gradle/rewrite.gradle

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
9+
project.apply plugin: 'org.openrewrite.rewrite'
10+
11+
rewrite {
12+
activeRecipe('org.opensearch.openrewrite.SanityCheck')
13+
exclusion('**ActionListener.java')
14+
exclusion('**SearchAfterIT.java')
15+
exclusion('**StarTreeMapper.java')
16+
exclusion('**package-info.java')
17+
setExportDatatables(true)
18+
setFailOnDryRunResults(true)
19+
}
20+
21+
dependencies {
22+
rewrite(platform('org.openrewrite.recipe:rewrite-recipe-bom:3.17.0'))
23+
rewrite('org.openrewrite.recipe:rewrite-migrate-java:3.20.0')
24+
rewrite('org.openrewrite.recipe:rewrite-java-security:3.19.2')
25+
rewrite('org.openrewrite.recipe:rewrite-rewrite:0.14.1')
26+
rewrite('org.openrewrite.recipe:rewrite-static-analysis:2.20.0')
27+
rewrite('org.openrewrite.recipe:rewrite-third-party:0.30.0')
28+
}

gradle/wrapper/gradle-wrapper.jar

1.83 KB
Binary file not shown.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-9.2.0-all.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-9.2.0-bin.zip
44
networkTimeout=10000
55
validateDistributionUrl=true
66
zipStoreBase=GRADLE_USER_HOME
77
zipStorePath=wrapper/dists
8-
distributionSha256Sum=16f2b95838c1ddcf7242b1c39e7bbbb43c842f1f1a1a0dc4959b6d4d68abcac3
8+
distributionSha256Sum=df67a32e86e3276d011735facb1535f64d0d88df84fa87521e90becc2d735444

gradlew

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/sh
22

33
#
4-
# Copyright © 2015-2021 the original authors.
4+
# Copyright © 2015 the original authors.
55
#
66
# Licensed under the Apache License, Version 2.0 (the "License");
77
# you may not use this file except in compliance with the License.
@@ -202,7 +202,7 @@ fi
202202

203203

204204
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
205-
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
205+
DEFAULT_JVM_OPTS=
206206

207207
# Collect all arguments for the java command:
208208
# * DEFAULT_JVM_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,

gradlew.bat

100644100755
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ set APP_HOME=%DIRNAME%
3636
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
3737

3838
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
39-
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
39+
set DEFAULT_JVM_OPTS=
4040

4141
@rem Find java.exe
4242
if defined JAVA_HOME goto findJavaFromJavaHome
@@ -70,11 +70,10 @@ goto fail
7070
:execute
7171
@rem Setup the command line
7272

73-
set CLASSPATH=
7473

7574

7675
@rem Execute Gradle
77-
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" -jar "%APP_HOME%\gradle\wrapper\gradle-wrapper.jar" %*
76+
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -jar "%APP_HOME%\gradle\wrapper\gradle-wrapper.jar" %*
7877

7978
:end
8079
@rem End local scope for the variables with windows NT shell

rewrite.yml

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
type: specs.openrewrite.org/v1beta/recipe
2+
name: org.opensearch.openrewrite.SanityCheck
3+
displayName: Apply all Java & Gradle best practices
4+
description: Comprehensive code quality recipe combining modernization, security, and best practices.
5+
tags:
6+
- java
7+
- gradle
8+
- static-analysis
9+
- cleanup
10+
recipeList:
11+
- org.openrewrite.gradle.EnableGradleBuildCache
12+
- org.openrewrite.gradle.EnableGradleParallelExecution
13+
- org.openrewrite.gradle.GradleBestPractices
14+
- org.openrewrite.staticanalysis.RemoveUnusedPrivateMethods
15+
# - org.openrewrite.java.RemoveUnusedImports
16+
# - org.openrewrite.java.format.NormalizeFormat
17+
# - org.openrewrite.java.format.NormalizeLineBreaks
18+
# - org.openrewrite.java.format.RemoveTrailingWhitespace
19+
# - org.openrewrite.java.migrate.UpgradeToJava17
20+
# - org.openrewrite.java.migrate.lang.FindVirtualThreadOpportunities # don't want to use: https://github.com/diffplug/spotless/pull/2684#discussion_r2433831887
21+
# - org.openrewrite.java.migrate.lang.StringRulesRecipes
22+
# - org.openrewrite.java.migrate.util.JavaLangAPIs
23+
# - org.openrewrite.java.migrate.util.JavaUtilAPIs
24+
# - org.openrewrite.java.migrate.util.MigrateInflaterDeflaterToClose
25+
# - org.openrewrite.java.migrate.util.ReplaceStreamCollectWithToList
26+
# - org.openrewrite.java.migrate.util.SequencedCollection
27+
# - org.openrewrite.java.recipes.JavaRecipeBestPractices
28+
# - org.openrewrite.java.recipes.RecipeTestingBestPractices
29+
# - org.openrewrite.java.security.JavaSecurityBestPractices
30+
# - org.openrewrite.staticanalysis.BufferedWriterCreationRecipes
31+
# - org.openrewrite.staticanalysis.CommonStaticAnalysis
32+
# - org.openrewrite.staticanalysis.EqualsAvoidsNull
33+
# - org.openrewrite.staticanalysis.JavaApiBestPractices
34+
# - org.openrewrite.staticanalysis.LowercasePackage
35+
# - org.openrewrite.staticanalysis.MissingOverrideAnnotation
36+
# - org.openrewrite.staticanalysis.ModifierOrder
37+
# - org.openrewrite.staticanalysis.NoFinalizer
38+
# - org.openrewrite.staticanalysis.NoToStringOnStringType
39+
# - org.openrewrite.staticanalysis.NoValueOfOnStringType
40+
# - org.openrewrite.staticanalysis.RemoveUnusedLocalVariables
41+
# - org.openrewrite.staticanalysis.RemoveUnusedPrivateFields
42+
# - org.openrewrite.staticanalysis.ReplaceApacheCommonsLang3ValidateNotNullWithObjectsRequireNonNull
43+
# - org.openrewrite.staticanalysis.SimplifyTernaryRecipes
44+
# - org.openrewrite.staticanalysis.URLEqualsHashCodeRecipes
45+
# - org.openrewrite.staticanalysis.UnnecessaryCloseInTryWithResources
46+
# - org.openrewrite.staticanalysis.UnnecessaryExplicitTypeArguments
47+
# - org.openrewrite.staticanalysis.UnnecessaryParentheses
48+
# - org.openrewrite.staticanalysis.UnnecessaryReturnAsLastStatement
49+
# - tech.picnic.errorprone.refasterrules.BigDecimalRulesRecipes
50+
# - tech.picnic.errorprone.refasterrules.CharSequenceRulesRecipes
51+
# - tech.picnic.errorprone.refasterrules.ClassRulesRecipes
52+
# - tech.picnic.errorprone.refasterrules.CollectionRulesRecipes
53+
# - tech.picnic.errorprone.refasterrules.ComparatorRulesRecipes
54+
# - tech.picnic.errorprone.refasterrules.EqualityRulesRecipes
55+
# - tech.picnic.errorprone.refasterrules.FileRulesRecipes
56+
# - tech.picnic.errorprone.refasterrules.MapRulesRecipes
57+
# - tech.picnic.errorprone.refasterrules.MicrometerRulesRecipes
58+
# - tech.picnic.errorprone.refasterrules.MockitoRulesRecipes
59+
# - tech.picnic.errorprone.refasterrules.NullRulesRecipes
60+
# - tech.picnic.errorprone.refasterrules.OptionalRulesRecipes
61+
# - tech.picnic.errorprone.refasterrules.PatternRulesRecipes
62+
# - tech.picnic.errorprone.refasterrules.PreconditionsRulesRecipes
63+
# - tech.picnic.errorprone.refasterrules.PrimitiveRulesRecipes
64+
# - tech.picnic.errorprone.refasterrules.StreamRulesRecipes
65+
# - tech.picnic.errorprone.refasterrules.StringRulesRecipes
66+
# - tech.picnic.errorprone.refasterrules.TimeRulesRecipes
67+
---

0 commit comments

Comments
 (0)