Skip to content

Commit

Permalink
Upgrade Maven to 3.9.
Browse files Browse the repository at this point in the history
Upgrade Maven Resolver to 1.9.
Added support for Exclusions metadata. Takes an artifact ID pattern.
New resolver code made some parsing methods private. Go after them with reflection.
  • Loading branch information
wasabii committed Jan 17, 2024
1 parent a6780d5 commit b428aea
Show file tree
Hide file tree
Showing 11 changed files with 199 additions and 48 deletions.
20 changes: 10 additions & 10 deletions src/IKVM.Maven.Sdk.Tasks.Java/ikvm-maven-sdk-tasks-java.pom
Original file line number Diff line number Diff line change
Expand Up @@ -10,52 +10,52 @@
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-model</artifactId>
<version>3.8.7</version>
<version>3.9.6</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>3.8.7</version>
<version>3.9.6</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-resolver-provider</artifactId>
<version>3.8.7</version>
<version>3.9.6</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-settings-builder</artifactId>
<version>3.8.7</version>
<version>3.9.6</version>
</dependency>
<dependency>
<groupId>org.apache.maven.resolver</groupId>
<artifactId>maven-resolver-api</artifactId>
<version>1.6.3</version>
<version>1.9.18</version>
</dependency>
<dependency>
<groupId>org.apache.maven.resolver</groupId>
<artifactId>maven-resolver-spi</artifactId>
<version>1.6.3</version>
<version>1.9.18</version>
</dependency>
<dependency>
<groupId>org.apache.maven.resolver</groupId>
<artifactId>maven-resolver-impl</artifactId>
<version>1.6.3</version>
<version>1.9.18</version>
</dependency>
<dependency>
<groupId>org.apache.maven.resolver</groupId>
<artifactId>maven-resolver-connector-basic</artifactId>
<version>1.6.3</version>
<version>1.9.18</version>
</dependency>
<dependency>
<groupId>org.apache.maven.resolver</groupId>
<artifactId>maven-resolver-transport-file</artifactId>
<version>1.6.3</version>
<version>1.9.18</version>
</dependency>
<dependency>
<groupId>org.apache.maven.resolver</groupId>
<artifactId>maven-resolver-transport-http</artifactId>
<version>1.6.3</version>
<version>1.9.18</version>
</dependency>
</dependencies>
</project>
29 changes: 29 additions & 0 deletions src/IKVM.Maven.Sdk.Tasks.Tests/MavenReferenceItemResolveTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,35 @@ public void CanResolveFromPrivateRepository()
t.ResolvedReferences.Should().Contain(i => i.ItemSpec == "maven$org.apache.xmlgraphics:fop:2.8");
}

[TestMethod]
public void ExclusionsShouldExcludeSystemDependency()
{
var cacheFile = Path.GetTempFileName();

var engine = new Mock<IBuildEngine>();
var errors = new List<BuildErrorEventArgs>();
engine.Setup(x => x.LogErrorEvent(It.IsAny<BuildErrorEventArgs>())).Callback((BuildErrorEventArgs e) => { errors.Add(e); TestContext.WriteLine("ERROR: " + e.Message); });
engine.Setup(x => x.LogWarningEvent(It.IsAny<BuildWarningEventArgs>())).Callback((BuildWarningEventArgs e) => TestContext.WriteLine("WARNING: " + e.Message));
engine.Setup(x => x.LogMessageEvent(It.IsAny<BuildMessageEventArgs>())).Callback((BuildMessageEventArgs e) => TestContext.WriteLine(e.Message));
var t = new MavenReferenceItemResolve();
t.BuildEngine = engine.Object;
t.CacheFile = cacheFile;
t.Repositories = new[] { GetCentralRepositoryItem() };

var i1 = new TaskItem("net.sf.jt400:jt400:20.0.6");
i1.SetMetadata(MavenReferenceItemMetadata.GroupId, "net.sf.jt400");
i1.SetMetadata(MavenReferenceItemMetadata.ArtifactId, "jt400");
i1.SetMetadata(MavenReferenceItemMetadata.Version, "20.0.6");
i1.SetMetadata(MavenReferenceItemMetadata.Exclusions, "com.sun:tools");

t.References = new[] { i1 };

t.Execute().Should().BeTrue();
errors.Should().BeEmpty();

t.ResolvedReferences.Should().NotContain(i => i.ItemSpec == "maven$com.sun:tools:jar:1.8.0");
}

}

}
1 change: 1 addition & 0 deletions src/IKVM.Maven.Sdk.Tasks/IKVM.Maven.Sdk.Tasks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

<ItemGroup>
<PackageReference Include="IKVM" Version="8.7.3" />
<PackageReference Include="Microsoft.Bcl.HashCode" Version="1.1.1" />
<PackageReference Include="Microsoft.Build" Version="16.9.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="16.9.0" PrivateAssets="All" />
<PackageReference Include="NuGet.Common" Version="6.2.4" />
Expand Down
12 changes: 11 additions & 1 deletion src/IKVM.Maven.Sdk.Tasks/Json/VersionConstraintJsonConverter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
Expand All @@ -13,12 +15,20 @@ namespace IKVM.Maven.Sdk.Tasks.Json
/// </summary>
class VersionConstraintJsonConverter : JsonConverter<org.eclipse.aether.version.VersionConstraint>
{

static readonly MethodInfo parseVersionConstraintMethod = typeof(GenericVersionScheme)
.GetMethods()
.Where(i => i.Name == "parseVersionConstraint")
.Where(i => i.GetParameters().Length == 1 && i.GetParameters()[0].ParameterType == typeof(string))
.Where(i => i.ReturnType == typeof(GenericVersionScheme).Assembly.GetType("org.eclipse.aether.util.version.GenericVersionConstraint"))
.First();

public override org.eclipse.aether.version.VersionConstraint Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType != JsonTokenType.String)
return null;
else
return new GenericVersionScheme().parseVersionConstraint(reader.GetString());
return (org.eclipse.aether.version.VersionConstraint)parseVersionConstraintMethod.Invoke(new GenericVersionScheme(), new[] { reader.GetString() });
}

public override void Write(Utf8JsonWriter writer, org.eclipse.aether.version.VersionConstraint value, JsonSerializerOptions options)
Expand Down
11 changes: 10 additions & 1 deletion src/IKVM.Maven.Sdk.Tasks/Json/VersionJsonConverter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Linq;
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Serialization;

Expand All @@ -13,12 +15,19 @@ namespace IKVM.Maven.Sdk.Tasks.Json
class VersionJsonConverter : JsonConverter<org.eclipse.aether.version.Version>
{

static readonly MethodInfo parseVersionMethod = typeof(GenericVersionScheme)
.GetMethods()
.Where(i => i.Name == "parseVersion")
.Where(i => i.GetParameters().Length == 1 && i.GetParameters()[0].ParameterType == typeof(string))
.Where(i => i.ReturnType == typeof(GenericVersionScheme).Assembly.GetType("org.eclipse.aether.util.version.GenericVersion"))
.First();

public override org.eclipse.aether.version.Version Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType != JsonTokenType.String)
return null;
else
return new GenericVersionScheme().parseVersion(reader.GetString());
return (org.eclipse.aether.version.Version)parseVersionMethod.Invoke(new GenericVersionScheme(), new[] { reader.GetString() });
}

public override void Write(Utf8JsonWriter writer, org.eclipse.aether.version.Version value, JsonSerializerOptions options)
Expand Down
2 changes: 2 additions & 0 deletions src/IKVM.Maven.Sdk.Tasks/MavenEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,8 @@ public RepositorySystemSession CreateRepositorySystemSession(bool noError = fals
session.setTransferListener(new MavenTransferListener(log, noError));
session.setRepositoryListener(new MavenRepositoryListener(log));
session.setConfigProperty(ConflictResolver.CONFIG_PROP_VERBOSE, "true");
session.setSystemProperty("java.version", java.lang.System.getProperty("java.version") ?? "1.8");
session.setOffline(settings.isOffline());
return session;
}

Expand Down
20 changes: 9 additions & 11 deletions src/IKVM.Maven.Sdk.Tasks/MavenReferenceItem.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;

using org.eclipse.aether.util.artifact;

Expand Down Expand Up @@ -47,11 +47,17 @@ class MavenReferenceItem : IEquatable<MavenReferenceItem>
/// </summary>
public string Scope { get; set; } = JavaScopes.COMPILE;

/// <summary>
/// Gets the exclusions of this reference.
/// </summary>
public MavenReferenceItemExclusion[] Exclusions { get; set; }

/// <summary>
/// Originator of the reference item.
/// </summary>
public string ReferenceSource { get; set; }

/// <inheritdoc />
public override bool Equals(object obj)
{
return Equals(obj as MavenReferenceItem);
Expand All @@ -72,6 +78,7 @@ public bool Equals(MavenReferenceItem other)
Version == other.Version &&
Optional == other.Optional &&
Scope == other.Scope &&
Enumerable.SequenceEqual(Exclusions, Exclusions) &&
ReferenceSource == other.ReferenceSource;
}

Expand All @@ -81,16 +88,7 @@ public bool Equals(MavenReferenceItem other)
/// <returns></returns>
public override int GetHashCode()
{
int hashCode = 1928079503;
hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(ItemSpec);
hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(GroupId);
hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(ArtifactId);
hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(Classifier);
hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(Version);
hashCode = hashCode * -1521134295 + Optional.GetHashCode();
hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(Scope);
hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(ReferenceSource);
return hashCode;
return HashCode.Combine(HashCode.Combine(ItemSpec, GroupId, ArtifactId, Classifier, Version, Optional, Scope, Exclusions), ReferenceSource);
}

/// <summary>
Expand Down
75 changes: 75 additions & 0 deletions src/IKVM.Maven.Sdk.Tasks/MavenReferenceItemExclusion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System;

namespace IKVM.Maven.Sdk.Tasks
{

public class MavenReferenceItemExclusion : IEquatable<MavenReferenceItemExclusion>
{

/// <summary>
/// Initializes a new instance.
/// </summary>
/// <param name="groupId"></param>
/// <param name="artifactId"></param>
/// <param name="classifier"></param>
/// <param name="extension"></param>
public MavenReferenceItemExclusion(string groupId, string artifactId, string classifier, string extension)
{
GroupId = groupId ?? throw new ArgumentNullException(nameof(groupId));
ArtifactId = artifactId ?? throw new ArgumentNullException(nameof(artifactId));
Classifier = classifier;
Extension = extension;
}

/// <summary>
/// The Maven group ID. Required.
/// </summary>
public string GroupId { get; set; }

/// <summary>
/// The Maven artifact ID. Required.
/// </summary>
public string ArtifactId { get; set; }

/// <summary>
/// Gets the classifier of the exclusion.
/// </summary>
public string Classifier { get; set; }

/// <summary>
/// Gets the extension ID of the exclusion.
/// </summary>
public string Extension { get; set; }

/// <inheritdoc />
public override bool Equals(object obj)
{
return Equals(obj as MavenReferenceItemExclusion);
}

/// <inheritdoc />
public override int GetHashCode()
{
return HashCode.Combine(GroupId, ArtifactId, Classifier, Extension);
}

/// <summary>
/// Returns <c>true</c> if the this item is equal to the other item.
/// </summary>
/// <param name="other"></param>
/// <returns></returns>
public bool Equals(MavenReferenceItemExclusion other)
{
return other is not null && GroupId == other.GroupId && ArtifactId == other.ArtifactId && Classifier == other.Classifier && Extension == other.Extension;
}


/// <inheritdoc />
public override string ToString()
{
return $"{GroupId}:{ArtifactId}";
}

}

}
24 changes: 24 additions & 0 deletions src/IKVM.Maven.Sdk.Tasks/MavenReferenceItemMetadata.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;

using Microsoft.Build.Framework;

Expand All @@ -19,6 +20,7 @@ static class MavenReferenceItemMetadata
public static readonly string Dependencies = "Dependencies";
public static readonly string Scope = "Scope";
public static readonly string Optional = "Optional";
public static readonly string Exclusions = "Exclusions";
public static readonly string Debug = "Debug";
public static readonly string ReferenceSource = "ReferenceSource";

Expand All @@ -39,6 +41,7 @@ public static void Save(MavenReferenceItem item, ITaskItem task)
task.SetMetadata(MavenReferenceItemMetadata.Version, item.Version);
task.SetMetadata(MavenReferenceItemMetadata.Optional, item.Optional ? "true" : "false");
task.SetMetadata(MavenReferenceItemMetadata.Scope, item.Scope);
task.SetMetadata(MavenReferenceItemMetadata.Exclusions, string.Join(";", item.Exclusions.Select(i => i.ToString())));
task.SetMetadata(MavenReferenceItemMetadata.ReferenceSource, item.ReferenceSource);
}

Expand All @@ -65,6 +68,7 @@ public static MavenReferenceItem[] Import(IEnumerable<ITaskItem> tasks)
item.Version = task.GetMetadata(MavenReferenceItemMetadata.Version);
item.Optional = string.Equals(task.GetMetadata(MavenReferenceItemMetadata.Optional), "true", StringComparison.OrdinalIgnoreCase);
item.Scope = task.GetMetadata(MavenReferenceItemMetadata.Scope);
item.Exclusions = task.GetMetadata(MavenReferenceItemMetadata.Exclusions).Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries).Select(i => ParseExclusion(i)).Where(i => i != null).ToArray();
item.ReferenceSource = task.GetMetadata(MavenReferenceItemMetadata.ReferenceSource);
list.Add(item);
}
Expand All @@ -73,6 +77,26 @@ public static MavenReferenceItem[] Import(IEnumerable<ITaskItem> tasks)
return list.ToArray();
}

/// <summary>
/// Parses a compressed exclusion string.
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
/// <exception cref="MavenTaskMessageException"></exception>
static MavenReferenceItemExclusion ParseExclusion(string value)
{
var a = value.Split(':');
if (a.Length is 2 or 3 or 4)
{
var groupId = a[0];
var artifactId = a[1];
var classifier = a.Length >= 3 ? a[2] : null;
var extension = a.Length >= 4 ? a[3] : null;
return new MavenReferenceItemExclusion(groupId, artifactId, classifier, extension);
}

return null;
}
}

}
Loading

0 comments on commit b428aea

Please sign in to comment.