Skip to content

Commit

Permalink
Merge pull request #43 from ikvmnet/runtimenotcopied
Browse files Browse the repository at this point in the history
Runtime dependencies not copied to output
  • Loading branch information
wasabii authored Dec 3, 2023
2 parents 8738f1f + 3b5b391 commit 719bb82
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 34 deletions.
113 changes: 113 additions & 0 deletions src/IKVM.Maven.Sdk.Tasks.Tests/MavenReferenceItemResolveTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,119 @@ public void ShouldIncludeProvidedDependencies()
pkg.GetMetadata("References").Split(';').Should().Contain("maven$org.graalvm.sdk:graal-sdk:22.3.2");
}

[TestMethod]
public void CompileDependencyShouldBePrivateAndReferenced()
{
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));
var t = new MavenReferenceItemResolve();
t.BuildEngine = engine.Object;
t.CacheFile = cacheFile;
t.Repositories = new[] { GetCentralRepositoryItem() };

var i1 = new TaskItem("javax.inject:javax.inject:1");
i1.SetMetadata(MavenReferenceItemMetadata.GroupId, "javax.inject");
i1.SetMetadata(MavenReferenceItemMetadata.ArtifactId, "javax.inject");
i1.SetMetadata(MavenReferenceItemMetadata.Version, "1");
i1.SetMetadata(MavenReferenceItemMetadata.Scope, "compile");
t.References = new[] { i1 };

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

t.ResolvedReferences.Should().Contain(i => i.ItemSpec == "maven$javax.inject:javax.inject:1");
var pkg = t.ResolvedReferences.First(i => i.ItemSpec == "maven$javax.inject:javax.inject:1");
pkg.GetMetadata("Private").Should().Be("true");
pkg.GetMetadata("ReferenceOutputAssembly").Should().Be("true");
}

[TestMethod]
public void RuntimeDependencyShouldBePrivateAndReferenced()
{
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));
var t = new MavenReferenceItemResolve();
t.BuildEngine = engine.Object;
t.CacheFile = cacheFile;
t.Repositories = new[] { GetCentralRepositoryItem() };

var i1 = new TaskItem("javax.inject:javax.inject:1");
i1.SetMetadata(MavenReferenceItemMetadata.GroupId, "javax.inject");
i1.SetMetadata(MavenReferenceItemMetadata.ArtifactId, "javax.inject");
i1.SetMetadata(MavenReferenceItemMetadata.Version, "1");
i1.SetMetadata(MavenReferenceItemMetadata.Scope, "runtime");
t.References = new[] { i1 };

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

t.ResolvedReferences.Should().Contain(i => i.ItemSpec == "maven$javax.inject:javax.inject:1");
var pkg = t.ResolvedReferences.First(i => i.ItemSpec == "maven$javax.inject:javax.inject:1");
pkg.GetMetadata("Private").Should().Be("true");
pkg.GetMetadata("ReferenceOutputAssembly").Should().Be("false");
}

[TestMethod]
public void ProvidedDependencyShouldBeNotPrivateAndReferenced()
{
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));
var t = new MavenReferenceItemResolve();
t.BuildEngine = engine.Object;
t.CacheFile = cacheFile;
t.Repositories = new[] { GetCentralRepositoryItem() };

var i1 = new TaskItem("javax.inject:javax.inject:1");
i1.SetMetadata(MavenReferenceItemMetadata.GroupId, "javax.inject");
i1.SetMetadata(MavenReferenceItemMetadata.ArtifactId, "javax.inject");
i1.SetMetadata(MavenReferenceItemMetadata.Version, "1");
i1.SetMetadata(MavenReferenceItemMetadata.Scope, "provided");
t.References = new[] { i1 };

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

t.ResolvedReferences.Should().Contain(i => i.ItemSpec == "maven$javax.inject:javax.inject:1");
var pkg = t.ResolvedReferences.First(i => i.ItemSpec == "maven$javax.inject:javax.inject:1");
pkg.GetMetadata("Private").Should().Be("false");
pkg.GetMetadata("ReferenceOutputAssembly").Should().Be("true");
}

[TestMethod]
public void SystemDependencyShouldBeExcluded()
{
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));
var t = new MavenReferenceItemResolve();
t.BuildEngine = engine.Object;
t.CacheFile = cacheFile;
t.Repositories = new[] { GetCentralRepositoryItem() };

var i1 = new TaskItem("javax.inject:javax.inject:1");
i1.SetMetadata(MavenReferenceItemMetadata.GroupId, "javax.inject");
i1.SetMetadata(MavenReferenceItemMetadata.ArtifactId, "javax.inject");
i1.SetMetadata(MavenReferenceItemMetadata.Version, "1");
i1.SetMetadata(MavenReferenceItemMetadata.Scope, "system");
t.References = new[] { i1 };

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

t.ResolvedReferences.Should().NotContain(i => i.ItemSpec == "maven$javax.inject:javax.inject:1");
}

[TestMethod]
public void CanResolveApacheFop()
{
Expand Down
67 changes: 35 additions & 32 deletions src/IKVM.Maven.Sdk.Tasks/MavenReferenceItemResolve.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ IEnumerable<IkvmReferenceItem> ResolveReferences(IList<MavenRepositoryItem> repo
CollectIkvmReferenceItems(output, graph);

// resolve compile and runtime items and ensure they are copied
var privateScopes = new List<string>() { JavaScopes.COMPILE, JavaScopes.RUNTIME };
var privateScopes = new List<string>() { JavaScopes.RUNTIME };
if (IncludeTestScope)
privateScopes.Add(JavaScopes.TEST);
foreach (var ikvmItem in ResolveIkvmReferenceItemsForScopes(output, maven, session, graph, privateScopes))
Expand Down Expand Up @@ -453,8 +453,11 @@ void CollectIkvmReferenceItems(Dictionary<string, IkvmReferenceItem> output, Dep
node = GetEffectiveNode(node);

// walk tree and ensure IkvmReferenceItem exists for each child
var scopes = new HashSet<string>() { JavaScopes.COMPILE, JavaScopes.RUNTIME, JavaScopes.PROVIDED };
if (IncludeTestScope)
scopes.Add(JavaScopes.TEST);
foreach (DependencyNode child in GetEffectiveChildren(node))
if (child.getDependency().getScope() is JavaScopes.COMPILE or JavaScopes.PROVIDED)
if (scopes.Contains(child.getDependency().getScope()))
CollectIkvmReferenceItems(output, child);

// if artifact, obtain IkvmReferenceItem from artifact
Expand All @@ -468,6 +471,36 @@ void CollectIkvmReferenceItems(Dictionary<string, IkvmReferenceItem> output, Dep
ikvmItem.References.Add(ikvmReference);
}

/// <summary>
/// Recursively populates the output collection with IkvmReferenceItems.
/// </summary>
/// <param name="output"></param>
/// <param name="node"></param>
/// <returns></returns>
IEnumerable<IkvmReferenceItem> CollectIkvmReferenceItemReferences(Dictionary<string, IkvmReferenceItem> output, DependencyNode node)
{
if (output is null)
throw new ArgumentNullException(nameof(output));
if (node is null)
throw new ArgumentNullException(nameof(node));

// resolve to winner of a conflict instead
node = GetEffectiveNode(node);

// each child of node
foreach (var child in GetEffectiveChildren(node))
{
// if the child node is a direct artifact
if (child.getArtifact() is Artifact artifact)
if (GetOrCreateIkvmReferenceItemForArtifact(output, artifact) is IkvmReferenceItem reference)
yield return reference;

// recurse into child
foreach (var reference in CollectIkvmReferenceItemReferences(output, child))
yield return reference;
}
}

/// <summary>
/// Gets the <see cref="IkvmReferenceItem"/> associated with the given artifact.
/// </summary>
Expand Down Expand Up @@ -561,36 +594,6 @@ IkvmReferenceItem GetIkvmReferenceItemForArtifact(IReadOnlyDictionary<string, Ik
return null;
}

/// <summary>
///
/// </summary>
/// <param name="output"></param>
/// <param name="node"></param>
/// <returns></returns>
IEnumerable<IkvmReferenceItem> CollectIkvmReferenceItemReferences(Dictionary<string, IkvmReferenceItem> output, DependencyNode node)
{
if (output is null)
throw new ArgumentNullException(nameof(output));
if (node is null)
throw new ArgumentNullException(nameof(node));

// resolve to winner of a conflict instead
node = GetEffectiveNode(node);

// each child of node
foreach (var child in GetEffectiveChildren(node))
{
// if the child node is a direct artifact
if (child.getArtifact() is Artifact artifact)
if (GetOrCreateIkvmReferenceItemForArtifact(output, artifact) is IkvmReferenceItem reference)
yield return reference;

// recurse into child
foreach (var reference in CollectIkvmReferenceItemReferences(output, child))
yield return reference;
}
}

/// <summary>
/// Gets the effective node.
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions src/IKVM.Maven.Sdk.Tests/ProjectTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,9 @@ public void CanBuildProject(EnvironmentPreference env, string tfm, string rid, s
File.Exists(Path.Combine(outDir, "maven.core.dll")).Should().BeTrue();
File.Exists(Path.Combine(outDir, "maven.model.dll")).Should().BeTrue();
File.Exists(Path.Combine(outDir, "org.apache.commons.io.dll")).Should().BeTrue();
File.Exists(Path.Combine(outDir, "org.apache.commons.logging.dll")).Should().BeTrue();
File.Exists(Path.Combine(outDir, "org.apache.commons.logging.dll")).Should().BeFalse();
File.Exists(Path.Combine(outDir, "org.slf4j.dll")).Should().BeTrue();
File.Exists(Path.Combine(outDir, "xml.apis.dll")).Should().BeTrue();
File.Exists(Path.Combine(outDir, "xml.apis.dll")).Should().BeFalse();

// ikvm libraries
File.Exists(Path.Combine(outDir, "IKVM.Runtime.dll")).Should().BeTrue();
Expand Down

0 comments on commit 719bb82

Please sign in to comment.