3 |
import java.io.InputStream; |
import java.io.InputStream; |
4 |
import java.net.URL; |
import java.net.URL; |
5 |
import java.util.Properties; |
import java.util.Properties; |
6 |
|
import java.util.regex.Matcher; |
7 |
|
import java.util.regex.Pattern; |
8 |
|
|
9 |
import org.apache.log4j.Logger; |
import org.apache.log4j.Logger; |
10 |
|
|
11 |
/** |
/** |
12 |
* Wer sein Maven Projekt wie folgt konfiguriert (oder ähnlich) kann die Maven + |
* This class provides static utility classes that help releasing and versioning |
13 |
* SVN revision nummer als Programmversion verwenden. <br/> |
* applications. Especially usefull in combination with maven2's |
14 |
|
* buildnumber-maven plugin.<br/> |
15 |
* |
* |
16 |
* 1. pom.xml anpassen: <code> |
* @author Stefan A. Tzeggai |
17 |
|
* |
18 |
|
* Wer sein Maven Projekt wie folgt konfiguriert kann die Maven + |
19 |
|
* SVN-Revisionnummer als Programmversion verwenden, z.B. |
20 |
|
* "v1.5-SNAPSHOT r123" |
21 |
|
* |
22 |
|
* <br/> |
23 |
|
* 1. pom.xml anpassen: <code> |
24 |
<plugin> |
<plugin> |
25 |
<groupId>org.codehaus.mojo</groupId> |
<groupId>org.codehaus.mojo</groupId> |
26 |
<artifactId>buildnumber-maven-plugin</artifactId> |
<artifactId>buildnumber-maven-plugin</artifactId> |
44 |
</configuration> |
</configuration> |
45 |
</plugin> |
</plugin> |
46 |
</code><br/> |
</code><br/> |
47 |
* 2. Datei src/main/resources.properties mit foglenden Inhalt anlegen:<code> |
* 2. Datei src/main/resources.properties mit foglenden Inhalt anlegen: |
48 |
#Properties describing this release/build |
* <code> |
49 |
version=${project.version} |
#Properties describing this release/build |
50 |
build=${buildNumber} |
version=${project.version} |
51 |
</code><br/> |
build=${buildNumber} |
52 |
* 3. Filtering für die src/main/resources.properties - Datei einschalten:<code> |
</code><br/> |
53 |
<resources> |
* 3. Filtering für die src/main/resources.properties - Datei |
54 |
<resource> |
* einschalten:<code> |
55 |
<filtering>true</filtering> |
* |
56 |
<directory>src/main/resources</directory> |
<resources> |
57 |
<includes><include>release.properties</include></includes> |
<resource> |
58 |
</resource> |
<filtering>true</filtering> |
59 |
</resources> |
<directory>src/main/resources</directory> |
60 |
|
<includes><include>release.properties</include></includes> |
61 |
</code> |
</resource> |
62 |
|
</resources> |
63 |
|
</code> <br/> |
64 |
|
* <br/> |
65 |
|
* Eine maven Fehlermeldung "The scm url cannot be null." ist |
66 |
|
* irreführend. In wirklichkeit wird ein Eintrag |
67 |
|
* <scm>...<developerConnection>..</scm> verlangt. |
68 |
*/ |
*/ |
69 |
public class ReleaseUtil { |
public class ReleaseUtil { |
70 |
|
private static Logger log = Logger.getLogger(ReleaseUtil.class); |
71 |
|
|
72 |
/** |
/** |
73 |
* @return The major.minor version, build number and build date |
* This is the main public API method which never throws an exception. |
74 |
|
* |
75 |
|
* @param clazz |
76 |
|
* Pass a class that resides in the same "project" or jar, where |
77 |
|
* the /release.properties resides as well. |
78 |
|
* |
79 |
|
* @return A {@link String} like "v1.4 r243" where the first number is the |
80 |
|
* maven project version and the second number is the SCM revision. |
81 |
|
* Returns "unknown version" if it can't be determined. |
82 |
*/ |
*/ |
83 |
public static String getVersionInfo(Class<?> clazz) { |
public static String getVersionInfo(Class<?> clazz) { |
|
/** |
|
|
* Release properties einlesen |
|
|
*/ |
|
84 |
try { |
try { |
85 |
return "v" + getVersion(clazz) + "-r" + getVersionBuild(clazz); |
return "v" + getVersion(clazz) + "-r" + getVersionBuild(clazz); |
86 |
} catch (final Exception e) { |
} catch (final Exception e) { |
89 |
} |
} |
90 |
|
|
91 |
/** |
/** |
92 |
* Return the major part of the software version of GP/AV/AS. |
* @param clazz |
93 |
|
* Pass a class that resides in the same "project" or jar, where |
94 |
|
* the /release.properties resides as well. |
95 |
* |
* |
96 |
* @throws Exception |
* @Return the major part of the software version or 0 if a problem occurs. |
|
* if release.properties not found |
|
97 |
*/ |
*/ |
98 |
public static int getVersionBuild(Class<?> clazz) { |
public static int getVersionBuild(Class<?> clazz) { |
99 |
try { |
try { |
117 |
|
|
118 |
return Integer.parseInt(str); |
return Integer.parseInt(str); |
119 |
} catch (final Exception e) { |
} catch (final Exception e) { |
120 |
throw new RuntimeException( |
log.error("/release.properties could not be read from " |
121 |
"/release.properties could not be read from " |
+ clazz.getSimpleName(), e); |
122 |
+ clazz.getSimpleName(), e); |
return 0; |
123 |
} |
} |
124 |
|
|
125 |
} |
} |
126 |
|
|
127 |
/** |
/** |
128 |
* Return the major part of the software version of GP/AV/AS. |
* @param clazz |
129 |
|
* Pass a class that resides in the same "project" or jar, where |
130 |
|
* the /release.properties resides as well. |
131 |
* |
* |
132 |
* @throws Exception |
* @return A Version String like "1.3" or "1.6". Returns "0.0" if a problem |
133 |
* if release.properties not found |
* occurred. |
134 |
*/ |
*/ |
135 |
public static String getVersion(Class<?> clazz) { |
public static String getVersion(Class<?> clazz) { |
136 |
|
final String defaultVer = "0.0"; |
137 |
|
|
138 |
try { |
try { |
139 |
|
|
140 |
final URL releasePropsURL = clazz |
final URL releasePropsURL = clazz |
148 |
openStream.close(); |
openStream.close(); |
149 |
} |
} |
150 |
|
|
|
final String defaultVer = "DEV"; |
|
151 |
final String versionProperty = releaseProps.getProperty("version", |
final String versionProperty = releaseProps.getProperty("version", |
152 |
defaultVer); |
defaultVer); |
153 |
if (versionProperty.equals("${project.version}")) |
if (versionProperty.equals("${project.version}")) |
154 |
return defaultVer; |
return defaultVer; |
155 |
return versionProperty; |
return versionProperty; |
156 |
} catch (final Exception e) { |
} catch (final Exception e) { |
157 |
throw new RuntimeException( |
log.error("/release.properties could not be read from " |
158 |
"/release.properties could not be read from " |
+ clazz.getSimpleName(), e); |
159 |
+ clazz.getSimpleName(), e); |
return defaultVer; |
160 |
} |
} |
161 |
|
|
162 |
} |
} |
163 |
|
|
164 |
|
/** |
165 |
|
* @param clazz |
166 |
|
* Pass a class that resides in the same "project" or jar, where |
167 |
|
* the /release.properties resides as well. |
168 |
|
* |
169 |
|
* @return Mayor version number part, e.g. "1" for version "1.3". Returns 0 |
170 |
|
* if a problem occurred. |
171 |
|
*/ |
172 |
public static int getVersionMaj(Class<?> clazz) { |
public static int getVersionMaj(Class<?> clazz) { |
173 |
try { |
try { |
174 |
return Integer.parseInt(getVersion(clazz).split("\\.")[0]); |
return Integer.parseInt(getVersion(clazz).split("\\.")[0]); |
175 |
} catch (final Exception e) { |
} catch (final Exception e) { |
176 |
|
log.error("Major version number '" + getVersion(clazz) |
177 |
|
+ "' part could not be parsed from could not parsed (from " |
178 |
|
+ clazz.getSimpleName() + "). Must be numeric!", e); |
179 |
return 0; |
return 0; |
180 |
} |
} |
181 |
} |
} |
182 |
|
|
183 |
|
/** |
184 |
|
* @param clazz |
185 |
|
* Pass a class that resides in the same "project" or jar, where |
186 |
|
* the /release.properties resides as well. |
187 |
|
* |
188 |
|
* @return Minor version number part, e.g. "3" for version "1.3". Returns 0 |
189 |
|
* if a problem occurred. |
190 |
|
*/ |
191 |
public static int getVersionMin(Class<?> clazz) { |
public static int getVersionMin(Class<?> clazz) { |
192 |
try { |
try { |
193 |
return Integer.parseInt(getVersion(clazz).split("\\.")[1]); |
return extractMinVersionFromString(getVersion(clazz)); |
194 |
} catch (final Exception e) { |
} catch (final Exception e) { |
195 |
|
log.error("Minor version number '" + getVersion(clazz) |
196 |
|
+ "' part could not be parsed from could not parsed (from " |
197 |
|
+ clazz.getSimpleName() + "). Must be numeric!", e); |
198 |
return 0; |
return 0; |
199 |
} |
} |
200 |
|
|
201 |
} |
} |
202 |
|
|
203 |
|
public static int extractMinVersionFromString(String versionString) { |
204 |
|
Pattern minVersionPattern = Pattern.compile("\\d*\\.(\\d*).*"); |
205 |
|
Matcher matcher = minVersionPattern.matcher(versionString); |
206 |
|
matcher.find(); |
207 |
|
return Integer.parseInt(matcher.group(1)); |
208 |
|
} |
209 |
|
|
210 |
/** |
/** |
211 |
* Print the GPL disclaimer to the given {@link Logger} as on INFO level. |
* @param clazz |
212 |
|
* Pass a class that resides in the same "project" or jar, where |
213 |
|
* the /release.properties resides as well. |
214 |
|
* |
215 |
|
* Print the GPL disclaimer to the given {@link Logger} on INFO |
216 |
|
* level. |
217 |
*/ |
*/ |
218 |
public static void logGPLCopyright(final Logger logger) { |
public static void logGPLCopyright(final Logger logger) { |
|
|
|
219 |
logger |
logger |
220 |
.info("\nThis program is free software: you can redistribute it and/or modify\n" |
.info("\nThis program is free software: you can redistribute it and/or modify\n" |
221 |
+ "it under the terms of the GNU General Public License as published by\n" |
+ "it under the terms of the GNU General Public License as published by\n" |
229 |
} |
} |
230 |
|
|
231 |
/** |
/** |
232 |
* Print the LGPL disclaimer to the given {@link Logger} as on INFO level. |
* @param clazz |
233 |
|
* Pass a class that resides in the same "project" or jar, where |
234 |
|
* the /release.properties resides as well. |
235 |
|
* |
236 |
|
* Print the LGPL disclaimer to the given {@link Logger} on INFO |
237 |
|
* level. |
238 |
*/ |
*/ |
239 |
public static void logLGPLCopyright(final Logger logger) { |
public static void logLGPLCopyright(final Logger logger) { |
|
|
|
240 |
logger |
logger |
241 |
.info("\nThis program is free software: you can redistribute it and/or modify\n" |
.info("\nThis program is free software: you can redistribute it and/or modify\n" |
242 |
+ "it under the terms of the GNU Lesser General Public License as published by\n" |
+ "it under the terms of the GNU Lesser General Public License as published by\n" |
247 |
+ "but WITHOUT ANY WARRANTY; without even the implied warranty of\n" |
+ "but WITHOUT ANY WARRANTY; without even the implied warranty of\n" |
248 |
+ "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" |
+ "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" |
249 |
+ "GNU Lesser General Public License for more details.\n"); |
+ "GNU Lesser General Public License for more details.\n"); |
|
|
|
250 |
} |
} |
251 |
} |
} |