Skip to content

Commit

Permalink
SHRINKRES-127 Honor global settings.xml in M2_HOME
Browse files Browse the repository at this point in the history
  • Loading branch information
kpiwko committed May 23, 2013
1 parent 209ad81 commit 8a52380
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ public class MavenSettingsBuilder {
*/
public static final String ALT_LOCAL_REPOSITORY_LOCATION = "maven.repo.local";

// path to global settings.xml
private static final String DEFAULT_GLOBAL_SETTINGS_PATH;
// path to the user settings.xml
private static final String DEFAULT_USER_SETTINGS_PATH;
// path to the default local repository
Expand All @@ -73,10 +75,19 @@ public class MavenSettingsBuilder {
static {
// it might happen that "user.home" is not defined
String userHome = SecurityActions.getProperty("user.home");
// it might happen that "M2_HOME" is not defined
String m2HomeEnv = SecurityActions.getEnvProperty("M2_HOME");
String m2HomeProp = SecurityActions.getProperty("maven.home");
String m2Home = m2HomeEnv == null ? m2HomeProp : m2HomeEnv;

// note that pointing settings.xml to a non existining file does not matter here
DEFAULT_GLOBAL_SETTINGS_PATH = m2Home == null ? "conf/settings.xml" : m2Home.concat("/conf/settings.xml".replaceAll(
"/", File.separator));
DEFAULT_USER_SETTINGS_PATH = userHome == null ? "settings.xml" : userHome.concat("/.m2/settings.xml".replaceAll("/",
File.separator));
DEFAULT_REPOSITORY_PATH = userHome == null ? "repository" : userHome.concat("/.m2/repository".replaceAll("/",
File.separator));

}

/**
Expand All @@ -85,23 +96,7 @@ public class MavenSettingsBuilder {
* @return
*/
public Settings buildDefaultSettings() {
SettingsBuildingRequest request = new DefaultSettingsBuildingRequest().setSystemProperties(SecurityActions
.getProperties());

String altUserSettings = SecurityActions.getProperty(ALT_USER_SETTINGS_XML_LOCATION);
String altGlobalSettings = SecurityActions.getProperty(ALT_GLOBAL_SETTINGS_XML_LOCATION);

request.setUserSettingsFile(new File(DEFAULT_USER_SETTINGS_PATH));
// set alternate files
if (altUserSettings != null && altUserSettings.length() > 0) {
request.setUserSettingsFile(new File(altUserSettings));
}

if (altGlobalSettings != null && altGlobalSettings.length() > 0) {
request.setGlobalSettingsFile(new File(altGlobalSettings));
}

return buildSettings(request);
return buildSettings(getDefaultSettingsBuildingRequest());
}

/**
Expand Down Expand Up @@ -177,6 +172,27 @@ public Settings buildSettings(SettingsBuildingRequest request) {
return settings;
}

private SettingsBuildingRequest getDefaultSettingsBuildingRequest() {
SettingsBuildingRequest request = new DefaultSettingsBuildingRequest().setSystemProperties(SecurityActions
.getProperties());

String altUserSettings = SecurityActions.getProperty(ALT_USER_SETTINGS_XML_LOCATION);
String altGlobalSettings = SecurityActions.getProperty(ALT_GLOBAL_SETTINGS_XML_LOCATION);

request.setGlobalSettingsFile(new File(DEFAULT_GLOBAL_SETTINGS_PATH));
request.setUserSettingsFile(new File(DEFAULT_USER_SETTINGS_PATH));
// set alternate files
if (altUserSettings != null && altUserSettings.length() > 0) {
request.setUserSettingsFile(new File(altUserSettings));
}

if (altGlobalSettings != null && altGlobalSettings.length() > 0) {
request.setGlobalSettingsFile(new File(altGlobalSettings));
}

return request;
}

// adds local repository
private Settings enrichWithLocalRepository(Settings settings) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,40 @@ public String run() {
}
}

static String getEnvProperty(final String key) {
try {
String value = AccessController.doPrivileged(new PrivilegedExceptionAction<String>() {
@Override
public String run() {
return System.getenv(key);
}
});
return value;
}
// Unwrap
catch (final PrivilegedActionException pae) {
final Throwable t = pae.getCause();
// Rethrow
if (t instanceof SecurityException) {
throw (SecurityException) t;
}
if (t instanceof NullPointerException) {
throw (NullPointerException) t;
} else if (t instanceof IllegalArgumentException) {
throw (IllegalArgumentException) t;
} else {
// No other checked Exception thrown by System.getProperty
try {
throw (RuntimeException) t;
}
// Just in case we've really messed up
catch (final ClassCastException cce) {
throw new RuntimeException("Obtained unchecked Exception; this code should never be reached", t);
}
}
}
}

static Properties getProperties() {
try {
return AccessController.doPrivileged(new PrivilegedExceptionAction<Properties>() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2013, Red Hat Middleware LLC, and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed 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.jboss.shrinkwrap.resolver.impl.maven.bootstrap;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.nullValue;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.apache.maven.settings.building.SettingsBuildingRequest;
import org.junit.Assert;
import org.junit.Assume;
import org.junit.BeforeClass;
import org.junit.Test;

/**
* Verifies that default paths to maven settings.xml files are set by default.
* See https://issues.jboss.org/browse/SHRINKRES-127 for more details.
*
* @author <a href="kpiwko@redhat.com">Karel Piwko</a>
*
*/
public class DefaultSettingsXmlLocationTestCase {

@BeforeClass
public static void beforeClass() {
System.clearProperty(MavenSettingsBuilder.ALT_USER_SETTINGS_XML_LOCATION);
System.clearProperty(MavenSettingsBuilder.ALT_GLOBAL_SETTINGS_XML_LOCATION);
}

@Test
public void loadDefaultUserSettingsXmlLocation() {

// user.home might not be set, so ignore test if that happens
Assume.assumeThat(System.getProperty("user.home"), is(not(nullValue())));

SettingsBuildingRequest request = createBuildingRequest();
Assert.assertThat(request.getUserSettingsFile(), is(not(nullValue())));

Assert.assertThat(request.getUserSettingsFile().getPath(),
is(System.getProperty("user.home") + "/.m2/settings.xml".replaceAll("/", File.separator)));
}

@Test
public void loadDefaultGlobalSettingsXmlLocation() {

// M2_HOME is optional, so ignore test if that happens
Assume.assumeThat(System.getenv("M2_HOME"), is(not(nullValue())));

SettingsBuildingRequest request = createBuildingRequest();
Assert.assertThat(request.getGlobalSettingsFile(), is(not(nullValue())));

Assert.assertThat(request.getGlobalSettingsFile().getPath(),
is(System.getenv("M2_HOME") + "/conf/settings.xml".replaceAll("/", File.separator)));
}

// this is calling internal private method that handles logic of settings.xml setup
private SettingsBuildingRequest createBuildingRequest() {
try {
MavenSettingsBuilder builder = new MavenSettingsBuilder();
Class<? extends MavenSettingsBuilder> clazz = builder.getClass();
Method m = clazz.getDeclaredMethod("getDefaultSettingsBuildingRequest");
m.setAccessible(true);
return (SettingsBuildingRequest) m.invoke(builder);
} catch (SecurityException e) {
e.printStackTrace();
Assert.fail("Unable to call getDefaultSettingsBuildingRequest via reflection, reason: " + e.getMessage());
} catch (NoSuchMethodException e) {
e.printStackTrace();
Assert.fail("Unable to call getDefaultSettingsBuildingRequest via reflection, reason: " + e.getMessage());
} catch (IllegalArgumentException e) {
e.printStackTrace();
Assert.fail("Unable to call getDefaultSettingsBuildingRequest via reflection, reason: " + e.getMessage());
} catch (IllegalAccessException e) {
e.printStackTrace();
Assert.fail("Unable to call getDefaultSettingsBuildingRequest via reflection, reason: " + e.getMessage());
} catch (InvocationTargetException e) {
e.printStackTrace();
Assert.fail("Unable to call getDefaultSettingsBuildingRequest via reflection, reason: " + e.getMessage());
}

return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.shrinkwrap.resolver.impl.maven.integration;
package org.jboss.shrinkwrap.resolver.impl.maven.bootstrap;

import org.apache.maven.settings.Settings;
import org.jboss.shrinkwrap.resolver.impl.maven.bootstrap.MavenSettingsBuilder;
Expand Down

0 comments on commit 8a52380

Please sign in to comment.