/[schmitzm]/branches/2.3.x/src/skrueger/versionnumber/ReleaseUtil.java
ViewVC logotype

Annotation of /branches/2.3.x/src/skrueger/versionnumber/ReleaseUtil.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1185 - (hide annotations)
Wed Oct 27 14:53:20 2010 UTC (14 years, 4 months ago) by alfonx
Original Path: trunk/src/skrueger/versionnumber/ReleaseUtil.java
File MIME type: text/plain
File size: 8285 byte(s)


1 alfonx 800 package skrueger.versionnumber;
2    
3     import java.io.InputStream;
4     import java.net.URL;
5     import java.util.Properties;
6 alfonx 813 import java.util.regex.Matcher;
7     import java.util.regex.Pattern;
8 alfonx 800
9     import org.apache.log4j.Logger;
10    
11     /**
12 alfonx 801 * This class provides static utility classes that help releasing and versioning
13     * applications. Especially usefull in combination with maven2's
14     * buildnumber-maven plugin.<br/>
15 alfonx 800 *
16 alfonx 801 * @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 alfonx 1111 <plugin>
25 alfonx 800 <groupId>org.codehaus.mojo</groupId>
26     <artifactId>buildnumber-maven-plugin</artifactId>
27     <version>1.0-beta-4</version>
28     <executions>
29     <execution>
30     <id>buildnumber in phase initialize</id>
31     <phase>initialize</phase>
32     <goals>
33     <goal>create</goal>
34     </goals>
35     </execution>
36     </executions>
37     <configuration>
38 alfonx 1111 <!-- <doCheck>false</doCheck> <doUpdate>false</doUpdate> <providerImplementations>
39     <svn>javasvn</svn> </providerImplementations> -->
40     <format>{0,date,yyyyMMddHHmm}</format>
41     <items>
42     <item>timestamp</item>
43     </items>
44 alfonx 800 </configuration>
45     </plugin>
46     </code><br/>
47 alfonx 801 * 2. Datei src/main/resources.properties mit foglenden Inhalt anlegen:
48     * <code>
49     #Properties describing this release/build
50     version=${project.version}
51     build=${buildNumber}
52     </code><br/>
53     * 3. Filtering für die src/main/resources.properties - Datei
54     * einschalten:<code>
55     *
56     <resources>
57     <resource>
58     <filtering>true</filtering>
59     <directory>src/main/resources</directory>
60     <includes><include>release.properties</include></includes>
61     </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 alfonx 800 */
69     public class ReleaseUtil {
70 alfonx 801 private static Logger log = Logger.getLogger(ReleaseUtil.class);
71 alfonx 800
72     /**
73 alfonx 801 * 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 alfonx 800 */
83     public static String getVersionInfo(Class<?> clazz) {
84     try {
85     return "v" + getVersion(clazz) + "-r" + getVersionBuild(clazz);
86     } catch (final Exception e) {
87     return "unknown version";
88     }
89     }
90    
91     /**
92 alfonx 801 * @param clazz
93     * Pass a class that resides in the same "project" or jar, where
94     * the /release.properties resides as well.
95 alfonx 800 *
96 alfonx 801 * @Return the major part of the software version or 0 if a problem occurs.
97 alfonx 800 */
98 alfonx 1019 public static String getVersionBuild(Class<?> clazz) {
99 alfonx 800 try {
100     final URL releasePropsURL = clazz
101     .getResource("/release.properties");
102    
103     final Properties releaseProps = new Properties();
104     final InputStream openStream = releasePropsURL.openStream();
105     try {
106     releaseProps.load(openStream);
107     } finally {
108     openStream.close();
109     }
110     final String str = releaseProps.getProperty("build", "0");
111    
112     if (str.equals("${buildNumber}")) {
113     // We are in development or Maven didn't filter the properties
114     // while building.
115 alfonx 1019 return "0";
116 alfonx 800 }
117    
118 alfonx 1019 return str;
119 alfonx 800 } catch (final Exception e) {
120 alfonx 1140 log.error(
121     "/release.properties could not be read from "
122     + clazz.getSimpleName(), e);
123 alfonx 1019 return "0";
124 alfonx 800 }
125    
126     }
127    
128     /**
129 alfonx 801 * @param clazz
130     * Pass a class that resides in the same "project" or jar, where
131     * the /release.properties resides as well.
132 alfonx 800 *
133 alfonx 801 * @return A Version String like "1.3" or "1.6". Returns "0.0" if a problem
134     * occurred.
135 alfonx 800 */
136     public static String getVersion(Class<?> clazz) {
137 alfonx 801 final String defaultVer = "0.0";
138    
139 alfonx 800 try {
140    
141     final URL releasePropsURL = clazz
142     .getResource("/release.properties");
143    
144     final Properties releaseProps = new Properties();
145     final InputStream openStream = releasePropsURL.openStream();
146     try {
147     releaseProps.load(openStream);
148     } finally {
149     openStream.close();
150     }
151    
152     final String versionProperty = releaseProps.getProperty("version",
153     defaultVer);
154     if (versionProperty.equals("${project.version}"))
155     return defaultVer;
156     return versionProperty;
157     } catch (final Exception e) {
158 alfonx 1140 log.error(
159     "/release.properties could not be read from "
160     + clazz.getSimpleName(), e);
161 alfonx 801 return defaultVer;
162 alfonx 800 }
163    
164     }
165    
166 alfonx 801 /**
167     * @param clazz
168     * Pass a class that resides in the same "project" or jar, where
169     * the /release.properties resides as well.
170     *
171     * @return Mayor version number part, e.g. "1" for version "1.3". Returns 0
172     * if a problem occurred.
173     */
174 alfonx 800 public static int getVersionMaj(Class<?> clazz) {
175     try {
176     return Integer.parseInt(getVersion(clazz).split("\\.")[0]);
177     } catch (final Exception e) {
178 alfonx 801 log.error("Major version number '" + getVersion(clazz)
179     + "' part could not be parsed from could not parsed (from "
180     + clazz.getSimpleName() + "). Must be numeric!", e);
181 alfonx 800 return 0;
182     }
183     }
184    
185 alfonx 801 /**
186     * @param clazz
187     * Pass a class that resides in the same "project" or jar, where
188     * the /release.properties resides as well.
189     *
190     * @return Minor version number part, e.g. "3" for version "1.3". Returns 0
191     * if a problem occurred.
192     */
193 alfonx 800 public static int getVersionMin(Class<?> clazz) {
194     try {
195 alfonx 813 return extractMinVersionFromString(getVersion(clazz));
196 alfonx 800 } catch (final Exception e) {
197 alfonx 801 log.error("Minor version number '" + getVersion(clazz)
198     + "' part could not be parsed from could not parsed (from "
199     + clazz.getSimpleName() + "). Must be numeric!", e);
200 alfonx 800 return 0;
201     }
202    
203     }
204    
205 alfonx 813 public static int extractMinVersionFromString(String versionString) {
206     Pattern minVersionPattern = Pattern.compile("\\d*\\.(\\d*).*");
207     Matcher matcher = minVersionPattern.matcher(versionString);
208     matcher.find();
209     return Integer.parseInt(matcher.group(1));
210     }
211    
212 alfonx 800 /**
213 alfonx 1140 * Different types of licenses supported.
214 alfonx 800 */
215 alfonx 1140 public enum License {
216     LGPL3, GPL3
217     };
218 alfonx 800
219     /**
220 alfonx 1140 * Print the GPL disclaimer to the given {@link Logger} on INFO level.
221 alfonx 801 *
222 alfonx 1140 * @param progname
223     * Name of the program as printed in the license. Will
224     * automatically be starting with an upper-case letter.
225     * <code>null</code> will fall-back to <code>This program</code>
226 alfonx 800 */
227 alfonx 1140 public static String getLicense(License l, String progname) {
228     if (progname == null || progname.isEmpty())
229     progname = "This program";
230     switch (l) {
231     case GPL3:
232     return ("\n"
233     + progname
234     + " is free software: you can redistribute it and/or modify\n"
235     + "it under the terms of the GNU General Public License as published by\n"
236     + "the Free Software Foundation, either version 3 of the License, or\n"
237     + "(at your option) any later version.\n"
238     + "\n"
239     + progname
240     + " is distributed in the hope that it will be useful,\n"
241     + "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
242     + "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" + "GNU General Public License for more details.\n");
243     case LGPL3:
244     return ("\n"
245     + progname
246     + " is free software: you can redistribute it and/or modify\n"
247     + "it under the terms of the GNU Lesser General Public License as published by\n"
248     + "the Free Software Foundation, either version 3 of the License, or\n"
249     + "(at your option) any later version.\n"
250     + "\n"
251     + progname
252     + " is distributed in the hope that it will be useful,\n"
253     + "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
254     + "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" + "GNU Lesser General Public License for more details.\n");
255     }
256     throw new IllegalArgumentException();
257 alfonx 800 }
258 alfonx 1137
259 alfonx 1185 public static boolean isSnapshot(Class<?> clazz) {
260 alfonx 1137 return getVersionInfo(clazz).contains("SNAPSHOT");
261     }
262 alfonx 800 }

Properties

Name Value
svn:eol-style native
svn:keywords Id URL
svn:mime-type text/plain

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26