From eefd51e1f104710cfb4a6863e0956e5b43f013bb Mon Sep 17 00:00:00 2001 From: Hannes Wellmann Date: Wed, 16 Oct 2024 21:11:57 +0200 Subject: [PATCH] Support version-ranges and no-version for units in IU target locations Relates to https://github.com/eclipse-pde/eclipse.pde/pull/1245 --- .../tycho/p2resolver/InstallableUnitResolver.java | 6 +++--- .../p2resolver/TargetPlatformFactoryTest.java | 14 +++++++++++++- .../resources/targetresolver/versionRanges.target | 11 +++++++++++ .../tycho/targetplatform/TargetDefinitionFile.java | 3 +++ 4 files changed, 30 insertions(+), 4 deletions(-) create mode 100644 tycho-core/src/test/resources/targetresolver/versionRanges.target diff --git a/tycho-core/src/main/java/org/eclipse/tycho/p2resolver/InstallableUnitResolver.java b/tycho-core/src/main/java/org/eclipse/tycho/p2resolver/InstallableUnitResolver.java index 6edba2fa89..ebf25dac8c 100644 --- a/tycho-core/src/main/java/org/eclipse/tycho/p2resolver/InstallableUnitResolver.java +++ b/tycho-core/src/main/java/org/eclipse/tycho/p2resolver/InstallableUnitResolver.java @@ -275,7 +275,7 @@ private static IInstallableUnit findUnits(Unit unitReference, IQueryable findUnit(Unit unitReference, IQueryable units) throws TargetDefinitionSyntaxException { - Version version = parseVersion(unitReference); + VersionRange version = parseVersion(unitReference); // the createIUQuery treats 0.0.0 version as "any version", and all other versions as exact versions IQuery matchingIUQuery = QueryUtil.createIUQuery(unitReference.getId(), version); @@ -285,9 +285,9 @@ private static IQueryResult findUnit(Unit unitReference, IQuer return queryResult; } - private static Version parseVersion(Unit unitReference) throws TargetDefinitionSyntaxException { + private static VersionRange parseVersion(Unit unitReference) throws TargetDefinitionSyntaxException { try { - return Version.parseVersion(unitReference.getVersion()); + return VersionRange.create(unitReference.getVersion()); } catch (IllegalArgumentException e) { throw new TargetDefinitionSyntaxException(NLS.bind("Cannot parse version \"{0}\" of unit \"{1}\"", unitReference.getVersion(), unitReference.getId()), e); diff --git a/tycho-core/src/test/java/org/eclipse/tycho/p2resolver/TargetPlatformFactoryTest.java b/tycho-core/src/test/java/org/eclipse/tycho/p2resolver/TargetPlatformFactoryTest.java index 1fa142563c..9228f24ba1 100644 --- a/tycho-core/src/test/java/org/eclipse/tycho/p2resolver/TargetPlatformFactoryTest.java +++ b/tycho-core/src/test/java/org/eclipse/tycho/p2resolver/TargetPlatformFactoryTest.java @@ -42,6 +42,7 @@ import org.eclipse.equinox.p2.core.IProvisioningAgent; import org.eclipse.equinox.p2.metadata.IInstallableUnit; import org.eclipse.equinox.p2.metadata.IVersionedId; +import org.eclipse.equinox.p2.metadata.VersionedId; import org.eclipse.tycho.ArtifactType; import org.eclipse.tycho.DefaultArtifactKey; import org.eclipse.tycho.IDependencyMetadata.DependencyMetadataType; @@ -298,7 +299,7 @@ public void testDuplicateReactorUnits() throws Exception { @Ignore("This test don't work because maven provides a 'not real' local repo to the test") public void testMavenArtifactsInTargetDefinitionResolveToMavenPath() throws Exception { File targetDefinition = resourceFile("targetresolver/mavenDep.target"); - tpConfig.getTargetDefinitions().add(TargetDefinitionFile.read(targetDefinition)); + tpConfig.addTargetDefinition(TargetDefinitionFile.read(targetDefinition)); P2TargetPlatform targetPlatform = subject.createTargetPlatform(tpConfig, NOOP_EE_RESOLUTION_HANDLER, List.of()); File artifactLocation = targetPlatform.getArtifactLocation( new DefaultArtifactKey(ArtifactType.TYPE_ECLIPSE_PLUGIN, "org.apache.commons.logging", "1.2.0")); @@ -307,6 +308,17 @@ public void testMavenArtifactsInTargetDefinitionResolveToMavenPath() throws Exce assertTrue(p2ArtifactPath.startsWith(localM2Repo)); } + @Test + public void testUnitsWithVersionRangeAndNoVersionInTargetDefinition() throws Exception { + File targetDefinition = resourceFile("targetresolver/versionRanges.target"); + tpConfig.addTargetDefinition(TargetDefinitionFile.read(targetDefinition)); + P2TargetPlatform tp = subject.createTargetPlatform(tpConfig, NOOP_EE_RESOLUTION_HANDLER, List.of()); + Set ius = tp.getInstallableUnits(); + assertThat(ius, hasItem(unitWithIdAndVersion(new VersionedId("jakarta.activation-api", "2.1.3")))); + assertThat(tp.getInstallableUnits(), + hasItem(unitWithIdAndVersion(new VersionedId("jakarta.inject.jakarta.inject-api", "1.0.5")))); + } + private static TargetDefinition plannerTargetDefinition(TestRepositories repository, IVersionedId unit) { TargetDefinition.Location location = new TargetDefinitionResolverIncludeModeTest.PlannerLocationStub(repository, unit); diff --git a/tycho-core/src/test/resources/targetresolver/versionRanges.target b/tycho-core/src/test/resources/targetresolver/versionRanges.target new file mode 100644 index 0000000000..2e50d0f9a6 --- /dev/null +++ b/tycho-core/src/test/resources/targetresolver/versionRanges.target @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/tycho-targetplatform/src/main/java/org/eclipse/tycho/targetplatform/TargetDefinitionFile.java b/tycho-targetplatform/src/main/java/org/eclipse/tycho/targetplatform/TargetDefinitionFile.java index e4bd84a9db..42fa355732 100644 --- a/tycho-targetplatform/src/main/java/org/eclipse/tycho/targetplatform/TargetDefinitionFile.java +++ b/tycho-targetplatform/src/main/java/org/eclipse/tycho/targetplatform/TargetDefinitionFile.java @@ -755,6 +755,9 @@ private static IULocation parseIULocation(Element dom) { for (Element unitDom : getChildren(dom, "unit")) { String id = unitDom.getAttribute("id"); String version = unitDom.getAttribute("version"); + if (version == null || version.isBlank()) { + version = "0.0.0"; + } units.add(new Unit(id, version)); } final List repositories = new ArrayList<>();