Skip to content

Commit

Permalink
Merge pull request #45 from ikvmnet/settings
Browse files Browse the repository at this point in the history
Fix user settings resolution
  • Loading branch information
wasabii authored Dec 3, 2023
2 parents 719bb82 + 551c89d commit b59be14
Showing 1 changed file with 35 additions and 12 deletions.
47 changes: 35 additions & 12 deletions src/IKVM.Maven.Sdk.Tasks/MavenEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public override void serviceCreationFailed(Class type, Class impl, System.Except
const string SettingsSecurityXml = "settings-security.xml";
const string DefaultRepositoryType = "default";

static readonly string DefaultRepositoryPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".m2");
static readonly string DefaultRepositoryPath = Path.Combine(java.lang.System.getProperty("user.home"), ".m2");

readonly TaskLoggingHelper log;
readonly string repositoryPath;
Expand Down Expand Up @@ -130,25 +130,49 @@ public IkvmMavenEnvironment(IList<MavenRepositoryItem> repositories, TaskLogging
/// </summary>
public List Repositories => repositories;

/// <summary>
/// Handles a settings problem.
/// </summary>
/// <param name="problem"></param>
void HandleSettingsProblem(SettingsProblem problem)
{
if (problem.getSeverity() == SettingsProblem.Severity.WARNING)
log.LogWarning(problem.getMessage());
else if (problem.getSeverity() == SettingsProblem.Severity.ERROR)
log.LogError(problem.getMessage());
else if (problem.getSeverity() == SettingsProblem.Severity.FATAL)
log.LogCriticalMessage(null, null, null, null, 0, 0, 0, 0, problem.getMessage(), null);
}

/// <summary>
/// Reads the Maven settings.
/// </summary>
/// <returns></returns>
Settings ReadSettings()
{
// initialize a request for the settings
var request = new DefaultSettingsBuildingRequest();
request.setUserSettingsFile(new File(Path.Combine(repositoryPath, SettingsXml)));

var builder = new DefaultSettingsBuilderFactory().newInstance();
var settings = builder.build(request).getEffectiveSettings();

// decrypt security sensitive areas of Maven settings
var securityDispatcher = new SecDispatcher(Path.Combine(repositoryPath, SettingsSecurityXml));
var settingsDecrypter = new DefaultSettingsDecrypter(securityDispatcher);
var rlt = settingsDecrypter.decrypt(new DefaultSettingsDecryptionRequest(settings));
settings.setServers(rlt.getServers());
settings.setProxies(rlt.getProxies());
var settingsResult = builder.build(request);
if (settingsResult.getProblems() is List settingsProblem)
for (var i = settingsProblem.iterator(); i.hasNext();)
HandleSettingsProblem((SettingsProblem)i.next());

// get currently effective settings
var settings = settingsResult.getEffectiveSettings();

// use settings-security.xml to decrypt loaded settings
var secDispatcher = new SecDispatcher(Path.Combine(repositoryPath, SettingsSecurityXml));
var secDecrypter = new DefaultSettingsDecrypter(secDispatcher);
var secResult = secDecrypter.decrypt(new DefaultSettingsDecryptionRequest(settings));
if (secResult.getProblems() is List secProblems)
for (var i = secProblems.iterator(); i.hasNext();)
HandleSettingsProblem((SettingsProblem)i.next());

// apply decrypted settings
settings.setServers(secResult.getServers());
settings.setProxies(secResult.getProxies());

return settings;
}
Expand Down Expand Up @@ -191,8 +215,7 @@ ProxySelector CreateProxySelector()
{
var builder = new AuthenticationBuilder();
builder.addUsername(proxy.getUsername()).addPassword(proxy.getPassword());
selector.add(new org.eclipse.aether.repository.Proxy(
proxy.getProtocol(), proxy.getHost(), proxy.getPort(), builder.build()), proxy.getNonProxyHosts());
selector.add(new org.eclipse.aether.repository.Proxy(proxy.getProtocol(), proxy.getHost(), proxy.getPort(), builder.build()), proxy.getNonProxyHosts());
}

return selector;
Expand Down

0 comments on commit b59be14

Please sign in to comment.