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