Skip to content

Commit c8f0bd8

Browse files
Read settings.xml from disk if no maven settings are defined for testing framework (#5873)
* Add settings.xml file to the rewrite test run * Polish * Polish * Use settings.xml if no settings is provided for MavenPomDownloader * Polish * Polish * Polish * Polish * Polish * Polish * Inline `hasNoDefinedConfiguration` * Explain what we're checking and why we're loading from disk * Tone down defaulting to pomXml rewrite-tests only * Put `readFromDiskEnabled` back * Remove `mavenSettings` from Assertions Gradle file * Polish --------- Co-authored-by: Tim te Beek <[email protected]>
1 parent 449650d commit c8f0bd8

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

rewrite-maven/src/main/java/org/openrewrite/maven/Assertions.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,21 @@
3030
import java.util.function.Consumer;
3131

3232
import static org.junit.jupiter.api.Assertions.fail;
33+
import static org.openrewrite.maven.tree.MavenRepository.MAVEN_LOCAL_DEFAULT;
3334

3435
public class Assertions {
3536
private Assertions() {
3637
}
3738

3839
static void customizeExecutionContext(ExecutionContext ctx) {
39-
if (MavenSettings.readFromDiskEnabled()) {
40-
MavenExecutionContextView mctx = MavenExecutionContextView.view(ctx);
40+
MavenExecutionContextView mctx = MavenExecutionContextView.view(ctx);
41+
boolean nothingConfigured = mctx.getSettings() == null &&
42+
mctx.getLocalRepository().equals(MAVEN_LOCAL_DEFAULT) &&
43+
mctx.getRepositories().isEmpty() &&
44+
mctx.getActiveProfiles().isEmpty() &&
45+
mctx.getMirrors().isEmpty();
46+
if (nothingConfigured) {
47+
// Load Maven settings to pick up any mirrors, proxies etc. that might be required in corporate environments
4148
mctx.setMavenSettings(MavenSettings.readMavenSettingsFromDisk(mctx));
4249
}
4350
}

rewrite-maven/src/main/java/org/openrewrite/maven/MavenExecutionContextView.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,23 @@ public List<String> getActiveProfiles() {
218218
return getMessage(MAVEN_ACTIVE_PROFILES, emptyList());
219219
}
220220

221+
/**
222+
* Replaces the current Maven execution settings with the provided {@link MavenSettings}.
223+
* <p>
224+
* This method does <strong>not</strong> merge with or preserve any existing configuration.
225+
* All previously configured repositories, mirrors, credentials, active profiles,
226+
* and local repository settings will be discarded and replaced entirely with those
227+
* derived from the given {@code settings}.
228+
* </p>
229+
*
230+
* <p>If {@code settings} is {@code null}, this call is a no-op and the current
231+
* configuration remains unchanged.</p>
232+
*
233+
* @param settings the Maven settings to apply; if {@code null}, no changes are made
234+
* @param activeProfiles additional active profiles to consider; these are resolved
235+
* against the provided settings to determine the effective profiles
236+
* @return this execution context view with the updated Maven settings applied
237+
*/
221238
public MavenExecutionContextView setMavenSettings(@Nullable MavenSettings settings, String... activeProfiles) {
222239
if (settings == null) {
223240
return this;

rewrite-maven/src/main/java/org/openrewrite/maven/MavenSettings.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,12 @@ void maybeDecryptPasswords(ExecutionContext ctx) {
150150
}
151151
}
152152

153-
153+
/**
154+
* @deprecated The concept of property `org.openrewrite.test.readMavenSettingsFromDisk` is no longer in use.
155+
*
156+
* @return True property `org.openrewrite.test.readMavenSettingsFromDisk` is true, false otherwise.
157+
*/
158+
@Deprecated
154159
public static boolean readFromDiskEnabled() {
155160
final String propertyValue = System.getProperty("org.openrewrite.test.readMavenSettingsFromDisk");
156161
return propertyValue != null && !"false".equalsIgnoreCase(propertyValue);

0 commit comments

Comments
 (0)