You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by el...@apache.org on 2026/08/06 12:01:14 UTC
(maven-resolver-ant-tasks) branch fix-168-global-settings-discovery created (now 1118a0a)
This is an automated email from the ASF dual-hosted git repository.
elharo pushed a change to branch fix-168-global-settings-discovery
in repository https://gitbox.apache.org/repos/asf/maven-resolver-ant-tasks.git
at 1118a0a Discover global settings from Maven home env vars and properties (#168)
This branch includes the following new commits:
new 1118a0a Discover global settings from Maven home env vars and properties (#168)
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
(maven-resolver-ant-tasks) 01/01: Discover global settings from Maven home env vars and properties (#168)
Posted by el...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
elharo pushed a commit to branch fix-168-global-settings-discovery
in repository https://gitbox.apache.org/repos/asf/maven-resolver-ant-tasks.git
commit 1118a0a3b720ad8ab9902caea13e1a622509f9d6
Author: Elliotte Rusty Harold <el...@ibiblio.org>
AuthorDate: Thu Aug 6 12:01:07 2026 +0000
Discover global settings from Maven home env vars and properties (#168)
---
.../maven/resolver/internal/ant/AetherUtils.java | 43 +++++-
.../resolver/internal/ant/AetherUtilsTest.java | 165 +++++++++++++++++++++
2 files changed, 200 insertions(+), 8 deletions(-)
diff --git a/src/main/java/org/apache/maven/resolver/internal/ant/AetherUtils.java b/src/main/java/org/apache/maven/resolver/internal/ant/AetherUtils.java
index 53c370a..4e0319c 100644
--- a/src/main/java/org/apache/maven/resolver/internal/ant/AetherUtils.java
+++ b/src/main/java/org/apache/maven/resolver/internal/ant/AetherUtils.java
@@ -19,6 +19,7 @@
package org.apache.maven.resolver.internal.ant;
import java.io.File;
+import java.util.Map;
import org.apache.maven.resolver.internal.ant.types.RemoteRepositories;
import org.apache.tools.ant.Project;
@@ -26,21 +27,47 @@ import org.apache.tools.ant.Project;
class AetherUtils {
public static File findGlobalSettings(final Project project) {
- final File file = new File(new File(project.getProperty("ant.home"), "etc"), Names.SETTINGS_XML);
- if (file.isFile()) {
- return file;
- } else {
- final String mavenHome = getMavenHome(project);
- if (mavenHome != null) {
- return new File(new File(mavenHome, "conf"), Names.SETTINGS_XML);
+ final String mavenHome = getMavenHome(project);
+ if (mavenHome != null) {
+ final File mavenSettings = new File(new File(mavenHome, "conf"), Names.SETTINGS_XML);
+ if (mavenSettings.isFile()) {
+ return mavenSettings;
}
}
+ final File antSettings = new File(new File(project.getProperty("ant.home"), "etc"), Names.SETTINGS_XML);
+ if (antSettings.isFile()) {
+ return antSettings;
+ }
+
return null;
}
public static String getMavenHome(final Project project) {
- return project.getProperty("maven.home");
+ return getMavenHome(project, System.getenv());
+ }
+
+ /**
+ * Resolves the Maven home directory from the {@code maven.home} Ant property, the {@code maven.home} system
+ * property, the {@code MAVEN_HOME} environment variable, or the {@code M2_HOME} environment variable, in that
+ * order.
+ *
+ * @param project the Ant project to read the {@code maven.home} property from
+ * @param environment the environment variables to consult
+ * @return the resolved Maven home directory, or {@code null} if none is set
+ */
+ static String getMavenHome(final Project project, final Map<String, String> environment) {
+ String mavenHome = project.getProperty("maven.home");
+ if (mavenHome == null) {
+ mavenHome = System.getProperty("maven.home");
+ }
+ if (mavenHome == null) {
+ mavenHome = environment.get("MAVEN_HOME");
+ }
+ if (mavenHome == null) {
+ mavenHome = environment.get("M2_HOME");
+ }
+ return mavenHome;
}
public static File findUserSettings(final Project project) {
diff --git a/src/test/java/org/apache/maven/resolver/internal/ant/AetherUtilsTest.java b/src/test/java/org/apache/maven/resolver/internal/ant/AetherUtilsTest.java
new file mode 100644
index 0000000..e0ad6af
--- /dev/null
+++ b/src/test/java/org/apache/maven/resolver/internal/ant/AetherUtilsTest.java
@@ -0,0 +1,165 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.resolver.internal.ant;
+
+import java.io.File;
+import java.nio.file.Files;
+import java.util.HashMap;
+import java.util.Map;
+
+import junit.framework.JUnit4TestAdapter;
+import org.apache.tools.ant.Project;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+public class AetherUtilsTest {
+ public static junit.framework.Test suite() {
+ return new JUnit4TestAdapter(AetherUtilsTest.class);
+ }
+
+ private static final String MAVEN_HOME_PROP = "maven.home";
+
+ @Rule
+ public final TemporaryFolder folder = new TemporaryFolder();
+
+ @Before
+ public void setUp() {
+ System.clearProperty(MAVEN_HOME_PROP);
+ }
+
+ @After
+ public void tearDown() {
+ System.clearProperty(MAVEN_HOME_PROP);
+ }
+
+ private Project newProject() {
+ Project project = new Project();
+ project.setProperty("user.home", System.getProperty("user.home"));
+ return project;
+ }
+
+ /**
+ * The {@code maven.home} Ant property must win over the system property and the environment variables.
+ */
+ @Test
+ public void testGetMavenHomePrefersAntProperty() {
+ Project project = newProject();
+ project.setProperty(MAVEN_HOME_PROP, "/ant/maven");
+ System.setProperty(MAVEN_HOME_PROP, "/system/maven");
+
+ assertEquals("/ant/maven", AetherUtils.getMavenHome(project));
+ }
+
+ /**
+ * The {@code maven.home} system property must be used when the Ant property is not set.
+ */
+ @Test
+ public void testGetMavenHomeFallsBackToSystemProperty() {
+ System.setProperty(MAVEN_HOME_PROP, "/system/maven");
+
+ assertEquals("/system/maven", AetherUtils.getMavenHome(newProject()));
+ }
+
+ /**
+ * The {@code MAVEN_HOME} environment variable must win over {@code M2_HOME}.
+ */
+ @Test
+ public void testGetMavenHomeFallsBackToMavenHomeEnv() {
+ Map<String, String> env = new HashMap<>();
+ env.put("MAVEN_HOME", "/env/maven");
+ env.put("M2_HOME", "/env/m2");
+
+ assertEquals("/env/maven", AetherUtils.getMavenHome(newProject(), env));
+ }
+
+ /**
+ * The {@code M2_HOME} environment variable is the last fallback.
+ */
+ @Test
+ public void testGetMavenHomeFallsBackToM2HomeEnv() {
+ Map<String, String> env = new HashMap<>();
+ env.put("M2_HOME", "/env/m2");
+
+ assertEquals("/env/m2", AetherUtils.getMavenHome(newProject(), env));
+ }
+
+ /**
+ * The Maven {@code conf} directory must take precedence over the Ant {@code etc} directory.
+ *
+ * @throws Exception in case of problems
+ */
+ @Test
+ public void testFindGlobalSettingsPrefersMavenConf() throws Exception {
+ File mavenSettings = writeSettings(folder.newFolder("maven-home"), "conf");
+ File antSettings = writeSettings(folder.newFolder("ant-home"), "etc");
+
+ Project project = newProject();
+ project.setProperty(
+ "ant.home", antSettings.getParentFile().getParentFile().getAbsolutePath());
+ project.setProperty(
+ MAVEN_HOME_PROP, mavenSettings.getParentFile().getParentFile().getAbsolutePath());
+
+ assertEquals(
+ mavenSettings.getAbsolutePath(),
+ AetherUtils.findGlobalSettings(project).getAbsolutePath());
+ }
+
+ /**
+ * The Ant {@code etc} directory must be used when no Maven home resolves to an existing settings file.
+ *
+ * @throws Exception in case of problems
+ */
+ @Test
+ public void testFindGlobalSettingsFallsBackToAntEtc() throws Exception {
+ File antSettings = writeSettings(folder.newFolder("ant-home"), "etc");
+
+ Project project = newProject();
+ project.setProperty(
+ "ant.home", antSettings.getParentFile().getParentFile().getAbsolutePath());
+
+ assertEquals(
+ antSettings.getAbsolutePath(),
+ AetherUtils.findGlobalSettings(project).getAbsolutePath());
+ }
+
+ /**
+ * No settings file must be returned when neither location holds one.
+ */
+ @Test
+ public void testFindGlobalSettingsReturnsNullWhenNotFound() {
+ Project project = newProject();
+ project.setProperty("ant.home", folder.getRoot().getAbsolutePath());
+
+ assertNull(AetherUtils.findGlobalSettings(project));
+ }
+
+ private File writeSettings(File home, String dirName) throws Exception {
+ File dir = new File(home, dirName);
+ dir.mkdirs();
+ File settings = new File(dir, Names.SETTINGS_XML);
+ Files.write(settings.toPath(), new byte[0]);
+ return settings;
+ }
+}