3939import org .sonatype .plexus .components .sec .dispatcher .DefaultSecDispatcher ;
4040
4141/**
42- * {@code MavenSettingsReader} reads settings from a user's Maven settings.xml file,
43- * decrypting them if necessary using settings-security.xml.
42+ * {@code MavenSettingsReader} reads settings from a user's Maven settings.xml
43+ * file, decrypting them if necessary using settings-security.xml.
4444 *
4545 * @author Andy Wilkinson
4646 * @since 1.3.0
@@ -57,7 +57,20 @@ public MavenSettingsReader() {
5757
5858 public MavenSettingsReader (String homeDir ) {
5959 if (homeDir == null ) {
60- homeDir = System .getProperty ("settings.home" , System .getProperty ("user.home" ));
60+ homeDir = System .getProperty ("settings.home" );
61+ if (homeDir == null ) {
62+ homeDir = System .getProperty ("maven.repo.local" );
63+ if (homeDir != null ) {
64+ homeDir = new File (homeDir ).getParent ();
65+ if (homeDir != null ) {
66+ homeDir = new File (homeDir ).getParent ();
67+ } else {
68+ homeDir = System .getProperty ("user.home" );
69+ }
70+ } else {
71+ homeDir = System .getProperty ("user.home" );
72+ }
73+ }
6174 }
6275 this .homeDir = homeDir ;
6376 }
@@ -77,8 +90,7 @@ public static void applySettings(MavenSettings settings, DefaultRepositorySystem
7790 try {
7891 session .setLocalRepositoryManager (new SimpleLocalRepositoryManagerFactory ().newInstance (session ,
7992 new LocalRepository (settings .getLocalRepository ())));
80- }
81- catch (NoLocalRepositoryManagerException e ) {
93+ } catch (NoLocalRepositoryManagerException e ) {
8294 throw new IllegalStateException ("Cannot set local repository to " + settings .getLocalRepository (), e );
8395 }
8496 }
@@ -92,14 +104,27 @@ private Settings loadSettings() {
92104 File settingsFile = new File (this .homeDir , ".m2/settings.xml" );
93105 if (settingsFile .exists ()) {
94106 log .info ("Reading settings from: " + settingsFile );
95- }
96- else {
107+ } else {
97108 log .info ("No settings found at: " + settingsFile );
98- String home = System .getProperty ("user.home" );
99- if (!new File (home ).getAbsolutePath ().equals (new File (this .homeDir ).getAbsolutePath ())) {
100- settingsFile = new File (home , ".m2/settings.xml" );
101- if (settingsFile .exists ()) {
102- log .info ("Reading settings from: " + settingsFile );
109+ settingsFile = new File (this .homeDir , "../settings.xml" );
110+ if (settingsFile .exists ()) {
111+ log .info ("Reading settings from: " + settingsFile );
112+ } else {
113+ String home = System .getProperty ("maven.repo.local" );
114+ if (home != null
115+ && !new File (home ).getAbsolutePath ().equals (new File (this .homeDir ).getAbsolutePath ())) {
116+ settingsFile = new File (home , "../settings.xml" );
117+ if (settingsFile .exists ()) {
118+ log .info ("Reading settings from: " + settingsFile );
119+ }
120+ } else {
121+ home = System .getProperty ("user.home" );
122+ if (!new File (home ).getAbsolutePath ().equals (new File (this .homeDir ).getAbsolutePath ())) {
123+ settingsFile = new File (home , ".m2/settings.xml" );
124+ if (settingsFile .exists ()) {
125+ log .info ("Reading settings from: " + settingsFile );
126+ }
127+ }
103128 }
104129 }
105130 }
@@ -112,8 +137,7 @@ private Settings loadSettings(File settingsFile) {
112137 request .setSystemProperties (System .getProperties ());
113138 try {
114139 return new DefaultSettingsBuilderFactory ().newInstance ().build (request ).getEffectiveSettings ();
115- }
116- catch (SettingsBuildingException ex ) {
140+ } catch (SettingsBuildingException ex ) {
117141 throw new IllegalStateException ("Failed to build settings from " + settingsFile , ex );
118142 }
119143 }
@@ -136,8 +160,7 @@ private void setField(Class<?> sourceClass, String fieldName, Object target, Obj
136160 Field field = sourceClass .getDeclaredField (fieldName );
137161 field .setAccessible (true );
138162 field .set (target , value );
139- }
140- catch (Exception ex ) {
163+ } catch (Exception ex ) {
141164 throw new IllegalStateException ("Failed to set field '" + fieldName + "' on '" + target + "'" , ex );
142165 }
143166 }
@@ -151,8 +174,7 @@ private class SpringBootSecDispatcher extends DefaultSecDispatcher {
151174 this ._configurationFile = file .getAbsolutePath ();
152175 try {
153176 this ._cipher = new DefaultPlexusCipher ();
154- }
155- catch (PlexusCipherException e ) {
177+ } catch (PlexusCipherException e ) {
156178 throw new IllegalStateException (e );
157179 }
158180 }
0 commit comments