aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGunnar Wagenknecht2015-11-13 04:22:22 (EST)
committerGunnar Wagenknecht2015-11-13 04:23:57 (EST)
commit4959b87a7add9188856136ef910f919bd7d2e2e2 (patch)
tree81d06c7fa38e65d994b445d53e57541cf870a622
parentc36efb6298487077ae4d567e4fcaee29f0a4125f (diff)
downloadorg.eclipse.tycho-4959b87a7add9188856136ef910f919bd7d2e2e2.zip
org.eclipse.tycho-4959b87a7add9188856136ef910f919bd7d2e2e2.tar.gz
org.eclipse.tycho-4959b87a7add9188856136ef910f919bd7d2e2e2.tar.xz
Bug 482086: Support common variations when parsing timestampsrefs/changes/97/60297/1
Change-Id: I49206688069946fb2309cbd2ef3202ee4190f949 Signed-off-by: Gunnar Wagenknecht <gunnar@wagenknecht.org>
-rw-r--r--tycho-packaging-plugin/src/main/java/org/eclipse/tycho/buildversion/BuildQualifierAggregatorMojo.java29
-rw-r--r--tycho-packaging-plugin/src/main/java/org/eclipse/tycho/buildversion/TimestampFinder.java69
-rw-r--r--tycho-packaging-plugin/src/test/java/org/eclipse/tycho/buildversion/TimestampFinderTest.java47
3 files changed, 139 insertions, 6 deletions
diff --git a/tycho-packaging-plugin/src/main/java/org/eclipse/tycho/buildversion/BuildQualifierAggregatorMojo.java b/tycho-packaging-plugin/src/main/java/org/eclipse/tycho/buildversion/BuildQualifierAggregatorMojo.java
index cddc1bd..a578a89 100644
--- a/tycho-packaging-plugin/src/main/java/org/eclipse/tycho/buildversion/BuildQualifierAggregatorMojo.java
+++ b/tycho-packaging-plugin/src/main/java/org/eclipse/tycho/buildversion/BuildQualifierAggregatorMojo.java
@@ -14,9 +14,6 @@ import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
-import org.apache.maven.plugin.MojoExecutionException;
-import org.apache.maven.plugins.annotations.LifecyclePhase;
-import org.apache.maven.plugins.annotations.Mojo;
import org.eclipse.tycho.ArtifactDescriptor;
import org.eclipse.tycho.ReactorProject;
import org.eclipse.tycho.core.ArtifactDependencyVisitor;
@@ -24,8 +21,13 @@ import org.eclipse.tycho.core.FeatureDescription;
import org.eclipse.tycho.core.PluginDescription;
import org.eclipse.tycho.core.TychoProject;
import org.eclipse.tycho.core.osgitools.DefaultReactorProject;
+
import org.osgi.framework.Version;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugins.annotations.LifecyclePhase;
+import org.apache.maven.plugins.annotations.Mojo;
+
/**
* <p>
* This mojo calculates build timestamp as the latest timestamp of the project itself and timestamps
@@ -50,6 +52,9 @@ import org.osgi.framework.Version;
*/
@Mojo(name = "build-qualifier-aggregator", defaultPhase = LifecyclePhase.VALIDATE)
public class BuildQualifierAggregatorMojo extends BuildQualifierMojo {
+
+ private final TimestampFinder timestampFinder = new TimestampFinder();
+
@Override
protected Date getBuildTimestamp() throws MojoExecutionException {
Date timestamp = super.getBuildTimestamp();
@@ -91,14 +96,18 @@ public class BuildQualifierAggregatorMojo extends BuildQualifierMojo {
private void visitArtifact(ArtifactDescriptor artifact) {
ReactorProject otherProject = artifact.getMavenProject();
- String otherVersion = (otherProject != null) ? otherProject.getExpandedVersion() : artifact.getKey()
- .getVersion();
+ String otherVersion = (otherProject != null) ? otherProject.getExpandedVersion()
+ : artifact.getKey().getVersion();
Version v = Version.parseVersion(otherVersion);
String otherQualifier = v.getQualifier();
if (otherQualifier != null) {
Date timestamp = parseQualifier(otherQualifier);
if (timestamp != null) {
if (latestTimestamp[0].compareTo(timestamp) < 0) {
+ if (getLog().isDebugEnabled()) {
+ getLog().debug("Found '" + format.format(timestamp) + "' from qualifier '"
+ + otherQualifier + "' for artifact " + artifact);
+ }
latestTimestamp[0] = timestamp;
}
} else {
@@ -108,7 +117,11 @@ public class BuildQualifierAggregatorMojo extends BuildQualifierMojo {
}
private Date parseQualifier(String qualifier) {
- return parseQualifier(qualifier, format);
+ Date timestamp = parseQualifier(qualifier, format);
+ if (timestamp != null) {
+ return timestamp;
+ }
+ return discoverTimestamp(qualifier);
}
private Date parseQualifier(String qualifier, SimpleDateFormat format) {
@@ -119,6 +132,10 @@ public class BuildQualifierAggregatorMojo extends BuildQualifierMojo {
}
return null;
}
+
+ private Date discoverTimestamp(String qualifier) {
+ return timestampFinder.findInString(qualifier);
+ }
});
return latestTimestamp[0];
diff --git a/tycho-packaging-plugin/src/main/java/org/eclipse/tycho/buildversion/TimestampFinder.java b/tycho-packaging-plugin/src/main/java/org/eclipse/tycho/buildversion/TimestampFinder.java
new file mode 100644
index 0000000..115ced6
--- /dev/null
+++ b/tycho-packaging-plugin/src/main/java/org/eclipse/tycho/buildversion/TimestampFinder.java
@@ -0,0 +1,69 @@
+/*******************************************************************************
+ * Copyright (c) 2015 Tasktop Technologies and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Sonatype Inc. - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.tycho.buildversion;
+
+import java.text.ParsePosition;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.TimeZone;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * A helper for discovering common timestamps in strings
+ */
+public class TimestampFinder {
+
+ private static Map<SimpleDateFormat, Pattern> defaultPatterns() {
+ Map<SimpleDateFormat, Pattern> result = new LinkedHashMap<>();
+ result.put(utcFormat("yyyyMMddHHmm"), Pattern.compile("([0-9]{12})"));
+ result.put(utcFormat("yyyyMMdd-HHmm"), Pattern.compile("([0-9]{8})-([0-9]{4})"));
+ result.put(utcFormat("yyyyMMdd"), Pattern.compile("([0-9]{8})"));
+ return result;
+ }
+
+ private static SimpleDateFormat utcFormat(String pattern) {
+ SimpleDateFormat format = new SimpleDateFormat(pattern);
+ format.setTimeZone(TimeZone.getTimeZone("UTC"));
+ return format;
+ }
+
+ private final Map<SimpleDateFormat, Pattern> datePatternsByRegularExpressions;
+
+ public TimestampFinder() {
+ datePatternsByRegularExpressions = defaultPatterns();
+ }
+
+ public Date findInString(String string) {
+ for (Entry<SimpleDateFormat, Pattern> e : datePatternsByRegularExpressions.entrySet()) {
+ Matcher matcher = e.getValue().matcher(string);
+ if (matcher.find()) {
+ String group = matcher.group();
+ Date timestamp = parseTimestamp(group, e.getKey());
+ if (timestamp != null)
+ return timestamp;
+ }
+ }
+ return null;
+ }
+
+ private Date parseTimestamp(String timestampString, SimpleDateFormat format) {
+ ParsePosition pos = new ParsePosition(0);
+ Date timestamp = format.parse(timestampString, pos);
+ if (timestamp != null && pos.getIndex() == timestampString.length()) {
+ return timestamp;
+ }
+ return null;
+ }
+}
diff --git a/tycho-packaging-plugin/src/test/java/org/eclipse/tycho/buildversion/TimestampFinderTest.java b/tycho-packaging-plugin/src/test/java/org/eclipse/tycho/buildversion/TimestampFinderTest.java
new file mode 100644
index 0000000..6635d37
--- /dev/null
+++ b/tycho-packaging-plugin/src/test/java/org/eclipse/tycho/buildversion/TimestampFinderTest.java
@@ -0,0 +1,47 @@
+package org.eclipse.tycho.buildversion;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.Calendar;
+import java.util.Date;
+import java.util.TimeZone;
+
+import org.junit.Test;
+
+public class TimestampFinderTest {
+
+ @Test
+ public void testFindInString() throws Exception {
+ TimestampFinder finder = new TimestampFinder();
+
+ assertEquals(utcTimestamp(2012, 05, 06, 22, 00), finder.findInString("N201205062200"));
+ assertEquals(utcTimestamp(2012, 05, 06, 22, 00), finder.findInString("I201205062200"));
+ assertEquals(utcTimestamp(2012, 05, 06, 22, 00), finder.findInString("R201205062200"));
+
+ assertEquals(utcTimestamp(2012, 05, 06, 22, 00), finder.findInString("N20120506-2200"));
+ assertEquals(utcTimestamp(2012, 05, 06, 22, 00), finder.findInString("I20120506-2200"));
+ assertEquals(utcTimestamp(2012, 05, 06, 22, 00), finder.findInString("R20120506-2200"));
+
+ assertEquals(utcTimestamp(2012, 05, 06, 00, 00), finder.findInString("N20120506"));
+ assertEquals(utcTimestamp(2012, 05, 06, 00, 00), finder.findInString("I20120506"));
+ assertEquals(utcTimestamp(2012, 05, 06, 00, 00), finder.findInString("R20120506"));
+
+ assertEquals(utcTimestamp(2012, 05, 06, 22, 00), finder.findInString("v201205062200"));
+ assertEquals(utcTimestamp(2012, 05, 06, 22, 00), finder.findInString("v20120506-2200"));
+
+ assertEquals(utcTimestamp(2012, 05, 06, 22, 00), finder.findInString("20120506220000"));
+ assertEquals(utcTimestamp(2012, 05, 06, 22, 00), finder.findInString("20120506-220000"));
+
+ assertEquals(utcTimestamp(2012, 05, 06, 22, 00),
+ finder.findInString("scdasdcasdc.sd0320-sdva-201205062200-dscsadvj0239inacslj"));
+ }
+
+ private Date utcTimestamp(int year, int month, int day, int hourOfDay, int minute) {
+ Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
+ calendar.clear();
+ month--; // month in Calendar is 0-based
+ calendar.set(year, month, day, hourOfDay, minute);
+ return calendar.getTime();
+ }
+
+}