From 931652fcc04c5dcb031bd5c6900c58326fe98160 Mon Sep 17 00:00:00 2001
From: Marek Goldmann <goldmann@fedoraproject.org>
Date: Mon, 30 Jul 2012 13:12:34 +0200
Subject: [PATCH 10/25] Maven 3 support

---
 .../DefaultLocalMavenRepositoryLocator.java        | 69 ++++------------------
 .../maven/internal/ant/LoggingHelper.java          |  4 +-
 2 files changed, 13 insertions(+), 60 deletions(-)

diff --git a/subprojects/core-impl/src/main/groovy/org/gradle/api/internal/artifacts/mvnsettings/DefaultLocalMavenRepositoryLocator.java b/subprojects/core-impl/src/main/groovy/org/gradle/api/internal/artifacts/mvnsettings/DefaultLocalMavenRepositoryLocator.java
index 6c57b88..f0d2831 100644
--- a/subprojects/core-impl/src/main/groovy/org/gradle/api/internal/artifacts/mvnsettings/DefaultLocalMavenRepositoryLocator.java
+++ b/subprojects/core-impl/src/main/groovy/org/gradle/api/internal/artifacts/mvnsettings/DefaultLocalMavenRepositoryLocator.java
@@ -15,79 +15,32 @@
  */
 package org.gradle.api.internal.artifacts.mvnsettings;
 
-import org.apache.maven.settings.DefaultMavenSettingsBuilder;
-import org.apache.maven.settings.MavenSettingsBuilder;
-import org.apache.maven.settings.Settings;
-import org.gradle.api.internal.artifacts.PlexusLoggerAdapter;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
 import java.io.File;
-import java.lang.reflect.Field;
 import java.util.Map;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
 
 /**
  * @author Steve Ebersole
  */
 public class DefaultLocalMavenRepositoryLocator implements LocalMavenRepositoryLocator {
-    private static final Logger LOGGER = LoggerFactory.getLogger(DefaultLocalMavenRepositoryLocator.class);
-    private static final Pattern PLACEHOLDER_PATTERN = Pattern.compile("\\$\\{([^\\}]*)\\}");
-
-    private final MavenFileLocations mavenFileLocations;
-    private final Map<String, String> systemProperties;
-    private final Map<String, String> environmentVariables;
+    private static final String SETTINGS_LOCATION_OVERRIDE = "maven.settings";
 
     public DefaultLocalMavenRepositoryLocator(MavenFileLocations mavenFileLocations, Map<String, String> systemProperties, Map<String, String> environmentVariables) {
-        this.mavenFileLocations = mavenFileLocations;
-        this.systemProperties = systemProperties;
-        this.environmentVariables = environmentVariables;
     }
 
     public File getLocalMavenRepository() {
-        Settings settings = buildSettings();
-        String repoPath = settings.getLocalRepository().trim();
-        return new File(resolvePlaceholders(repoPath));
+        return determineSettingsFileLocation();
     }
 
-    private String resolvePlaceholders(String value) {
-        StringBuffer result = new StringBuffer();
-        Matcher matcher = PLACEHOLDER_PATTERN.matcher(value);
-
-        while (matcher.find()) {
-            String placeholder = matcher.group(1);
-            String replacement = placeholder.startsWith("env.") ? environmentVariables.get(placeholder.substring(4)) : systemProperties.get(placeholder);
-            if (replacement == null) {
-                throw new CannotLocateLocalMavenRepositoryException(String.format("Cannot resolve placeholder '%s' in value '%s'", placeholder, value));
-            }
-            matcher.appendReplacement(result, Matcher.quoteReplacement(replacement));
-        }
-        matcher.appendTail(result);
-
-        return result.toString();
+    private static String normalizePath(String path) {
+        if ( path.startsWith( "~" ) ) {
+            path = System.getProperty( "user.home" ) + path.substring( 1 );
+         }
+        return path;
     }
 
-    private Settings buildSettings() {
-        try {
-            return createSettingsBuilder().buildSettings();
-        } catch (Exception e) {
-            throw new CannotLocateLocalMavenRepositoryException(e);
-        }
-    }
-
-    private MavenSettingsBuilder createSettingsBuilder() throws Exception {
-        DefaultMavenSettingsBuilder builder = new DefaultMavenSettingsBuilder();
-        builder.enableLogging(new PlexusLoggerAdapter(LOGGER));
-
-        Field userSettingsFileField = DefaultMavenSettingsBuilder.class.getDeclaredField("userSettingsFile");
-        userSettingsFileField.setAccessible(true);
-        userSettingsFileField.set(builder, mavenFileLocations.getUserSettingsFile());
-
-        Field globalSettingsFileField = DefaultMavenSettingsBuilder.class.getDeclaredField("globalSettingsFile");
-        globalSettingsFileField.setAccessible(true);
-        globalSettingsFileField.set(builder, mavenFileLocations.getGlobalSettingsFile());
-
-        return builder;
+    private File determineSettingsFileLocation() {
+        final String defaultLocation = "~/.m2/settings.xml";
+        final String location = System.getProperty( SETTINGS_LOCATION_OVERRIDE, defaultLocation );
+        return new File( normalizePath( location ) );
     }
 }
diff --git a/subprojects/maven/src/main/groovy/org/gradle/api/publication/maven/internal/ant/LoggingHelper.java b/subprojects/maven/src/main/groovy/org/gradle/api/publication/maven/internal/ant/LoggingHelper.java
index 759a5b0..fe83ab6 100644
--- a/subprojects/maven/src/main/groovy/org/gradle/api/publication/maven/internal/ant/LoggingHelper.java
+++ b/subprojects/maven/src/main/groovy/org/gradle/api/publication/maven/internal/ant/LoggingHelper.java
@@ -17,7 +17,7 @@ package org.gradle.api.publication.maven.internal.ant;
 
 import org.apache.maven.artifact.ant.AntDownloadMonitor;
 import org.apache.maven.artifact.manager.DefaultWagonManager;
-import org.apache.maven.artifact.manager.WagonManager;
+import org.apache.maven.artifact.handler.ArtifactHandler;
 import org.apache.tools.ant.Project;
 import org.codehaus.plexus.PlexusContainer;
 import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
@@ -30,7 +30,7 @@ import java.lang.reflect.Field;
 public class LoggingHelper {
     public static void injectLogger(PlexusContainer container, Project project) {
         try {
-            WagonManager wagonManager = (WagonManager) container.lookup(WagonManager.ROLE);
+            ArtifactHandler wagonManager = (ArtifactHandler) container.lookup(ArtifactHandler.ROLE);
             Field field = DefaultWagonManager.class.getDeclaredField("downloadMonitor");
             field.setAccessible(true);
             AntDownloadMonitor antDownloadMonitor = (AntDownloadMonitor) field.get(wagonManager);
-- 
1.7.11.2

