You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ss...@apache.org on 2024/12/13 15:18:21 UTC
(sling-org-apache-sling-feature) branch master updated: SLING-12566 Respect localRepository configuration in ~/.m2/settings.xml before defaulting to ~/.m2/repository
This is an automated email from the ASF dual-hosted git repository.
sseifert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-feature.git
The following commit(s) were added to refs/heads/master by this push:
new 9c55fa2 SLING-12566 Respect localRepository configuration in ~/.m2/settings.xml before defaulting to ~/.m2/repository
9c55fa2 is described below
commit 9c55fa2efcb0676eafe09898f4382c83844da4ce
Author: Stefan Seifert <st...@users.noreply.github.com>
AuthorDate: Fri Dec 13 16:18:12 2024 +0100
SLING-12566 Respect localRepository configuration in ~/.m2/settings.xml before defaulting to ~/.m2/repository
---
.../io/artifacts/ArtifactManagerConfig.java | 45 +++++++++++++++++++++-
1 file changed, 43 insertions(+), 2 deletions(-)
diff --git a/src/main/java/org/apache/sling/feature/io/artifacts/ArtifactManagerConfig.java b/src/main/java/org/apache/sling/feature/io/artifacts/ArtifactManagerConfig.java
index 5dde69d..568e729 100644
--- a/src/main/java/org/apache/sling/feature/io/artifacts/ArtifactManagerConfig.java
+++ b/src/main/java/org/apache/sling/feature/io/artifacts/ArtifactManagerConfig.java
@@ -18,12 +18,23 @@
*/
package org.apache.sling.feature.io.artifacts;
+import javax.xml.XMLConstants;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+
import java.io.File;
+import java.io.IOException;
import java.util.concurrent.atomic.AtomicLong;
import org.apache.sling.feature.io.artifacts.spi.ArtifactProviderContext;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.w3c.dom.Document;
+import org.w3c.dom.NodeList;
+import org.xml.sax.SAXException;
/**
* This class holds the configuration of artifact manager.
@@ -54,17 +65,20 @@ public class ArtifactManagerConfig implements ArtifactProviderContext {
*/
private final @NotNull String repoHome;
+ private static final Logger logger = LoggerFactory.getLogger(ArtifactManagerConfig.class);
+
/**
* Create a new configuration object. Set the default values
*/
public ArtifactManagerConfig() {
// set defaults
+ String mvnRepositoryDirectory = getMvnRepositoryDirectory();
this.repositoryUrls = new String[] {
- "file://" + new File(System.getProperty("user.home")).toURI().getPath() + ".m2/repository",
+ "file://" + new File(mvnRepositoryDirectory).toURI().getPath(),
"https://repo.maven.apache.org/maven2",
"https://repository.apache.org/content/groups/snapshots"
};
- this.repoHome = System.getProperty("user.home") + "/.m2/repository/";
+ this.repoHome = mvnRepositoryDirectory + "/";
}
/**
@@ -179,4 +193,31 @@ public class ArtifactManagerConfig implements ArtifactProviderContext {
String getMvnHome() {
return this.repoHome;
}
+
+ /**
+ * Get the .m2 directory for the current user.
+ * If a ~/.m2/settings.xml exists, it is checked for a localRepository configuration.
+ * Otherwise ~/.m2/repository is used as default value.
+ * @return Maven repository directory
+ */
+ private static final String getMvnRepositoryDirectory() {
+ String mavenDirectory = System.getProperty("user.home") + "/.m2";
+ File settings = new File(mavenDirectory, "settings.xml");
+ if (settings.exists()) {
+ try {
+ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+ factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
+ DocumentBuilder builder = factory.newDocumentBuilder();
+ Document doc = builder.parse(settings);
+ NodeList nodes = doc.getElementsByTagName("localRepository");
+ if (nodes.getLength() > 0) {
+ String localRepository = nodes.item(0).getTextContent();
+ return new File(localRepository).getAbsolutePath();
+ }
+ } catch (ParserConfigurationException | SAXException | IOException ex) {
+ logger.warn("Unable to parse " + settings.getAbsolutePath(), ex);
+ }
+ }
+ return mavenDirectory + "/repository";
+ }
}