package org.kie.scanner.embedder;

import org.apache.maven.settings.Settings;
import org.apache.maven.settings.building.DefaultSettingsBuilderFactory;
import org.apache.maven.settings.building.DefaultSettingsBuildingRequest;
import org.apache.maven.settings.building.SettingsBuilder;
import org.apache.maven.settings.building.SettingsBuildingException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;

public class MavenSettings {

    private static final Logger log = LoggerFactory.getLogger(MavenSettings.class);

    private static final Settings settings = initSettings();

    public static Settings getSettings() {
        return settings;
    }

    private static Settings initSettings() {
        SettingsBuilder settingsBuilder = new DefaultSettingsBuilderFactory().newInstance();

        DefaultSettingsBuildingRequest request = new DefaultSettingsBuildingRequest();

        String userHome = System.getProperty( "user.home" );
        if (userHome != null) {
            File userSettingsFile = new File( userHome + "/.m2/settings.xml" );
            if (userSettingsFile.exists()) {
                request.setUserSettingsFile( userSettingsFile );
            }
        } else {
            log.warn("User home is not set");
        }

        String mavenHome = System.getenv( "M2_HOME" );
        if (mavenHome != null) {
            File globalSettingsFile = new File( mavenHome + "/conf/settings.xml" );
            if (globalSettingsFile.exists()) {
                request.setGlobalSettingsFile( globalSettingsFile );
            }
        } else {
            log.warn("Environment variable M2_HOME is not set");
        }


        request.setSystemProperties( System.getProperties() );

        Settings settings = null;
        try {
            settings = settingsBuilder.build( request ).getEffectiveSettings();
        } catch ( SettingsBuildingException e ) {
            new RuntimeException(e);
        }

        if (settings.getLocalRepository() == null) {
            if (userHome != null) {
                settings.setLocalRepository( userHome + "/.m2/repository" );
            } else {
                log.error("Cannot find maven local repository");
            }
        }

        return settings;
    }

}

@@ -537,24 +537,113 @@
 gs.class);%0A%0A
+    private static final String CUSTOM_SETTINGS_PROPERTY = %22kie.maven.settings.custom%22;%0A%0A
     private 
@@ -990,24 +990,556 @@
 Request();%0A%0A
+        boolean useCustomSettingsLocation = false;%0A        String customSettings = System.getProperty( CUSTOM_SETTINGS_PROPERTY );%0A        if (customSettings != null) %7B%0A            File customSettingsFile = new File( customSettings );%0A            if (customSettingsFile.exists()) %7B%0A                request.setUserSettingsFile( customSettingsFile );%0A                useCustomSettingsLocation = true;%0A            %7D else %7B%0A                log.warn(%22Cannot find custom maven settings file: %22 + customSettings);%0A            %7D%0A        %7D%0A%0A
         Stri
@@ -1583,24 +1583,72 @@
 er.home%22 );%0A
+        if ( !useCustomSettingsLocation ) %7B%0A    
         if (
@@ -1675,24 +1675,28 @@
             
+    
 File userSet
@@ -1755,32 +1755,36 @@
  );%0A            
+    
 if (userSettings
@@ -1808,32 +1808,36 @@
                 
+  
+  
 request.setUserS
@@ -1877,34 +1877,41 @@
             
-%7D%0A
+    %7D%0A   
         
 %7D else %7B%0A   
@@ -1890,32 +1890,33 @@
    %7D%0A           
+ 
 %7D else %7B%0A       
@@ -1900,32 +1900,36 @@
        %7D else %7B%0A
+    
             log.
@@ -1950,32 +1950,46 @@
 e is not set%22);%0A
+            %7D%0A
         %7D%0A%0A     

allow to set a custom maven setting file