comparison src/main/groovy/org/ssdt_ohio/gradle/plugins/VersionClassPlugin.groovy @ 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 c51874d4a5a7
children 1756755e4e71
comparison
equal deleted inserted replaced
1:619ed31de9ac 2:4db19f1c5ad0
1 package org.ssdt_ohio.gradle.plugins 1 package org.ssdt_ohio.gradle.plugins
2 2
3 import org.gradle.api.Plugin
3 import org.gradle.api.Project 4 import org.gradle.api.Project
4 import org.gradle.api.Plugin 5 import org.gradle.api.plugins.GroovyPlugin
5 import org.gradle.api.plugins.JavaPlugin
6 import java.util.Date
7 import org.gradle.api.plugins.GroovyPlugin;
8 6
9 class VersionClassPlugin implements Plugin<Project> { 7 class VersionClassPlugin implements Plugin<Project> {
10 8
11 def void apply(Project project) { 9 def void apply(Project project) {
12 project.getPlugins().apply( GroovyPlugin.class ) 10 project.getPlugins().apply(GroovyPlugin.class)
13 def genSrc = 'generated-src/version' 11 def genSrc = 'generated-src/version'
14 def generatedSrcDir = new File(project.buildDir, genSrc) 12 def generatedSrcDir = new File(project.buildDir, genSrc)
15 13
16 def makeVersionClassTask = project.task('makeVersionClass') << { 14 def makeVersionClassTask = project.task('makeVersionClass') << {
17 15
18 def now = new Date() 16 def now = new Date().format('yyyy-MM-dd hh:mm:ss')
17
18 def buildNumber = -1
19 if (project.rootProject?.file('build-number.txt').exists()) {
20 def props = new Properties()
21 project.rootProject?.file('build-number.txt').withInputStream {stream ->
22 props.load(stream)
23 }
24 buildNumber = props['build.number']
25 }
26
19 def targetPackage = getTargetClass(project) 27 def targetPackage = getTargetClass(project)
20 28
21 def outFile = new File(generatedSrcDir,targetPackage.replaceAll('\\.',"/") + "/ProjectVersion.groovy") 29 def outFile = new File(generatedSrcDir, targetPackage.replaceAll('\\.', "/") + "/ProjectVersion.groovy")
22 30
23 outFile.getParentFile().mkdirs() 31 outFile.getParentFile().mkdirs()
24 logger.info("creating $targetPackage in $outFile") 32 logger.info("creating $targetPackage in $outFile")
25 def f = new FileWriter(outFile) 33 def f = new FileWriter(outFile)
26 f.write(""" 34 f.write("""
27 package $targetPackage 35 package $targetPackage
28 /** 36 /**
29 * Generated by gradle build. 37 * Generated by gradle build. $now ($buildNumber)
38 * @version ${project.version}
30 */ 39 */
31 public class ProjectVersion { 40 public class ProjectVersion {
32 41
33 public static final String NAME = "${project.name}" 42 final String name = "${project.name}"
34 public static final String GROUP = "${project.group}" 43 final String group = "${project.group}"
35 public static final String VERSION = "${project.version}" 44 final String version = "${project.version}"
36 public static final String DATE = "$now" 45 final String buildDate = "$now"
46 final int buildNumber = $buildNumber
37 47
38 public static String getDetailedVersion() { 48 String getFullVersion() {
39 "\$GROUP:\$NAME:\$VERSION \$DATE" 49 "\$version \$buildDate (#\$buildNumber)"
40 } 50 }
41 51
42 public static String getID() { 52 String getId() {
43 "\$GROUP:\$NAME:\$VERSION" 53 "\$group:\$name:\$version"
54 }
55
56 String toString() {
57 "\$id \$buildDate (#\$buildNumber)"
44 } 58 }
45 } 59 }
46 """) 60 """)
47 f.close() 61 f.close()
48 } 62 }
49 63
50 project.sourceSets { 64 project.sourceSets {
51 main { 65 main {
52 groovy { 66 groovy {
53 srcDir project.buildDir.name+'/'+genSrc 67 srcDir project.buildDir.name + '/' + genSrc
54 } 68 }
55 } 69 }
56 } 70 }
57 71
58 makeVersionClassTask.getInputs().files(project.sourceSets.main.getAllSource() ) 72 makeVersionClassTask.getInputs().files(project.sourceSets.main.getAllSource())
59 makeVersionClassTask.getOutputs().files(generatedSrcDir) 73 makeVersionClassTask.getOutputs().files(generatedSrcDir)
60 if (project.getBuildFile() != null && project.getBuildFile().exists()) { 74 if (project.getBuildFile() != null && project.getBuildFile().exists()) {
61 makeVersionClassTask.getInputs().files(project.getBuildFile()) 75 makeVersionClassTask.getInputs().files(project.getBuildFile())
62 } 76 }
63 // project.getTasks().getByName('compileGroovy').dependsOn('compileVersionGroovy')
64 project.getTasks().getByName('compileGroovy').dependsOn('makeVersionClass') 77 project.getTasks().getByName('compileGroovy').dependsOn('makeVersionClass')
65 // project.getTasks().getByName('jar') { 78
66 // from project.sourceSets.version.output 79 project.getTasks().getByName('jar').manifest.attributes(
67 // } 80 [
81 "Specification-Title": project.name,
82 "Specification-Version": project.version,
83 "Specification-Vendor": project.group,
84 "Implementation-Title": project.name,
85 "Implementation-Version": project.version,
86 "Implementation-Vendor": project.group,
87 "Built-By": System.properties['user.name'],
88 "Build-Jdk": System.properties['java.version'],
89 "Build-Time": new Date().format('yyyy-MM-dd hh:mm:ss'),
90
91 ]
92 )
93
94
68 } 95 }
96
69 97
70 98
71 private getTargetClass(project) { 99 private getTargetClass(project) {
72 def source = project.sourceSets.main.groovy.getSrcDirTrees() 100 def source = project.sourceSets.main.groovy.getSrcDirTrees()
73 def files = project.sourceSets.main.groovy as File[] 101 def files = project.sourceSets.main.groovy as File[]
75 103
76 def targetPackage = targetDir 104 def targetPackage = targetDir
77 source.each { 105 source.each {
78 targetPackage -= it.getDir().getPath().toString() 106 targetPackage -= it.getDir().getPath().toString()
79 } 107 }
80 targetPackage = targetPackage.replaceAll("\\\\",'.').replaceAll('/','.') 108 targetPackage = targetPackage.replaceAll("\\\\", '.').replaceAll('/', '.')
81 targetPackage = targetPackage.startsWith(".") ? targetPackage - "." : targetPackage 109 targetPackage = targetPackage.startsWith(".") ? targetPackage - "." : targetPackage
82 110
83 return targetPackage 111 return targetPackage
84 112
85 } 113 }
114
86 } 115 }