diff options
-rw-r--r-- | build.gradle | 20 | ||||
-rw-r--r-- | gradle/buildscript.gradle | 2 | ||||
-rw-r--r-- | src/main/java/org/anarres/cpp/BuildMetadata.java | 72 | ||||
-rw-r--r-- | src/main/java/org/anarres/cpp/Main.java | 6 | ||||
-rw-r--r-- | src/main/velocity/org/anarres/cpp/Version.java | 12 | ||||
-rw-r--r-- | src/test/java/org/anarres/cpp/BuildMetadataTest.java | 33 |
6 files changed, 131 insertions, 14 deletions
diff --git a/build.gradle b/build.gradle index c3813f8..0f0d31e 100644 --- a/build.gradle +++ b/build.gradle @@ -12,25 +12,27 @@ apply from: file('gradle/nexus.gradle') apply from: file('gradle/check.gradle') apply from: file('gradle/license.gradle') -apply plugin: 'application' -apply plugin: 'velocity' - dependencies { // compile 'gnu.getopt:java-getopt:1.0.13' compile 'net.sf.jopt-simple:jopt-simple:4.7' compile 'org.apache.ant:ant:1.7.0' + compile 'com.github.zafarkhaja:java-semver:0.8.0' runtime 'org.slf4j:slf4j-simple:1.7.7' testCompile 'com.google.guava:guava:18.0' } -velocity { - def p = project - Map m = [ - version: project.version - ] - context m +// This ensures that the info-plugin's properties file is in the +// same location for the test suite as in the JAR. +task('processTestVersionResources', type: Copy, dependsOn: processTestResources) { + into project.sourceSets.test.output.resourcesDir + from(writeManifestProperties) { + into "META-INF" + } } +testClasses.dependsOn(processTestVersionResources) + +apply plugin: 'application' mainClassName = "org.anarres.cpp.Main" diff --git a/gradle/buildscript.gradle b/gradle/buildscript.gradle index bc2c68c..50a1d9c 100644 --- a/gradle/buildscript.gradle +++ b/gradle/buildscript.gradle @@ -16,6 +16,4 @@ dependencies { exclude group: 'com.perforce' } classpath 'com.bmuschko:gradle-nexus-plugin:2.2' - - classpath 'org.anarres.gradle:gradle-velocity-plugin:1.0.0' } diff --git a/src/main/java/org/anarres/cpp/BuildMetadata.java b/src/main/java/org/anarres/cpp/BuildMetadata.java new file mode 100644 index 0000000..79de407 --- /dev/null +++ b/src/main/java/org/anarres/cpp/BuildMetadata.java @@ -0,0 +1,72 @@ +package org.anarres.cpp; + +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; +import javax.annotation.Nonnull; + +/** + * Returns information about the build. + * + * @author shevek + */ +public class BuildMetadata { + + public static final String RESOURCE = "/META-INF/jcpp.properties"; + private static BuildMetadata INSTANCE; + + /** @throws RuntimeException if the properties file cannot be found on the classpath. */ + @Nonnull + public static synchronized BuildMetadata getInstance() { + try { + if (INSTANCE == null) + INSTANCE = new BuildMetadata(); + return INSTANCE; + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + private final Properties properties = new Properties(); + + private BuildMetadata() throws IOException { + URL url = BuildMetadata.class.getResource(RESOURCE); + InputStream in = url.openStream(); + try { + properties.load(in); + } finally { + in.close(); + } + } + + @Nonnull + public Map<? extends String, ? extends String> asMap() { + Map<String, String> out = new HashMap<String, String>(); + for (Map.Entry<Object, Object> e : properties.entrySet()) + out.put(String.valueOf(e.getKey()), String.valueOf(e.getValue())); + return out; + } + + @Nonnull + public com.github.zafarkhaja.semver.Version getVersion() { + return com.github.zafarkhaja.semver.Version.valueOf(properties.getProperty("Implementation-Version")); + } + + @Nonnull + public Date getBuildDate() throws ParseException { + // Build-Date=2015-01-01_10:09:09 + String text = properties.getProperty("Build-Date"); + SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss"); + return format.parse(text); + } + + public String getChangeId() { + return properties.getProperty("Change"); + } +} diff --git a/src/main/java/org/anarres/cpp/Main.java b/src/main/java/org/anarres/cpp/Main.java index 5a3e113..acf2436 100644 --- a/src/main/java/org/anarres/cpp/Main.java +++ b/src/main/java/org/anarres/cpp/Main.java @@ -25,7 +25,6 @@ import javax.annotation.Nonnull; import joptsimple.OptionParser; import joptsimple.OptionSet; import joptsimple.OptionSpec; -import joptsimple.ValueConverter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -59,7 +58,7 @@ public class Main { "Displays command-line help.") .forHelp(); OptionSpec<?> versionOption = parser.acceptsAll(Arrays.asList("version"), - "Displays the product version (" + Version.getVersion() + ") and exits.") + "Displays the product version (" + BuildMetadata.getInstance().getVersion() + ") and exits.") .forHelp(); OptionSpec<?> debugOption = parser.acceptsAll(Arrays.asList("debug"), @@ -187,7 +186,8 @@ public class Main { } private static void version(@Nonnull PrintStream out) { - out.println("Anarres Java C Preprocessor version " + Version.getVersion()); + BuildMetadata metadata = BuildMetadata.getInstance(); + out.println("Anarres Java C Preprocessor version " + metadata.getVersion() + " change-id " + metadata.getChangeId()); out.println("Copyright (C) 2008-2014 Shevek (http://www.anarres.org/)."); out.println("This is free software; see the source for copying conditions. There is NO"); out.println("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."); diff --git a/src/main/velocity/org/anarres/cpp/Version.java b/src/main/velocity/org/anarres/cpp/Version.java index 62494b5..cedd6bb 100644 --- a/src/main/velocity/org/anarres/cpp/Version.java +++ b/src/main/velocity/org/anarres/cpp/Version.java @@ -17,6 +17,7 @@ package org.anarres.cpp; +import javax.annotation.CheckForNull; import javax.annotation.Nonnull; /** @@ -35,12 +36,14 @@ public class Version { private static final int major; private static final int minor; private static final int patch; + private static final String modifier; static { String[] tmp = VERSION.split("[\\.-]"); major = Integer.parseInt(tmp[0]); minor = Integer.parseInt(tmp[1]); patch = Integer.parseInt(tmp[2]); + modifier = (tmp.length > 3) ? tmp[3] : null; } @Nonnull @@ -60,6 +63,15 @@ public class Version { return patch; } + @CheckForNull + public static String getModifier() { + return modifier; + } + + public static boolean isSnapshot() { + return "SNAPSHOT".equalsIgnoreCase(getModifier()); + } + public static void main(String[] args) { System.out.println("Version " + VERSION); System.out.println("getVersion() returns " + getVersion()); diff --git a/src/test/java/org/anarres/cpp/BuildMetadataTest.java b/src/test/java/org/anarres/cpp/BuildMetadataTest.java new file mode 100644 index 0000000..42dc071 --- /dev/null +++ b/src/test/java/org/anarres/cpp/BuildMetadataTest.java @@ -0,0 +1,33 @@ +package org.anarres.cpp; + +import com.google.common.base.Charsets; +import com.google.common.io.Resources; +import java.net.URL; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * + * @author shevek + */ +public class BuildMetadataTest { + + private static final Logger LOG = LoggerFactory.getLogger(BuildMetadataTest.class); + + @Test + public void testProperties() throws Exception { + URL url = Resources.getResource("META-INF/jcpp.properties"); + String text = Resources.asCharSource(url, Charsets.ISO_8859_1).read(); + LOG.info("Metadata is " + text); + } + + @Test + public void testMetadata() throws Exception { + BuildMetadata metadata = BuildMetadata.getInstance(); + LOG.info("Version is " + metadata.getVersion()); + LOG.info("BuildDate is " + metadata.getBuildDate()); + LOG.info("ChangeId is " + metadata.getChangeId()); + } + +} |