Skip to content

Commit

Permalink
TargetPlatformFactoryImpl.gatherP2InfUnits should use artifact version
Browse files Browse the repository at this point in the history
Currently the implementation hard codes the version to 1.0.0 when
parsing advice from a p2.inf.  This can result in an IAE if the version
is used in a range such as [1.0.0,$version$) where because [1.0.0,1.0.0)
is not a valid range. Instead use the reactor project's actual version.
  • Loading branch information
merks committed Mar 31, 2024
1 parent b5669da commit f14cee8
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.maven.execution.MavenSession;
import org.apache.maven.project.MavenProject;
Expand Down Expand Up @@ -77,6 +79,7 @@
import org.eclipse.tycho.ReactorProjectIdentities;
import org.eclipse.tycho.ResolvedArtifactKey;
import org.eclipse.tycho.TargetPlatform;
import org.eclipse.tycho.TychoConstants;
import org.eclipse.tycho.core.TychoProject;
import org.eclipse.tycho.core.TychoProjectManager;
import org.eclipse.tycho.core.ee.impl.ExecutionEnvironmentResolutionHandler;
Expand Down Expand Up @@ -122,6 +125,11 @@

public class TargetPlatformFactoryImpl implements TargetPlatformFactory {

private static final Version DEFAULT_P2_ADVICE_VERSION = Version.parseVersion("1.0.0.qualifier");

private static final Pattern REACTOR_PROJECT_VERSION_PATTERN = Pattern
.compile("((?:[0-9]+)(?:\\.(?:[0-9]+)(?:\\.(?:[0-9]+))?)?)?([.-][A-Za-z0-9_-]+)?");

private final MavenContext mavenContext;
private final MavenLogger logger;
private final IProgressMonitor monitor;
Expand Down Expand Up @@ -304,12 +312,13 @@ private void gatherP2InfUnits(ReactorProject reactorProject, Set<IInstallableUni
if (reactorProject == null) {
return;
}

AdviceFileAdvice advice;
if (PackagingType.TYPE_ECLIPSE_PLUGIN.equals(reactorProject.getPackaging())) {
advice = new AdviceFileAdvice(reactorProject.getArtifactId(), Version.parseVersion("1.0.0"),
advice = new AdviceFileAdvice(reactorProject.getArtifactId(), getVersion(reactorProject),
new Path(reactorProject.getBasedir().getAbsolutePath()), AdviceFileAdvice.BUNDLE_ADVICE_FILE);
} else if (PackagingType.TYPE_ECLIPSE_FEATURE.equals(reactorProject.getPackaging())) {
advice = new AdviceFileAdvice(reactorProject.getArtifactId(), Version.parseVersion("1.0.0"),
advice = new AdviceFileAdvice(reactorProject.getArtifactId(), getVersion(reactorProject),
new Path(reactorProject.getBasedir().getAbsolutePath()), new Path("p2.inf"));
} else {
//not a project with advice...
Expand All @@ -325,6 +334,31 @@ private void gatherP2InfUnits(ReactorProject reactorProject, Set<IInstallableUni
}
}

private static Version getVersion(ReactorProject reactorProject) {
String version = reactorProject.getVersion();
if (version == null) {
return DEFAULT_P2_ADVICE_VERSION;
}
Matcher matcher = REACTOR_PROJECT_VERSION_PATTERN.matcher(version);
if (!matcher.matches()) {
return DEFAULT_P2_ADVICE_VERSION;
}
String qualifier = matcher.group(2);
if (qualifier != null && qualifier.startsWith("-")) {
if (TychoConstants.SUFFIX_SNAPSHOT.equals(qualifier)) {
version = version.substring(0, version.length() - TychoConstants.SUFFIX_SNAPSHOT.length())
+ TychoConstants.SUFFIX_QUALIFIER;
} else {
version = matcher.group(1) + '.' + qualifier.substring(1);
}
}
try {
return Version.parseVersion(version);
} catch (IllegalAccessError ex) {
return DEFAULT_P2_ADVICE_VERSION;
}
}

private List<MavenArtifactKey> getMissingJunitBundles(ReactorProject project, Set<IInstallableUnit> externalUIs) {
List<MavenArtifactKey> missing = new ArrayList<>();
if (projectManager != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: pvu.bundle
Bundle-SymbolicName: pvu.bundle;singleton:=true
Bundle-Version: 1.0.0.qualifier
Bundle-Version: 2.0.0.qualifier
Bundle-Vendor: TEST
Bundle-RequiredExecutionEnvironment: JavaSE-17
12 changes: 9 additions & 3 deletions tycho-its/projects/p2Inf.virtualUnit/bundle/META-INF/p2.inf
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
# Create the virtual IU
units.0.id=configure.pvu.bundle
units.0.version=1.0.0
units.0.version=$version$
units.0.provides.1.namespace=org.eclipse.equinox.p2.iu
units.0.provides.1.name=configure.pvu.bundle
units.0.provides.1.version=1.0.0
units.0.provides.1.version=$version$

# Require in this bundle the created virtual IU
requires.0.namespace=org.eclipse.equinox.p2.iu
requires.0.name=configure.pvu.bundle
requires.0.range=0.0.0
requires.0.range=$version$

# Specify update advice with a version range that is invalid if $version$ expands to 1.0.0
# For this example it should expand to 2.0.0
update.id = update.pvu.bundle
update.range = [1.0.0,$version$)
update.severity = 0
2 changes: 1 addition & 1 deletion tycho-its/projects/p2Inf.virtualUnit/bundle/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>tycho-its-project.p2Inf.virtualUnit</groupId>
<artifactId>parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<version>2.0.0-SNAPSHOT</version>
</parent>

<artifactId>pvu.bundle</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion tycho-its/projects/p2Inf.virtualUnit/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>tycho-its-project.p2Inf.virtualUnit</groupId>
<artifactId>parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<version>2.0.0-SNAPSHOT</version>
<packaging>pom</packaging>

<properties>
Expand Down

0 comments on commit f14cee8

Please sign in to comment.