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

Allow the snapshot version to be retrieved from maven-metadata-local.xml #142

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
44 changes: 43 additions & 1 deletion src/main/java/org/reficio/p2/P2Helper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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";
Expand All @@ -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) {
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/org/reficio/p2/P2Validator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/reficio/p2/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
16 changes: 16 additions & 0 deletions src/main/java/org/reficio/p2/utils/XmlUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}