This is an automated email from the ASF dual-hosted git repository. kwin pushed a commit to branch bugfix/fix-reading-settings.xml in repository https://gitbox.apache.org/repos/asf/maven-verifier.git
commit 48ff41cfbc91a757c858fd6f4aa67e6733420365 Author: Konrad Windszus <k...@apache.org> AuthorDate: Thu Jun 20 08:49:09 2024 +0200 [MSHARED-1414] Interpolate expressions in settings file correctly --- pom.xml | 6 ++- .../org/apache/maven/shared/verifier/Verifier.java | 54 +++++++++++++--------- .../apache/maven/shared/verifier/VerifierTest.java | 9 ++++ src/test/resources/settings-with-expressions.xml | 22 +++++++++ 4 files changed, 69 insertions(+), 22 deletions(-) diff --git a/pom.xml b/pom.xml index 54392fe..e6d91c8 100644 --- a/pom.xml +++ b/pom.xml @@ -75,7 +75,11 @@ <groupId>org.codehaus.plexus</groupId> <artifactId>plexus-xml</artifactId> </dependency> - + <dependency> + <groupId>org.apache.maven</groupId> + <artifactId>maven-settings-builder</artifactId> + <version>3.9.8</version> + </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> diff --git a/src/main/java/org/apache/maven/shared/verifier/Verifier.java b/src/main/java/org/apache/maven/shared/verifier/Verifier.java index 362b11d..378ffd0 100644 --- a/src/main/java/org/apache/maven/shared/verifier/Verifier.java +++ b/src/main/java/org/apache/maven/shared/verifier/Verifier.java @@ -65,6 +65,12 @@ import java.util.Properties; import java.util.StringTokenizer; import java.util.regex.Pattern; +import org.apache.maven.settings.building.DefaultSettingsBuilder; +import org.apache.maven.settings.building.DefaultSettingsBuilderFactory; +import org.apache.maven.settings.building.DefaultSettingsBuildingRequest; +import org.apache.maven.settings.building.SettingsBuildingException; +import org.apache.maven.settings.building.SettingsBuildingRequest; +import org.apache.maven.settings.building.SettingsBuildingResult; import org.apache.maven.shared.utils.StringUtils; import org.apache.maven.shared.utils.io.FileUtils; import org.xml.sax.InputSource; @@ -88,6 +94,14 @@ public class Verifier { */ private static final String CLEAN_CLI_ARGUMENT = "org.apache.maven.plugins:maven-clean-plugin:clean"; + public static final String USER_HOME = System.getProperty("user.home"); + + public static final File USER_MAVEN_CONFIGURATION_HOME = new File(USER_HOME, ".m2"); + + public static final File DEFAULT_USER_SETTINGS_FILE = new File(USER_MAVEN_CONFIGURATION_HOME, "settings.xml"); + + public static final File DEFAULT_GLOBAL_SETTINGS_FILE = new File(System.getProperty("maven.conf"), "settings.xml"); + private String localRepo; private final String basedir; @@ -627,31 +641,25 @@ public class Verifier { return getArtifactMetadataPath(gid, aid, null); } - private static String retrieveLocalRepo(String settingsXmlPath) throws VerificationException { - UserModelReader userModelReader = new UserModelReader(); - - String userHome = System.getProperty("user.home"); - - File userXml; - - String repo = null; + static String retrieveLocalRepo(String settingsXmlPath) throws SettingsBuildingException { + DefaultSettingsBuilderFactory settingsBuilderFactory = new DefaultSettingsBuilderFactory(); + DefaultSettingsBuilder settingsBuilder = settingsBuilderFactory.newInstance(); + File userSettingsFile; if (settingsXmlPath != null) { - userXml = new File(settingsXmlPath); + userSettingsFile = new File(settingsXmlPath); } else { - userXml = new File(userHome, ".m2/settings.xml"); + userSettingsFile = DEFAULT_USER_SETTINGS_FILE; } - if (userXml.exists()) { - userModelReader.parse(userXml); - - String localRepository = userModelReader.getLocalRepository(); - if (localRepository != null) { - repo = new File(localRepository).getAbsolutePath(); - } - } + SettingsBuildingRequest settingsBuildingRequest = new DefaultSettingsBuildingRequest(); + settingsBuildingRequest.setGlobalSettingsFile(DEFAULT_GLOBAL_SETTINGS_FILE); + settingsBuildingRequest.setUserSettingsFile(userSettingsFile); + settingsBuildingRequest.setSystemProperties(System.getProperties()); - return repo; + // takes care of interpolation and merging + SettingsBuildingResult result = settingsBuilder.build(settingsBuildingRequest); + return result.getEffectiveSettings().getLocalRepository(); } public void deleteArtifact(String org, String name, String version, String ext) throws IOException { @@ -1228,11 +1236,15 @@ public class Verifier { } if (localRepo == null) { - localRepo = retrieveLocalRepo(settingsFile); + try { + localRepo = retrieveLocalRepo(settingsFile); + } catch (SettingsBuildingException e) { + throw new VerificationException("Cannot read settings.xml to determine local repository location", e); + } } if (localRepo == null) { - localRepo = System.getProperty("user.home") + "/.m2/repository"; + localRepo = USER_HOME + "/.m2/repository"; } File repoDir = new File(localRepo); diff --git a/src/test/java/org/apache/maven/shared/verifier/VerifierTest.java b/src/test/java/org/apache/maven/shared/verifier/VerifierTest.java index 1ce198d..9fc3bdd 100644 --- a/src/test/java/org/apache/maven/shared/verifier/VerifierTest.java +++ b/src/test/java/org/apache/maven/shared/verifier/VerifierTest.java @@ -50,6 +50,7 @@ import java.util.Properties; import java.util.stream.Stream; import org.apache.commons.io.FileUtils; +import org.apache.maven.settings.building.SettingsBuildingException; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; import org.junit.jupiter.params.ParameterizedTest; @@ -234,6 +235,14 @@ public class VerifierTest { assertThat(verifier.launcher.cliArgs, allOf(hasItemInArray("cliArg1"), hasItemInArray("cliArg2"))); } + @Test + void testInterpolationInSettingsFile() throws SettingsBuildingException { + // use settings.xml with expressions for local repo + String localRepo = Verifier.retrieveLocalRepo("src/test/resources/settings-with-expressions.xml"); + String expectedLocalRepo = System.getProperty("user.home") + "/test-repository"; + assertEquals(expectedLocalRepo, localRepo); + } + @Test public void useRealLogFile() throws Exception { FileUtils.copyDirectory(new File("src/test/resources"), new File("target/test-project")); diff --git a/src/test/resources/settings-with-expressions.xml b/src/test/resources/settings-with-expressions.xml new file mode 100644 index 0000000..fdfc742 --- /dev/null +++ b/src/test/resources/settings-with-expressions.xml @@ -0,0 +1,22 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + 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. +--> +<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"> + <localRepository>${user.home}/test-repository</localRepository> +</settings> \ No newline at end of file