Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[tycho-4.0.x] Only use ci-friendly version if there is such property defined #3323

Merged
merged 1 commit into from
Jan 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -23,11 +23,11 @@
import java.util.stream.Stream;

import org.apache.maven.model.Model;
import org.apache.maven.model.Parent;
import org.codehaus.plexus.component.annotations.Component;
import org.eclipse.tycho.TychoConstants;
import org.eclipse.tycho.pomless.AbstractTychoMapping;
import org.eclipse.tycho.pomless.NoParentPomFound;
import org.eclipse.tycho.pomless.ParentModel;
import org.sonatype.maven.polyglot.mapping.Mapping;

import aQute.bnd.build.Project;
Expand Down Expand Up @@ -123,12 +123,12 @@ protected void initModel(Model model, Reader artifactReader, Path cnfFolder) thr
}

@Override
protected Parent findParent(Path projectRoot, Map<String, ?> projectOptions) throws IOException {
protected ParentModel findParent(Path projectRoot, Map<String, ?> projectOptions) throws IOException {
try {
return super.findParent(projectRoot, projectOptions);
} catch (NoParentPomFound e) {
// this can happen in 100% pomless mode!
return null;
return new ParentModel(null, null);
}
}
}
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,42 @@ 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 assume it is NOT present...
return false;
}
Properties properties = model.getProperties();
String string = properties.getProperty(property);
if (string != null) {
return true;
}
try {
ParentModel parent = findParent(projectRoot.getParent(), Map.of());
Model parentModel = parent.parentModel();
if (parentModel != null) {
return modelHasProperty(property, parentModel,
projectRoot.resolve(parent.parentReference().getRelativePath()));
}
} catch (IOException e) {
//in this case we can't find the parent or there is no more parent...
}
return false;
}

public boolean isExtensionMode() {
return extensionMode;
}
Expand All @@ -367,8 +394,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