Skip to content

Commit

Permalink
Only use ci-friendly version if there is such property defined
Browse files Browse the repository at this point in the history
Currently it can happen that one uses one of the "trigger properties"
that enable Tycho pomless feature to assume that a cifriendly version is
to be used even though ci-friendly versions are actually not used at
all.

This tries to differentiate the two cases by checking if there is
actually a property defined that could be used in a ci-friendly way.
  • Loading branch information
laeubi committed Jan 5, 2024
1 parent 64b3ccf commit 2b9de3a
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public Result<ProjectDependencyGraph> build(MavenSession session) {
if (properties.getProperty(TychoCiFriendlyVersions.PROPERTY_BUILDQUALIFIER_FORMAT) != null
|| properties.getProperty(TychoCiFriendlyVersions.PROPERTY_FORCE_QUALIFIER) != null
|| properties.getProperty(TychoCiFriendlyVersions.BUILD_QUALIFIER) != null) {
tychoMapping.setSnapshotFormat("${" + TychoCiFriendlyVersions.BUILD_QUALIFIER + "}");
tychoMapping.setSnapshotProperty(TychoCiFriendlyVersions.BUILD_QUALIFIER);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ public abstract class AbstractTychoMapping implements Mapping, ModelReader {

private static final String PARENT_POM_DEFAULT_VALUE = System.getProperty(TYCHO_POMLESS_PARENT_PROPERTY, "..");
private static final String QUALIFIER_SUFFIX = ".qualifier";
private static final String MODEL_PARENT = "TychoMapping.model.parent";

private Map<Path, ParentModel> parentModelCache = new HashMap<Path, ParentModel>();

@Requirement
protected PlexusContainer container;
Expand All @@ -79,7 +80,7 @@ public abstract class AbstractTychoMapping implements Mapping, ModelReader {
private boolean extensionMode;
@SuppressWarnings("unused")
private File multiModuleProjectDirectory;
private String snapshotFormat;
private String snapshotProperty;

@Override
public File locatePom(File dir) {
Expand Down Expand Up @@ -155,7 +156,7 @@ private Model read(Reader artifactReader, Path artifactFile, Map<String, ?> opti
model.setPackaging(getPackaging());
initModel(model, artifactReader, artifactFile);
if (model.getParent() == null) {
model.setParent(findParent(artifactFile.getParent(), options));
model.setParent(findParent(artifactFile.getParent(), options).parentReference());
}
if (model.getVersion() == null && model.getParent() != null) {
//inherit version from parent if not given
Expand All @@ -172,11 +173,10 @@ protected Path getRealArtifactFile(Path polyglotArtifactFile) {
return polyglotArtifactFile;
}

protected Parent findParent(Path projectRoot, Map<String, ?> projectOptions) throws IOException {
Parent parent = (Parent) projectOptions.get(MODEL_PARENT);
if (parent != null) {
//if the parent is given by the options we don't need to search it!
return parent;
protected synchronized ParentModel findParent(Path projectRoot, Map<String, ?> projectOptions) throws IOException {
ParentModel cached = parentModelCache.get(projectRoot);
if (cached != null) {
return cached;
}
Properties buildProperties = getBuildProperties(projectRoot);
// assumption parent pom must be physically located in parent directory if not given by build.properties
Expand All @@ -198,24 +198,27 @@ protected Parent findParent(Path projectRoot, Map<String, ?> projectOptions) thr
Model parentModel = parentPom.getReader().read(parentPom.getPomFile(), options);
Parent parentReference = new Parent();
String groupId = parentModel.getGroupId();
if (groupId == null) {
Parent grandParent = parentModel.getParent();
if (groupId == null && grandParent != null) {
// must be inherited from grandparent
groupId = parentModel.getParent().getGroupId();
groupId = grandParent.getGroupId();
}
parentReference.setGroupId(groupId);
parentReference.setArtifactId(parentModel.getArtifactId());
String version = parentModel.getVersion();
if (version == null) {
if (version == null && grandParent != null) {
// must be inherited from grandparent
version = parentModel.getParent().getVersion();
version = grandParent.getVersion();
}
parentReference.setVersion(version);
parentReference
.setRelativePath(projectRoot.toRealPath().relativize(parentPom.getPomFile().toPath()).toString());
logger.debug("Derived parent for path " + projectRoot + " is groupId: " + parentReference.getGroupId()
+ ", artifactId: " + parentReference.getArtifactId() + ", relativePath: "
+ parentReference.getRelativePath());
return parentReference;
ParentModel model = new ParentModel(parentReference, parentModel);
parentModelCache.put(projectRoot, model);
return model;
}

/**
Expand Down Expand Up @@ -342,18 +345,41 @@ private static void setLocation(Model model, Path modelSource) {
model.setLocation("", new InputLocation(0, 0, inputSource));
}

protected String getPomVersion(String pdeVersion) {
protected String getPomVersion(String pdeVersion, Model model, Path projectRoot) {
String pomVersion = pdeVersion;
if (pdeVersion.endsWith(QUALIFIER_SUFFIX)) {
String unqualifiedVersion = pdeVersion.substring(0, pdeVersion.length() - QUALIFIER_SUFFIX.length());
if (isExtensionMode() && snapshotFormat != null) {
return unqualifiedVersion + snapshotFormat;
//we need to check that this property is actually defined!
if (isExtensionMode() && modelHasProperty(snapshotProperty, model, projectRoot)) {
return unqualifiedVersion + "${" + snapshotProperty + "}";
}
return unqualifiedVersion + "-SNAPSHOT";
}
return pomVersion;
}

private boolean modelHasProperty(String property, Model model, Path projectRoot) {
if (property == null) {
//nothing we can check asume it is NOT present...
return false;
}
Properties properties = model.getProperties();
String string = properties.getProperty(property);
if (string != null) {
return true;
}
if (model.getParent() == null) {
try {
ParentModel findParent = findParent(projectRoot.getParent(), Map.of());
return modelHasProperty(property, findParent.parentModel(),
projectRoot.resolve(findParent.parentReference().getRelativePath()));
} catch (IOException e) {
return false;
}
}
return false;
}

public boolean isExtensionMode() {
return extensionMode;
}
Expand All @@ -367,8 +393,8 @@ public void setMultiModuleProjectDirectory(File multiModuleProjectDirectory) {
this.multiModuleProjectDirectory = multiModuleProjectDirectory;
}

public void setSnapshotFormat(String snapshotFormat) {
this.snapshotFormat = snapshotFormat;
public void setSnapshotProperty(String snapshotFormat) {
this.snapshotProperty = snapshotFormat;
}

static Optional<Path> getLocation(Map<String, ?> options) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*******************************************************************************
* Copyright (c) 2024 Christoph Läubrich and others.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Christoph Läubrich - initial API and implementation
*******************************************************************************/
package org.eclipse.tycho.pomless;

import org.apache.maven.model.Model;
import org.apache.maven.model.Parent;

public record ParentModel(Parent parentReference, Model parentModel) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ protected void initModel(Model model, Reader artifactReader, Path artifactFile)
// groupId is inherited from parent pom
model.setArtifactId(bundleSymbolicName);
String bundleVersion = getRequiredHeaderValue("Bundle-Version", manifestHeaders, manifestFile);
model.setVersion(getPomVersion(bundleVersion));
model.setVersion(getPomVersion(bundleVersion, model, artifactFile));
String prefix;
if (isTestBundle(bundleSymbolicName, bundleRoot)) {
model.setPackaging(PACKAGING_TEST);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public float getPriority() {
@Override
protected void initModelFromXML(Model model, Element xml, Path artifactFile) throws IOException {
model.setArtifactId(getRequiredXMLAttributeValue(xml, "id"));
model.setVersion(getPomVersion(getRequiredXMLAttributeValue(xml, "version")));
model.setVersion(getPomVersion(getRequiredXMLAttributeValue(xml, "version"), model, artifactFile));

Path featureProperties = artifactFile.getParent().resolve("feature.properties");
Supplier<Properties> properties = getPropertiesSupplier(featureProperties);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ protected void initModelFromXML(Model model, Element xml, Path artifactFile) thr
model.setArtifactId(getRequiredXMLAttributeValue(xml, PRODUCT_UID_ATTRIBUTE));
String version = getXMLAttributeValue(xml, PRODUCT_VERSION_ATTRIBUTE);
if (version != null) {
model.setVersion(getPomVersion(version));
model.setVersion(getPomVersion(version, model, artifactFile));
}
String name = getXMLAttributeValue(xml, PRODUCT_NAME_ATTRIBUTE);
model.setName(PRODUCT_NAME_PREFIX + (name != null ? name : model.getArtifactId()));
Expand Down

0 comments on commit 2b9de3a

Please sign in to comment.