private void GetMainConfiguration()
        {
            FileInfo artifactoryConfigurationFile = new FileInfo(_task.SolutionRoot + "\\.artifactory\\Artifactory.build");

            if (!artifactoryConfigurationFile.Exists)
                throw new Exception("The main configuration file are missing! (Location: solutionDir\\.artifactory)");

            StringBuilder xmlContent = MsbuildInterpreter(artifactoryConfigurationFile, _task);
            System.Xml.Serialization.XmlSerializer reader = new System.Xml.Serialization.XmlSerializer(typeof(ArtifactoryConfig));
            MainArtifactoryConfiguration = (ArtifactoryConfig)reader.Deserialize(new System.IO.StringReader(xmlContent.ToString()));
        }
        public static Build extractBuild(ArtifactoryBuild task, ArtifactoryConfig artifactoryConfig, BuildInfoLog log)
        {
            Build build = new Build
            {
                modules = new List<Module>(),
            };

            build.started = string.Format(Build.STARTED_FORMAT, task.StartTime);
            build.artifactoryPrincipal = task.User;
            build.buildAgent = new BuildAgent { name = "MSBuild", version = task.ToolVersion };
            build.type = "MSBuild";

            build.agent = Agent.BuildAgentFactory(task);

            //get the current use from the windows OS
            System.Security.Principal.WindowsIdentity user;
            user = System.Security.Principal.WindowsIdentity.GetCurrent();
            if (user != null) build.principal = string.Format(@"{0}", user.Name);

            //Calculate how long it took to do the build
            DateTime start = DateTime.ParseExact(task.StartTime, artifactoryDateFormat, null);
            build.startedDateMillis = GetTimeStamp();
            build.durationMillis = Convert.ToInt64((DateTime.Now - start).TotalMilliseconds);

            build.number = string.IsNullOrWhiteSpace(task.BuildNumber) ? build.startedDateMillis : task.BuildNumber;
            build.name = task.BuildName ?? task.ProjectName;
            build.url = build.agent.BuildAgentUrl();
            build.vcsRevision = task.VcsRevision;

            //Add build server properties, if exists.
            AddSystemVariables(artifactoryConfig, build);
            AddLicenseControl(artifactoryConfig, build, log);
            AddBlackDuck(artifactoryConfig, build, log);

            ConfigHttpClient(artifactoryConfig, build);

            return build;
        }
        private static void ConfigHttpClient(ArtifactoryConfig artifactoryConfig, Build build)
        {
            build.deployClient = new DeployClient();
            if (!string.IsNullOrWhiteSpace(artifactoryConfig.PropertyGroup.ConnectionTimeout))
                build.deployClient.timeout = int.Parse(artifactoryConfig.PropertyGroup.ConnectionTimeout);

            ProxySettings proxySettings = artifactoryConfig.PropertyGroup.ProxySettings;

            //Check if the user Bypass proxy settings
            if (!string.IsNullOrWhiteSpace(proxySettings.Bypass) && proxySettings.Bypass.ToLower().Equals("true")) 
            {             
                    build.deployClient.proxy = new Proxy();
                    build.deployClient.proxy.IsBypass = true;
                    return;             
            }

            /*
            * Incase that the proxy settings, is not set in the plugin level, we need
            * to check for proxy settings in the environment variables.
            */
            if (string.IsNullOrWhiteSpace(proxySettings.Host)) 
            {
                ProxySettings envVariableProxy = proxyEnvVariables();
                if (envVariableProxy != null)
                {
                    proxySettings = envVariableProxy;
                }
                else 
                {
                    build.deployClient.proxy = new Proxy();
                    build.deployClient.proxy.IsBypass = true;
                    return;  
                }
            }

            if (!string.IsNullOrWhiteSpace(proxySettings.UserName) && !string.IsNullOrWhiteSpace(proxySettings.Password))
                build.deployClient.proxy = new Proxy(proxySettings.Host, proxySettings.Port, proxySettings.UserName, proxySettings.Password);
            else
                build.deployClient.proxy = new Proxy(proxySettings.Host, proxySettings.Port);

            build.deployClient.proxy.IsBypass = false;
        }
        private static void AddBlackDuck(ArtifactoryConfig artifactoryConfig, Build build, BuildInfoLog log)
        {
            if (artifactoryConfig.PropertyGroup.BlackDuckCheck == null)
                return;

            BlackDuckGovernance blackDuckControl = new BlackDuckGovernance();
            blackDuckControl.runChecks = artifactoryConfig.PropertyGroup.BlackDuckCheck.EnabledBlackDuckCheck;
            blackDuckControl.appName = artifactoryConfig.PropertyGroup.BlackDuckCheck.CodeCenterApplicationName;
            blackDuckControl.appVersion = artifactoryConfig.PropertyGroup.BlackDuckCheck.CodeCenterApplicationVersion;
            blackDuckControl.reportRecipients = new List<string>();
            blackDuckControl.scopes = new List<string>();
            blackDuckControl.includePublishedArtifacts = artifactoryConfig.PropertyGroup.BlackDuckCheck.IncludePublishedArtifacts;
            blackDuckControl.autoCreateMissingComponentRequests = artifactoryConfig.PropertyGroup.BlackDuckCheck.AutoCreateMissingComponent;
            blackDuckControl.autoDiscardStaleComponentRequests = artifactoryConfig.PropertyGroup.BlackDuckCheck.AutoDiscardStaleComponent;

            foreach (Recipient recip in artifactoryConfig.PropertyGroup.BlackDuckCheck.ComplianceReportRecipients.Recipient)
            {
                if (validateEmail(recip))
                {
                    blackDuckControl.reportRecipients.Add(recip.email);
                }
                else
                {
                    log.Warning("Invalid email address, under License Control violation recipients.");
                }
            }

            foreach (Scope scope in artifactoryConfig.PropertyGroup.BlackDuckCheck.ScopesForLicenseAnalysis.Scope)
            {
                blackDuckControl.scopes.Add(scope.value);
            }

            build.blackDuckGovernance = blackDuckControl;
        }
        private static void AddLicenseControl(ArtifactoryConfig artifactoryConfig, Build build, BuildInfoLog log)
        {
            if (artifactoryConfig.PropertyGroup.LicenseControlCheck == null)
                return;

            LicenseControl licenseControl = new LicenseControl();
            licenseControl.runChecks = artifactoryConfig.PropertyGroup.LicenseControlCheck.EnabledLicenseControl;
            licenseControl.autoDiscover = artifactoryConfig.PropertyGroup.LicenseControlCheck.AutomaticLicenseDiscovery;
            licenseControl.includePublishedArtifacts = artifactoryConfig.PropertyGroup.LicenseControlCheck.IncludePublishedArtifacts;
            licenseControl.licenseViolationsRecipients = new List<string>();
            licenseControl.scopes = new List<string>();
           
            foreach (Recipient recip in artifactoryConfig.PropertyGroup.LicenseControlCheck.LicenseViolationRecipients.Recipient)
            {
                if (validateEmail(recip))
                {
                    licenseControl.licenseViolationsRecipients.Add(recip.email);
                }
                else 
                {
                    log.Warning("Invalid email address, under License Control violation recipients.");
                }                            
            }

            foreach (Scope scope in artifactoryConfig.PropertyGroup.LicenseControlCheck.ScopesForLicenseAnalysis.Scope)
            {
                licenseControl.scopes.Add(scope.value);
            }

            build.licenseControl = licenseControl;
        }
        /// <summary>
        /// Gather all windows system variables and their values
        /// </summary>
        /// <returns></returns>
        private static void AddSystemVariables(ArtifactoryConfig artifactoryConfig, Build build)
        {
            if (artifactoryConfig.PropertyGroup.EnvironmentVariables == null)
                return;

            string enable = artifactoryConfig.PropertyGroup.EnvironmentVariables.EnabledEnvVariable;
            if (string.IsNullOrWhiteSpace(enable) || !enable.ToLower().Equals("true"))
                return;

            // includePatterns = new List<Pattern>();
            //List<Pattern> excludePatterns = new List<Pattern>();
            List<Pattern> includePatterns = artifactoryConfig.PropertyGroup.EnvironmentVariables.IncludePatterns.Pattern;
            List<Pattern> excludePatterns = artifactoryConfig.PropertyGroup.EnvironmentVariables.ExcludePatterns.Pattern;

            StringBuilder includeRegexUnion = new StringBuilder();
            StringBuilder excludeRegexUnion = new StringBuilder();

            if (includePatterns != null && includePatterns.Count > 0) 
            {
                includePatterns.ForEach(pattern => includeRegexUnion.Append(WildcardToRegex(pattern.key)).Append("|"));
                includeRegexUnion.Remove(includeRegexUnion.Length - 1, 1);
            }

            if (excludePatterns != null && excludePatterns.Count > 0) 
            {
                excludePatterns.ForEach(pattern => excludeRegexUnion.Append(WildcardToRegex(pattern.key)).Append("|"));
                excludeRegexUnion.Remove(excludeRegexUnion.Length - 1, 1);
            }

            Regex includeRegex = new Regex(includeRegexUnion.ToString(), RegexOptions.IgnoreCase);
            Regex excludeRegex = new Regex(excludeRegexUnion.ToString(), RegexOptions.IgnoreCase);

            //System.Environment.GetEnvironmentVariables()
            //EnvironmentVariableTarget
            IDictionary sysVariables = Environment.GetEnvironmentVariables(EnvironmentVariableTarget.Process);
            var dicVariables = new Dictionary<string, string>();

            var environmentVariables = build.agent.BuildAgentEnvironment();

            foreach(string key in sysVariables.Keys)
            {
                if (!environmentVariables.ContainsKey(string.Concat(Agent.PRE_FIX_ENV, key)) && !PathConflicts(includePatterns, excludePatterns, includeRegex, excludeRegex, key)) 
                {
                    dicVariables.Add(key, (string)sysVariables[key]);
                }
            }

            dicVariables.AddRange(environmentVariables);

            build.properties = dicVariables;
        }