1 |
alfonx |
800 |
package skrueger.versionnumber; |
2 |
|
|
|
3 |
|
|
import java.io.InputStream; |
4 |
|
|
import java.net.URL; |
5 |
|
|
import java.util.Properties; |
6 |
|
|
|
7 |
|
|
import org.apache.log4j.Logger; |
8 |
|
|
|
9 |
|
|
/** |
10 |
|
|
* Wer sein Maven Projekt wie folgt konfiguriert (oder ähnlich) kann die Maven + |
11 |
|
|
* SVN revision nummer als Programmversion verwenden. <br/> |
12 |
|
|
* |
13 |
|
|
* 1. pom.xml anpassen: <code> |
14 |
|
|
<plugin> |
15 |
|
|
<groupId>org.codehaus.mojo</groupId> |
16 |
|
|
<artifactId>buildnumber-maven-plugin</artifactId> |
17 |
|
|
<version>1.0-beta-4</version> |
18 |
|
|
<executions> |
19 |
|
|
<execution> |
20 |
|
|
<id>buildnumber in phase initialize</id> |
21 |
|
|
<phase>initialize</phase> |
22 |
|
|
<goals> |
23 |
|
|
<goal>create</goal> |
24 |
|
|
</goals> |
25 |
|
|
</execution> |
26 |
|
|
</executions> |
27 |
|
|
<configuration> |
28 |
|
|
|
29 |
|
|
<doCheck>false</doCheck> |
30 |
|
|
<doUpdate>false</doUpdate> |
31 |
|
|
<providerImplementations> |
32 |
|
|
<svn>javasvn</svn> |
33 |
|
|
</providerImplementations> |
34 |
|
|
</configuration> |
35 |
|
|
</plugin> |
36 |
|
|
</code><br/> |
37 |
|
|
* 2. Datei src/main/resources.properties mit foglenden Inhalt anlegen:<code> |
38 |
|
|
#Properties describing this release/build |
39 |
|
|
version=${project.version} |
40 |
|
|
build=${buildNumber} |
41 |
|
|
</code><br/> |
42 |
|
|
* 3. Filtering für die src/main/resources.properties - Datei einschalten:<code> |
43 |
|
|
<resources> |
44 |
|
|
<resource> |
45 |
|
|
<filtering>true</filtering> |
46 |
|
|
<directory>src/main/resources</directory> |
47 |
|
|
<includes><include>release.properties</include></includes> |
48 |
|
|
</resource> |
49 |
|
|
</resources> |
50 |
|
|
|
51 |
|
|
</code> |
52 |
|
|
*/ |
53 |
|
|
public class ReleaseUtil { |
54 |
|
|
|
55 |
|
|
/** |
56 |
|
|
* @return The major.minor version, build number and build date |
57 |
|
|
*/ |
58 |
|
|
public static String getVersionInfo(Class<?> clazz) { |
59 |
|
|
/** |
60 |
|
|
* Release properties einlesen |
61 |
|
|
*/ |
62 |
|
|
try { |
63 |
|
|
return "v" + getVersion(clazz) + "-r" + getVersionBuild(clazz); |
64 |
|
|
} catch (final Exception e) { |
65 |
|
|
return "unknown version"; |
66 |
|
|
} |
67 |
|
|
} |
68 |
|
|
|
69 |
|
|
/** |
70 |
|
|
* Return the major part of the software version of GP/AV/AS. |
71 |
|
|
* |
72 |
|
|
* @throws Exception |
73 |
|
|
* if release.properties not found |
74 |
|
|
*/ |
75 |
|
|
public static int getVersionBuild(Class<?> clazz) { |
76 |
|
|
try { |
77 |
|
|
final URL releasePropsURL = clazz |
78 |
|
|
.getResource("/release.properties"); |
79 |
|
|
|
80 |
|
|
final Properties releaseProps = new Properties(); |
81 |
|
|
final InputStream openStream = releasePropsURL.openStream(); |
82 |
|
|
try { |
83 |
|
|
releaseProps.load(openStream); |
84 |
|
|
} finally { |
85 |
|
|
openStream.close(); |
86 |
|
|
} |
87 |
|
|
final String str = releaseProps.getProperty("build", "0"); |
88 |
|
|
|
89 |
|
|
if (str.equals("${buildNumber}")) { |
90 |
|
|
// We are in development or Maven didn't filter the properties |
91 |
|
|
// while building. |
92 |
|
|
return 0; |
93 |
|
|
} |
94 |
|
|
|
95 |
|
|
return Integer.parseInt(str); |
96 |
|
|
} catch (final Exception e) { |
97 |
|
|
throw new RuntimeException( |
98 |
|
|
"/release.properties could not be read from " |
99 |
|
|
+ clazz.getSimpleName(), e); |
100 |
|
|
} |
101 |
|
|
|
102 |
|
|
} |
103 |
|
|
|
104 |
|
|
/** |
105 |
|
|
* Return the major part of the software version of GP/AV/AS. |
106 |
|
|
* |
107 |
|
|
* @throws Exception |
108 |
|
|
* if release.properties not found |
109 |
|
|
*/ |
110 |
|
|
public static String getVersion(Class<?> clazz) { |
111 |
|
|
try { |
112 |
|
|
|
113 |
|
|
final URL releasePropsURL = clazz |
114 |
|
|
.getResource("/release.properties"); |
115 |
|
|
|
116 |
|
|
final Properties releaseProps = new Properties(); |
117 |
|
|
final InputStream openStream = releasePropsURL.openStream(); |
118 |
|
|
try { |
119 |
|
|
releaseProps.load(openStream); |
120 |
|
|
} finally { |
121 |
|
|
openStream.close(); |
122 |
|
|
} |
123 |
|
|
|
124 |
|
|
final String defaultVer = "DEV"; |
125 |
|
|
final String versionProperty = releaseProps.getProperty("version", |
126 |
|
|
defaultVer); |
127 |
|
|
if (versionProperty.equals("${project.version}")) |
128 |
|
|
return defaultVer; |
129 |
|
|
return versionProperty; |
130 |
|
|
} catch (final Exception e) { |
131 |
|
|
throw new RuntimeException( |
132 |
|
|
"/release.properties could not be read from " |
133 |
|
|
+ clazz.getSimpleName(), e); |
134 |
|
|
} |
135 |
|
|
|
136 |
|
|
} |
137 |
|
|
|
138 |
|
|
public static int getVersionMaj(Class<?> clazz) { |
139 |
|
|
try { |
140 |
|
|
return Integer.parseInt(getVersion(clazz).split("\\.")[0]); |
141 |
|
|
} catch (final Exception e) { |
142 |
|
|
return 0; |
143 |
|
|
} |
144 |
|
|
} |
145 |
|
|
|
146 |
|
|
public static int getVersionMin(Class<?> clazz) { |
147 |
|
|
try { |
148 |
|
|
return Integer.parseInt(getVersion(clazz).split("\\.")[1]); |
149 |
|
|
} catch (final Exception e) { |
150 |
|
|
return 0; |
151 |
|
|
} |
152 |
|
|
|
153 |
|
|
} |
154 |
|
|
|
155 |
|
|
/** |
156 |
|
|
* Print the GPL disclaimer to the given {@link Logger} as on INFO level. |
157 |
|
|
*/ |
158 |
|
|
public static void logGPLCopyright(final Logger logger) { |
159 |
|
|
|
160 |
|
|
logger |
161 |
|
|
.info("\nThis program is free software: you can redistribute it and/or modify\n" |
162 |
|
|
+ "it under the terms of the GNU General Public License as published by\n" |
163 |
|
|
+ "the Free Software Foundation, either version 3 of the License, or\n" |
164 |
|
|
+ "(at your option) any later version.\n" |
165 |
|
|
+ "\n" |
166 |
|
|
+ "This program is distributed in the hope that it will be useful,\n" |
167 |
|
|
+ "but WITHOUT ANY WARRANTY; without even the implied warranty of\n" |
168 |
|
|
+ "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" |
169 |
|
|
+ "GNU General Public License for more details.\n"); |
170 |
|
|
} |
171 |
|
|
|
172 |
|
|
/** |
173 |
|
|
* Print the LGPL disclaimer to the given {@link Logger} as on INFO level. |
174 |
|
|
*/ |
175 |
|
|
public static void logLGPLCopyright(final Logger logger) { |
176 |
|
|
|
177 |
|
|
logger |
178 |
|
|
.info("\nThis program is free software: you can redistribute it and/or modify\n" |
179 |
|
|
+ "it under the terms of the GNU Lesser General Public License as published by\n" |
180 |
|
|
+ "the Free Software Foundation, either version 3 of the License, or\n" |
181 |
|
|
+ "(at your option) any later version.\n" |
182 |
|
|
+ "\n" |
183 |
|
|
+ "This program is distributed in the hope that it will be useful,\n" |
184 |
|
|
+ "but WITHOUT ANY WARRANTY; without even the implied warranty of\n" |
185 |
|
|
+ "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" |
186 |
|
|
+ "GNU Lesser General Public License for more details.\n"); |
187 |
|
|
|
188 |
|
|
} |
189 |
|
|
} |