diff --git a/src/main/java/org/reficio/p2/P2Helper.java b/src/main/java/org/reficio/p2/P2Helper.java index bbd0f828..4e2dfb3c 100644 --- a/src/main/java/org/reficio/p2/P2Helper.java +++ b/src/main/java/org/reficio/p2/P2Helper.java @@ -25,15 +25,23 @@ import org.reficio.p2.bundler.ArtifactBundlerInstructions; import org.reficio.p2.bundler.ArtifactBundlerRequest; import org.reficio.p2.bundler.impl.AquteHelper; +import org.reficio.p2.logger.Logger; import org.reficio.p2.resolver.maven.Artifact; import org.reficio.p2.resolver.maven.ResolvedArtifact; import org.reficio.p2.utils.BundleUtils; import org.reficio.p2.utils.JarUtils; import org.reficio.p2.utils.Utils; +import org.reficio.p2.utils.XmlUtils; +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; import java.io.File; import java.io.IOException; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; + /** * Glues together the following independent modules that know nothing about the * plugin and the p2-maven-plugin model: @@ -200,11 +208,43 @@ private static String calculateSnapshotVersion(ResolvedArtifact resolvedArtifact return version; } } + // attempt to take the proper snapshot version from the artifact's baseVersion String baseVersion = resolvedArtifact.getArtifact().getBaseVersion(); if (isProperSnapshotVersion(baseVersion)) { return baseVersion; } + + // attempt to construct version from maven-metadata-local.xml + File mavenMetadataLocal = new File(resolvedArtifact.getArtifact().getFile().getParentFile(), "maven-metadata-local.xml"); + if (mavenMetadataLocal.exists()) { + try { + DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory + .newInstance(); + DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); + Document document = documentBuilder.parse(mavenMetadataLocal); + Node metadata = XmlUtils.getFirstChildNodeByName(document, "metadata"); + Node versioning = XmlUtils.getFirstChildNodeByName(metadata, "versioning"); + Node lastUpdated = XmlUtils.getFirstChildNodeByName(versioning, "lastUpdated"); + String version = BundleUtils.INSTANCE.calculateBundleVersion(resolvedArtifact.getArtifact()); + if (version.endsWith(Utils.JAR_SNAPSHOT_POSTFIX) || version.endsWith(Utils.OSGI_SNAPSHOT_POSTFIX)) { + version = version.substring(0, version.length() - Utils.JAR_SNAPSHOT_POSTFIX.length()); + String lastUpdatedTrimmed = lastUpdated.getTextContent().trim(); + if (lastUpdatedTrimmed.length() > 8) { + lastUpdatedTrimmed = lastUpdatedTrimmed.substring(0, 8) + "." + lastUpdatedTrimmed.substring(8); + version += "-" + lastUpdatedTrimmed; + version += "-1"; + } + } + if (isProperSnapshotVersion(version)) + return version; + } catch (Exception e) { + Logger.getLog().warn(String.format("Could not extract version from Maven local metadata file %s", mavenMetadataLocal.getAbsolutePath()), e); + } + + } + + // otherwise manually add the SNAPSHOT postfix that will be automatically changed to timestamp if (!baseVersion.contains("SNAPSHOT")) { baseVersion += ".SNAPSHOT"; @@ -213,7 +253,9 @@ private static String calculateSnapshotVersion(ResolvedArtifact resolvedArtifact } public static boolean isProperSnapshotVersion(String version) { - return version.matches(".*[0-9\\.]{13,16}-[0-9]{3}"); + //matches e.g. 3.3.1-20190722.014326-123 or + //1.0.1-20190721.031346-1 + return version.matches(".*[0-9\\.]{13,16}-[0-9]+"); } public static String calculateSourceSymbolicName(String symbolicName) { diff --git a/src/main/java/org/reficio/p2/P2Validator.java b/src/main/java/org/reficio/p2/P2Validator.java index f1a3eddc..4926384b 100644 --- a/src/main/java/org/reficio/p2/P2Validator.java +++ b/src/main/java/org/reficio/p2/P2Validator.java @@ -37,8 +37,7 @@ public static void validateBundleRequest(P2Artifact p2Artifact, ResolvedArtifact private static void validateGeneralConfig(P2Artifact p2Artifact) { if (p2Artifact.shouldIncludeTransitive() && !p2Artifact.getInstructions().isEmpty()) { - String message = "BND instructions are NOT applied to the transitive dependencies of "; - Logger.getLog().warn(String.format("%s %s", message, p2Artifact.getId())); + Logger.getLog().warn(String.format("BND instructions are NOT applied to the transitive dependencies of %s", p2Artifact.getId())); } } diff --git a/src/main/java/org/reficio/p2/utils/Utils.java b/src/main/java/org/reficio/p2/utils/Utils.java index 96c81a8f..474c6b7d 100644 --- a/src/main/java/org/reficio/p2/utils/Utils.java +++ b/src/main/java/org/reficio/p2/utils/Utils.java @@ -23,8 +23,8 @@ public class Utils { - private static final String JAR_SNAPSHOT_POSTFIX = "-SNAPSHOT"; - private static final String OSGI_SNAPSHOT_POSTFIX = ".SNAPSHOT"; + public static final String JAR_SNAPSHOT_POSTFIX = "-SNAPSHOT"; + public static final String OSGI_SNAPSHOT_POSTFIX = ".SNAPSHOT"; private static final String ECLIPSE_QUALIFIER_POSTFIX = ".qualifier"; public static final String TYCHO_VERSION = "1.0.0"; diff --git a/src/main/java/org/reficio/p2/utils/XmlUtils.java b/src/main/java/org/reficio/p2/utils/XmlUtils.java index f0f970f7..788e25d0 100644 --- a/src/main/java/org/reficio/p2/utils/XmlUtils.java +++ b/src/main/java/org/reficio/p2/utils/XmlUtils.java @@ -83,4 +83,20 @@ public static Element createElement(Document doc, Node parent, String tagName) { parent.appendChild(e); return e; } + + public static Node getFirstChildNodeByName(Node parent, String childNodeName) { + if (parent == null) + return null; + NodeList childNodes = parent.getChildNodes(); + for (int i = 0; i < childNodes.getLength(); i++) { + Node probeChild = childNodes.item(i); + if (probeChild == null) + continue; + + if (probeChild.getNodeName().equals(childNodeName)) + return probeChild; + } + return null; + } + }