/[schmitzm]/trunk/src/skrueger/versionnumber/ReleaseUtil.java
ViewVC logotype

Annotation of /trunk/src/skrueger/versionnumber/ReleaseUtil.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1187 - (hide annotations)
Wed Oct 27 22:46:45 2010 UTC (14 years, 4 months ago) by alfonx
File MIME type: text/plain
File size: 8327 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 1187 * Returns all (the most) information in one sting. This is a public method
74     * which never throws an Exception.
75 alfonx 801 *
76     * @param clazz
77     * Pass a class that resides in the same "project" or jar, where
78     * the /release.properties resides as well.
79     *
80     * @return A {@link String} like "v1.4 r243" where the first number is the
81     * maven project version and the second number is the SCM revision.
82     * Returns "unknown version" if it can't be determined.
83 alfonx 800 */
84     public static String getVersionInfo(Class<?> clazz) {
85     try {
86     return "v" + getVersion(clazz) + "-r" + getVersionBuild(clazz);
87     } catch (final Exception e) {
88     return "unknown version";
89     }
90     }
91    
92     /**
93 alfonx 801 * @param clazz
94     * Pass a class that resides in the same "project" or jar, where
95     * the /release.properties resides as well.
96 alfonx 800 *
97 alfonx 801 * @Return the major part of the software version or 0 if a problem occurs.
98 alfonx 800 */
99 alfonx 1019 public static String getVersionBuild(Class<?> clazz) {
100 alfonx 800 try {
101     final URL releasePropsURL = clazz
102     .getResource("/release.properties");
103    
104     final Properties releaseProps = new Properties();
105     final InputStream openStream = releasePropsURL.openStream();
106     try {
107     releaseProps.load(openStream);
108     } finally {
109     openStream.close();
110     }
111     final String str = releaseProps.getProperty("build", "0");
112    
113     if (str.equals("${buildNumber}")) {
114     // We are in development or Maven didn't filter the properties
115     // while building.
116 alfonx 1019 return "0";
117 alfonx 800 }
118    
119 alfonx 1019 return str;
120 alfonx 800 } catch (final Exception e) {
121 alfonx 1140 log.error(
122     "/release.properties could not be read from "
123     + clazz.getSimpleName(), e);
124 alfonx 1019 return "0";
125 alfonx 800 }
126    
127     }
128    
129     /**
130 alfonx 801 * @param clazz
131     * Pass a class that resides in the same "project" or jar, where
132     * the /release.properties resides as well.
133 alfonx 800 *
134 alfonx 801 * @return A Version String like "1.3" or "1.6". Returns "0.0" if a problem
135     * occurred.
136 alfonx 800 */
137     public static String getVersion(Class<?> clazz) {
138 alfonx 801 final String defaultVer = "0.0";
139    
140 alfonx 800 try {
141    
142     final URL releasePropsURL = clazz
143     .getResource("/release.properties");
144    
145     final Properties releaseProps = new Properties();
146     final InputStream openStream = releasePropsURL.openStream();
147     try {
148     releaseProps.load(openStream);
149     } finally {
150     openStream.close();
151     }
152    
153     final String versionProperty = releaseProps.getProperty("version",
154     defaultVer);
155     if (versionProperty.equals("${project.version}"))
156     return defaultVer;
157     return versionProperty;
158     } catch (final Exception e) {
159 alfonx 1140 log.error(
160     "/release.properties could not be read from "
161     + clazz.getSimpleName(), e);
162 alfonx 801 return defaultVer;
163 alfonx 800 }
164    
165     }
166    
167 alfonx 801 /**
168     * @param clazz
169     * Pass a class that resides in the same "project" or jar, where
170     * the /release.properties resides as well.
171     *
172     * @return Mayor version number part, e.g. "1" for version "1.3". Returns 0
173     * if a problem occurred.
174     */
175 alfonx 800 public static int getVersionMaj(Class<?> clazz) {
176     try {
177     return Integer.parseInt(getVersion(clazz).split("\\.")[0]);
178     } catch (final Exception e) {
179 alfonx 801 log.error("Major version number '" + getVersion(clazz)
180     + "' part could not be parsed from could not parsed (from "
181     + clazz.getSimpleName() + "). Must be numeric!", e);
182 alfonx 800 return 0;
183     }
184     }
185    
186 alfonx 801 /**
187     * @param clazz
188     * Pass a class that resides in the same "project" or jar, where
189     * the /release.properties resides as well.
190     *
191     * @return Minor version number part, e.g. "3" for version "1.3". Returns 0
192     * if a problem occurred.
193     */
194 alfonx 800 public static int getVersionMin(Class<?> clazz) {
195     try {
196 alfonx 813 return extractMinVersionFromString(getVersion(clazz));
197 alfonx 800 } catch (final Exception e) {
198 alfonx 801 log.error("Minor version number '" + getVersion(clazz)
199     + "' part could not be parsed from could not parsed (from "
200     + clazz.getSimpleName() + "). Must be numeric!", e);
201 alfonx 800 return 0;
202     }
203    
204     }
205    
206 alfonx 813 public static int extractMinVersionFromString(String versionString) {
207     Pattern minVersionPattern = Pattern.compile("\\d*\\.(\\d*).*");
208     Matcher matcher = minVersionPattern.matcher(versionString);
209     matcher.find();
210     return Integer.parseInt(matcher.group(1));
211     }
212    
213 alfonx 800 /**
214 alfonx 1140 * Different types of licenses supported.
215 alfonx 800 */
216 alfonx 1140 public enum License {
217     LGPL3, GPL3
218     };
219 alfonx 800
220     /**
221 alfonx 1140 * Print the GPL disclaimer to the given {@link Logger} on INFO level.
222 alfonx 801 *
223 alfonx 1140 * @param progname
224     * Name of the program as printed in the license. Will
225     * automatically be starting with an upper-case letter.
226     * <code>null</code> will fall-back to <code>This program</code>
227 alfonx 800 */
228 alfonx 1140 public static String getLicense(License l, String progname) {
229     if (progname == null || progname.isEmpty())
230     progname = "This program";
231     switch (l) {
232     case GPL3:
233     return ("\n"
234     + progname
235     + " is free software: you can redistribute it and/or modify\n"
236     + "it under the terms of the GNU General Public License as published by\n"
237     + "the Free Software Foundation, either version 3 of the License, or\n"
238     + "(at your option) any later version.\n"
239     + "\n"
240     + progname
241     + " is distributed in the hope that it will be useful,\n"
242     + "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
243     + "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" + "GNU General Public License for more details.\n");
244     case LGPL3:
245     return ("\n"
246     + progname
247     + " is free software: you can redistribute it and/or modify\n"
248     + "it under the terms of the GNU Lesser General Public License as published by\n"
249     + "the Free Software Foundation, either version 3 of the License, or\n"
250     + "(at your option) any later version.\n"
251     + "\n"
252     + progname
253     + " is distributed in the hope that it will be useful,\n"
254     + "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
255     + "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" + "GNU Lesser General Public License for more details.\n");
256     }
257     throw new IllegalArgumentException();
258 alfonx 800 }
259 alfonx 1137
260 alfonx 1185 public static boolean isSnapshot(Class<?> clazz) {
261 alfonx 1137 return getVersionInfo(clazz).contains("SNAPSHOT");
262     }
263 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