0
|
1 package org.ssdt_ohio.gradle.plugins
|
|
2
|
|
3 import org.gradle.api.Project
|
|
4 import org.gradle.api.Plugin
|
|
5 import org.gradle.api.plugins.JavaPlugin
|
|
6 import java.util.Date
|
|
7 import org.gradle.api.plugins.GroovyPlugin;
|
|
8
|
|
9 class VersionClassPlugin implements Plugin<Project> {
|
|
10
|
|
11 def void apply(Project project) {
|
|
12 project.getPlugins().apply( GroovyPlugin.class )
|
|
13 def genSrc = 'generated-src/version'
|
|
14 def generatedSrcDir = new File(project.buildDir, genSrc)
|
|
15
|
|
16 def makeVersionClassTask = project.task('makeVersionClass') << {
|
|
17
|
|
18 def now = new Date()
|
|
19 def targetPackage = getTargetClass(project)
|
|
20
|
|
21 def outFile = new File(generatedSrcDir,targetPackage.replaceAll('\\.',"/") + "/ProjectVersion.groovy")
|
|
22
|
|
23 outFile.getParentFile().mkdirs()
|
|
24 logger.info("creating $targetPackage in $outFile")
|
|
25 def f = new FileWriter(outFile)
|
|
26 f.write("""
|
|
27 package $targetPackage
|
|
28 /**
|
|
29 * Generated by gradle build.
|
|
30 */
|
|
31 public class ProjectVersion {
|
|
32
|
|
33 public static final String NAME = "${project.name}"
|
|
34 public static final String GROUP = "${project.group}"
|
|
35 public static final String VERSION = "${project.version}"
|
|
36 public static final String DATE = "$now"
|
|
37
|
|
38 public static String getDetailedVersion() {
|
|
39 "\$GROUP:\$NAME:\$VERSION \$DATE"
|
|
40 }
|
|
41
|
|
42 public static String getID() {
|
|
43 "\$GROUP:\$NAME:\$VERSION"
|
|
44 }
|
|
45 }
|
|
46 """)
|
|
47 f.close()
|
|
48 }
|
|
49
|
|
50 project.sourceSets {
|
|
51 main {
|
|
52 groovy {
|
|
53 srcDir project.buildDir.name+'/'+genSrc
|
|
54 }
|
|
55 }
|
|
56 }
|
|
57
|
|
58 makeVersionClassTask.getInputs().files(project.sourceSets.main.getAllSource() )
|
|
59 makeVersionClassTask.getOutputs().files(generatedSrcDir)
|
|
60 if (project.getBuildFile() != null && project.getBuildFile().exists()) {
|
|
61 makeVersionClassTask.getInputs().files(project.getBuildFile())
|
|
62 }
|
|
63 // project.getTasks().getByName('compileGroovy').dependsOn('compileVersionGroovy')
|
|
64 project.getTasks().getByName('compileGroovy').dependsOn('makeVersionClass')
|
|
65 // project.getTasks().getByName('jar') {
|
|
66 // from project.sourceSets.version.output
|
|
67 // }
|
|
68 }
|
|
69
|
|
70
|
|
71 private getTargetClass(project) {
|
|
72 def source = project.sourceSets.main.groovy.getSrcDirTrees()
|
|
73 def files = project.sourceSets.main.groovy as File[]
|
|
74 def targetDir = files.first().parentFile.toString()
|
|
75
|
|
76 def targetPackage = targetDir
|
|
77 source.each {
|
|
78 targetPackage -= it.getDir().getPath().toString()
|
|
79 }
|
|
80 targetPackage = targetPackage.replaceAll("\\\\",'.').replaceAll('/','.')
|
|
81 targetPackage = targetPackage.startsWith(".") ? targetPackage - "." : targetPackage
|
|
82
|
|
83 return targetPackage
|
|
84
|
|
85 }
|
|
86 } |