66import java .io .FileWriter ;
77import java .io .IOException ;
88import java .lang .management .ManagementFactory ;
9- import java .nio .file .Paths ;
109import java .util .ArrayList ;
1110import java .util .Arrays ;
1211import java .util .HashMap ;
@@ -22,12 +21,39 @@ public class QuarkusGradleWrapperTestBase extends QuarkusGradleTestBase {
2221 private static final String GRADLE_WRAPPER_WINDOWS = "gradlew.bat" ;
2322 private static final String GRADLE_WRAPPER_UNIX = "./gradlew" ;
2423 private static final String MAVEN_REPO_LOCAL = "maven.repo.local" ;
24+ private static final String QUARKUS_TEST_GRADLE_WRAPPER_VERSION = "quarkus-test-gradle-wrapper-version" ;
2525
2626 private Map <String , String > systemProps ;
2727
2828 private boolean configurationCacheEnable = true ;
2929 private boolean noWatchFs = true ;
3030
31+ @ Override
32+ protected File getProjectDir (String projectName ) {
33+ return getProjectDir (projectName , getTestProjectNameSuffix ());
34+ }
35+
36+ /**
37+ * Returns a suffix for a test project directory name based on the requested Gradle wrapper version.
38+ * If a Gradle wrapper version was not configured, the suffix will be null.
39+ * Otherwise, it will be {@code -wrapper-${quarkus-test-gradle-wrapper-version}}.
40+ *
41+ * @return test project directory suffix or null
42+ */
43+ private static String getTestProjectNameSuffix () {
44+ var wrapperVersion = getRequestedWrapperVersion ();
45+ return wrapperVersion == null ? null : "-wrapper-" + wrapperVersion ;
46+ }
47+
48+ /**
49+ * Returns configured Gradle wrapper version for a test or null, in case it wasn't configured.
50+ *
51+ * @return configured Gradle wrapper version or null
52+ */
53+ private static String getRequestedWrapperVersion () {
54+ return System .getProperty (QUARKUS_TEST_GRADLE_WRAPPER_VERSION );
55+ }
56+
3157 protected void setupTestCommand () {
3258
3359 }
@@ -61,9 +87,12 @@ public BuildResult runGradleWrapper(boolean expectError, File projectDir, boolea
6187 throws IOException , InterruptedException {
6288 boolean isInCiPipeline = "true" .equals (System .getenv ("CI" ));
6389
90+ // install a custom version of the wrapper, in case it's configured
91+ installRequestedWrapper (projectDir );
92+
6493 setupTestCommand ();
6594 List <String > command = new ArrayList <>();
66- command .add (getGradleWrapperCommand ());
95+ command .add (getGradleWrapperCommand (projectDir ));
6796 addSystemProperties (command );
6897
6998 if (!isInCiPipeline && isDebuggerConnected ()) {
@@ -87,7 +116,7 @@ public BuildResult runGradleWrapper(boolean expectError, File projectDir, boolea
87116
88117 File logOutput = new File (projectDir , "command-output.log" );
89118
90- System . out . println ( "$ " + String . join ( " " , command ) );
119+ logCommandLine ( command );
91120 ProcessBuilder pb = new ProcessBuilder ()
92121 .directory (projectDir )
93122 .command (command )
@@ -142,15 +171,50 @@ public BuildResult runGradleWrapper(boolean expectError, File projectDir, boolea
142171 return commandResult ;
143172 }
144173
174+ private static void logCommandLine (List <String > command ) {
175+ System .out .println ("$ " + String .join (" " , command ));
176+ }
177+
178+ private void installRequestedWrapper (File projectDir ) {
179+ String wrapperVersion = getRequestedWrapperVersion ();
180+ if (wrapperVersion == null ) {
181+ // no specific version was configured, the integration-test/gradle one will be used
182+ return ;
183+ }
184+ final String defaultWrapper = getGradleWrapperCommand (projectDir );
185+
186+ final List <String > command = List .of (defaultWrapper , "wrapper" , "--gradle-version=" + wrapperVersion );
187+ logCommandLine (command );
188+
189+ final ProcessBuilder pb = new ProcessBuilder ()
190+ .directory (projectDir )
191+ .command (command )
192+ .redirectInput (ProcessBuilder .Redirect .INHERIT )
193+ // Should prevent "fragmented" output (parts of stdout and stderr interleaved)
194+ .redirectErrorStream (true );
195+ try {
196+ pb .start ().waitFor ();
197+ } catch (Exception e ) {
198+ throw new RuntimeException ("Failed to install Gradle wrapper" , e );
199+ }
200+ }
201+
145202 protected void setSystemProperty (String name , String value ) {
146203 if (systemProps == null ) {
147204 systemProps = new HashMap <>();
148205 }
149206 systemProps .put (name , value );
150207 }
151208
152- private String getGradleWrapperCommand () {
153- return Paths .get (getGradleWrapperName ()).toAbsolutePath ().toString ();
209+ private String getGradleWrapperCommand (File projectDir ) {
210+ File wrapper = null ;
211+ if (projectDir != null ) {
212+ wrapper = new File (projectDir , getGradleWrapperName ());
213+ }
214+ if (wrapper == null || !wrapper .exists ()) {
215+ wrapper = new File (getGradleWrapperName ());
216+ }
217+ return wrapper .getAbsolutePath ();
154218 }
155219
156220 private String getGradleWrapperName () {
0 commit comments