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

#97 Use the artifact lastModified or sysprop to generate qualifier #98

Open
wants to merge 1 commit 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
6 changes: 4 additions & 2 deletions src/main/java/org/reficio/p2/P2Helper.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ private static String getUserDefinedVersion(P2Artifact p2Artifact, ResolvedArtif
// in case of root artifact try to take the version from the instructions
if (resolvedArtifact.isRoot()) {
Object versionValue = p2Artifact.getInstructions().get(Analyzer.BUNDLE_VERSION);
version = versionValue != null ? JarUtils.replaceSnapshotWithTimestamp(versionValue.toString()) : null;
long artifactLastModified = resolvedArtifact.getArtifact().getFile().lastModified();
version = versionValue != null ? JarUtils.replaceSnapshotWithTimestamp(versionValue.toString(), artifactLastModified) : null;
}
// if contains snapshot (manually set by the user) -> "SNAPSHOT" will be manually replaced
return version;
Expand All @@ -177,7 +178,8 @@ private static String calculateProposedVersion(ResolvedArtifact resolvedArtifact
}
}
// if still contains snapshot (manually set by the user) -> "SNAPSHOT" will be manually replaced
return BundleUtils.INSTANCE.cleanupVersion(JarUtils.replaceSnapshotWithTimestamp(version));
long artifactLastModified = resolvedArtifact.getArtifact().getFile().lastModified();
return BundleUtils.INSTANCE.cleanupVersion(JarUtils.replaceSnapshotWithTimestamp(version, artifactLastModified));
}

private static String calculateSnapshotVersion(ResolvedArtifact resolvedArtifact) {
Expand Down
24 changes: 15 additions & 9 deletions src/main/java/org/reficio/p2/utils/JarUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.TimeZone;
import java.util.jar.Attributes;
import java.util.jar.Manifest;
import java.util.zip.ZipEntry;
Expand All @@ -58,6 +59,7 @@ public class JarUtils {
private static final String JAR_SNAPSHOT_POSTFIX = "-SNAPSHOT";
private static final String OSGI_SNAPSHOT_POSTFIX = ".SNAPSHOT";
private static final String ECLIPSE_QUALIFIER_POSTFIX = ".qualifier";
private static final String FORCED_CONTEXT_QUALIFIER = System.getProperty("forceContextQualifier");

public static void adjustSnapshotOutputVersion(File inputFile, File outputFile, String version) {
Jar jar = null;
Expand All @@ -83,7 +85,7 @@ public static void adjustFeatureQualifierVersionWithTimestamp(File inputFile, Fi
Resource res = jar.getResource("feature.xml");
Document featureSpec = parseXml(res.openInputStream());
String version = featureSpec.getDocumentElement().getAttributeNode("version").getValue();
String newVersion = replaceQualifierWithTimestamp(version);
String newVersion = replaceQualifierWithTimestamp(version, inputFile.lastModified());
featureSpec.getDocumentElement().getAttributeNode("version").setValue(newVersion);
File newXml = new File(inputFile.getParentFile(), "feature.xml");
writeXml(featureSpec, newXml);
Expand Down Expand Up @@ -122,27 +124,31 @@ public static void writeXml(Document doc, File outputFile) {
}
}

public static String replaceQualifierWithTimestamp(String version) {
public static String replaceQualifierWithTimestamp(String version, long artifactLastModified) {
String tweakedVersion = version;
if (version.contains(ECLIPSE_QUALIFIER_POSTFIX)) {
tweakedVersion = tweakedVersion.replace(ECLIPSE_QUALIFIER_POSTFIX, "." + getTimeStamp());
tweakedVersion = tweakedVersion.replace(ECLIPSE_QUALIFIER_POSTFIX, "." + getTimeStamp(artifactLastModified));
}
return tweakedVersion;
}

public static String replaceSnapshotWithTimestamp(String version) {
public static String replaceSnapshotWithTimestamp(String version, long artifactLastModified) {
String tweakedVersion = version;
if (version.contains(JAR_SNAPSHOT_POSTFIX)) {
tweakedVersion = tweakedVersion.replace(JAR_SNAPSHOT_POSTFIX, "-" + getTimeStamp());
tweakedVersion = tweakedVersion.replace(JAR_SNAPSHOT_POSTFIX, "-" + getTimeStamp(artifactLastModified));
} else if (version.contains(OSGI_SNAPSHOT_POSTFIX)) {
tweakedVersion = tweakedVersion.replace(OSGI_SNAPSHOT_POSTFIX, "." + getTimeStamp());
tweakedVersion = tweakedVersion.replace(OSGI_SNAPSHOT_POSTFIX, "." + getTimeStamp(artifactLastModified));
}
return tweakedVersion;
}

public static String getTimeStamp() {
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
return format.format(new Date());
public static String getTimeStamp(long artifactLastModified) {
if (FORCED_CONTEXT_QUALIFIER != null) {
return FORCED_CONTEXT_QUALIFIER;
}
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmm");
format.setTimeZone(TimeZone.getTimeZone("UTC"));
return format.format(new Date(artifactLastModified));
}

public static void removeSignature(File jar) {
Expand Down