changeset 2:4db19f1c5ad0

create ProjectVersion as instance instead of static. Add build info to jar manifest
author smith@nwoca.org
date Fri, 18 May 2012 18:21:13 +0100
parents 619ed31de9ac
children 1756755e4e71
files build.gradle src/main/groovy/org/ssdt_ohio/gradle/plugins/VersionClassPlugin.groovy
diffstat 2 files changed, 53 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- a/build.gradle	Thu May 17 23:33:24 2012 -0400
+++ b/build.gradle	Fri May 18 18:21:13 2012 +0100
@@ -16,17 +16,9 @@
 dependencies {
     groovy      localGroovy()
     compile     gradleApi()
-//                'org.gcontracts:gcontracts-core:1.2.5'
     testCompile 'junit:junit:4.10'
 }
 
-sourceSets {
-    main {
-        groovy { srcDir 'other' }
-
-    }
-}
-
 idea.project {
     jdkName = '1.6'
 }
--- a/src/main/groovy/org/ssdt_ohio/gradle/plugins/VersionClassPlugin.groovy	Thu May 17 23:33:24 2012 -0400
+++ b/src/main/groovy/org/ssdt_ohio/gradle/plugins/VersionClassPlugin.groovy	Fri May 18 18:21:13 2012 +0100
@@ -1,24 +1,32 @@
 package org.ssdt_ohio.gradle.plugins
 
+import org.gradle.api.Plugin
 import org.gradle.api.Project
-import org.gradle.api.Plugin
-import org.gradle.api.plugins.JavaPlugin
-import java.util.Date
-import org.gradle.api.plugins.GroovyPlugin;
+import org.gradle.api.plugins.GroovyPlugin
 
-class VersionClassPlugin implements Plugin<Project>  {
+class VersionClassPlugin implements Plugin<Project> {
 
     def void apply(Project project) {
-        project.getPlugins().apply( GroovyPlugin.class )
+        project.getPlugins().apply(GroovyPlugin.class)
         def genSrc = 'generated-src/version'
         def generatedSrcDir = new File(project.buildDir, genSrc)
 
         def makeVersionClassTask = project.task('makeVersionClass') << {
 
-            def now = new Date()
+            def now = new Date().format('yyyy-MM-dd hh:mm:ss')
+
+            def buildNumber = -1
+            if (project.rootProject?.file('build-number.txt').exists()) {
+                def props = new Properties()
+                project.rootProject?.file('build-number.txt').withInputStream {stream ->
+                    props.load(stream)
+                }
+                buildNumber = props['build.number']
+            }
+
             def targetPackage = getTargetClass(project)
 
-            def outFile = new File(generatedSrcDir,targetPackage.replaceAll('\\.',"/") + "/ProjectVersion.groovy")
+            def outFile = new File(generatedSrcDir, targetPackage.replaceAll('\\.', "/") + "/ProjectVersion.groovy")
 
             outFile.getParentFile().mkdirs()
             logger.info("creating $targetPackage in $outFile")
@@ -26,21 +34,27 @@
             f.write("""
 package $targetPackage
 /**
- * Generated by gradle build.
+ * Generated by gradle build. $now ($buildNumber)
+ * @version ${project.version}
  */
 public class ProjectVersion {
 
-    public static final String NAME = "${project.name}"
-    public static final String GROUP = "${project.group}"
-    public static final String VERSION = "${project.version}"
-    public static final String DATE = "$now"
+    final String name = "${project.name}"
+    final String group = "${project.group}"
+    final String version = "${project.version}"
+    final String buildDate = "$now"
+    final int buildNumber = $buildNumber
 
-    public static String getDetailedVersion() {
-        "\$GROUP:\$NAME:\$VERSION \$DATE"
+    String getFullVersion() {
+        "\$version \$buildDate (#\$buildNumber)"
     }
 
-    public static String getID() {
-        "\$GROUP:\$NAME:\$VERSION"
+    String getId() {
+        "\$group:\$name:\$version"
+    }
+
+    String toString() {
+        "\$id \$buildDate (#\$buildNumber)"
     }
 }
 """)
@@ -50,24 +64,38 @@
         project.sourceSets {
             main {
                 groovy {
-                    srcDir project.buildDir.name+'/'+genSrc
+                    srcDir project.buildDir.name + '/' + genSrc
                 }
             }
         }
 
-        makeVersionClassTask.getInputs().files(project.sourceSets.main.getAllSource() )
+        makeVersionClassTask.getInputs().files(project.sourceSets.main.getAllSource())
         makeVersionClassTask.getOutputs().files(generatedSrcDir)
         if (project.getBuildFile() != null && project.getBuildFile().exists()) {
             makeVersionClassTask.getInputs().files(project.getBuildFile())
         }
-//        project.getTasks().getByName('compileGroovy').dependsOn('compileVersionGroovy')
         project.getTasks().getByName('compileGroovy').dependsOn('makeVersionClass')
-//        project.getTasks().getByName('jar') {
-//            from project.sourceSets.version.output
-//        }
+
+        project.getTasks().getByName('jar').manifest.attributes(
+                [
+                        "Specification-Title": project.name,
+                        "Specification-Version": project.version,
+                        "Specification-Vendor": project.group,
+                        "Implementation-Title": project.name,
+                        "Implementation-Version": project.version,
+                        "Implementation-Vendor": project.group,
+                        "Built-By": System.properties['user.name'],
+                        "Build-Jdk": System.properties['java.version'],
+                        "Build-Time":  new Date().format('yyyy-MM-dd hh:mm:ss'),
+
+                ]
+        )
+
+
     }
 
 
+
     private getTargetClass(project) {
         def source = project.sourceSets.main.groovy.getSrcDirTrees()
         def files = project.sourceSets.main.groovy as File[]
@@ -77,10 +105,11 @@
         source.each {
             targetPackage -= it.getDir().getPath().toString()
         }
-        targetPackage = targetPackage.replaceAll("\\\\",'.').replaceAll('/','.')
+        targetPackage = targetPackage.replaceAll("\\\\", '.').replaceAll('/', '.')
         targetPackage = targetPackage.startsWith(".") ? targetPackage - "." : targetPackage
 
         return targetPackage
 
     }
+
 }
\ No newline at end of file