ciFriendlyProperties = pom.getCiFriendlyVersionPropertyNames();
+ if (!ciFriendlyProperties.isEmpty()) {
if (ciFriendlyProperties.size() == 1) {
//thats actually a simply property change
applyPropertyChange(pomName, pom, ciFriendlyProperties.get(0), newVersion);
diff --git a/tycho-versions-plugin/src/main/java/org/eclipse/tycho/versions/pom/PomFile.java b/tycho-versions-plugin/src/main/java/org/eclipse/tycho/versions/pom/PomFile.java
index 4552718a10..f8b0ced8c7 100644
--- a/tycho-versions-plugin/src/main/java/org/eclipse/tycho/versions/pom/PomFile.java
+++ b/tycho-versions-plugin/src/main/java/org/eclipse/tycho/versions/pom/PomFile.java
@@ -10,7 +10,8 @@
* Contributors:
* Sonatype Inc. - initial API and implementation
* Bachmann electronic GmbH. - #472579 - Support setting the version for pomless builds
- * Christoph Läubrich - Bug 550313 - tycho-versions-plugin uses hard-coded polyglot file
+ * Christoph Läubrich - Bug 550313 - tycho-versions-plugin uses hard-coded polyglot file
+ * SAP SE - #3744 - ci-friendly version support
*******************************************************************************/
package org.eclipse.tycho.versions.pom;
@@ -28,6 +29,10 @@
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import java.util.stream.Collectors;
import de.pdark.decentxml.Document;
import de.pdark.decentxml.Element;
@@ -43,13 +48,17 @@ public class PomFile {
public static final String POM_XML = "pom.xml";
private static final String DEFAULT_XML_ENCODING = "UTF-8";
+ private static final Pattern CI_FRIENDLY_EXPRESSION = Pattern.compile("\\$\\{(.+?)\\}");
+
private static XMLParser parser = new XMLParser();
private Document document;
private Element project;
- /** The (effective) project version */
+ /** The (raw) project version */
private String version;
+ /** The ${property}-resolved version, in case of ci-friendly versions */
+ private String resolvedVersion;
private final boolean preferExplicitProjectVersion;
private final boolean isMutable;
@@ -142,7 +151,7 @@ private static void removeVersionElementFromXML(Element project) {
/**
* Sets the version in the parent POM declaration. This never affects the (effective) version of
* the project itself.
- *
+ *
* @see #setVersion(String)
*/
public void setParentVersion(String newVersion) {
@@ -158,13 +167,35 @@ public void setParentVersion(String newVersion) {
*/
public void setVersion(String version) {
this.version = version;
+ this.resolvedVersion = null;
}
/**
* Returns the (effective) version of the project.
*/
public String getVersion() {
- return version;
+ if (this.resolvedVersion == null) {
+ resolveCiFriendlyVersion();
+ }
+ return this.resolvedVersion;
+ }
+
+ /**
+ * Returns the list of property names that make up the <version>
of the
+ * project.
+ *
+ * If this list is non-empty, the project uses ci-friendly versions.
+ */
+ public List getCiFriendlyVersionPropertyNames() {
+ if (isCiFriendly(version)) {
+ Matcher m = CI_FRIENDLY_EXPRESSION.matcher(version.trim());
+ List ciFriendlyProperties = new ArrayList();
+ while (m.find()) {
+ ciFriendlyProperties.add(m.group(1));
+ }
+ return ciFriendlyProperties;
+ }
+ return List.of();
}
public String getPackaging() {
@@ -245,4 +276,25 @@ private String getElementValue(String name) {
public boolean isMutable() {
return isMutable;
}
+
+ private static boolean isCiFriendly(String v) {
+ return v != null && v.contains("${");
+ }
+
+ private void resolveCiFriendlyVersion() {
+ if (isCiFriendly(version)) {
+ Map properties = getProperties().stream()
+ .collect(Collectors.toMap(p -> p.getName(), p -> p.getValue()));
+ StringBuilder resolvedVersionBuilder = new StringBuilder();
+ Matcher m = CI_FRIENDLY_EXPRESSION.matcher(version.trim());
+ while (m.find()) {
+ m.appendReplacement(resolvedVersionBuilder, properties.get(m.group(1)));
+ }
+ m.appendTail(resolvedVersionBuilder);
+ resolvedVersion = resolvedVersionBuilder.toString();
+ } else {
+ resolvedVersion = version;
+ }
+ }
+
}