/[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 1019 - (hide annotations)
Mon Sep 20 17:41:04 2010 UTC (14 years, 5 months ago) by alfonx
File MIME type: text/plain
File size: 8085 byte(s)
Some more tuning with the release number.. it's now a string 
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 800 <plugin>
25     <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    
39     <doCheck>false</doCheck>
40     <doUpdate>false</doUpdate>
41     <providerImplementations>
42     <svn>javasvn</svn>
43     </providerImplementations>
44     </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 801 log.error("/release.properties could not be read from "
121     + clazz.getSimpleName(), e);
122 alfonx 1019 return "0";
123 alfonx 800 }
124    
125     }
126    
127     /**
128 alfonx 801 * @param clazz
129     * Pass a class that resides in the same "project" or jar, where
130     * the /release.properties resides as well.
131 alfonx 800 *
132 alfonx 801 * @return A Version String like "1.3" or "1.6". Returns "0.0" if a problem
133     * occurred.
134 alfonx 800 */
135     public static String getVersion(Class<?> clazz) {
136 alfonx 801 final String defaultVer = "0.0";
137    
138 alfonx 800 try {
139    
140     final URL releasePropsURL = clazz
141     .getResource("/release.properties");
142    
143     final Properties releaseProps = new Properties();
144     final InputStream openStream = releasePropsURL.openStream();
145     try {
146     releaseProps.load(openStream);
147     } finally {
148     openStream.close();
149     }
150    
151     final String versionProperty = releaseProps.getProperty("version",
152     defaultVer);
153     if (versionProperty.equals("${project.version}"))
154     return defaultVer;
155     return versionProperty;
156     } catch (final Exception e) {
157 alfonx 801 log.error("/release.properties could not be read from "
158     + clazz.getSimpleName(), e);
159     return defaultVer;
160 alfonx 800 }
161    
162     }
163    
164 alfonx 801 /**
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 alfonx 800 public static int getVersionMaj(Class<?> clazz) {
173     try {
174     return Integer.parseInt(getVersion(clazz).split("\\.")[0]);
175     } catch (final Exception e) {
176 alfonx 801 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 alfonx 800 return 0;
180     }
181     }
182    
183 alfonx 801 /**
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 alfonx 800 public static int getVersionMin(Class<?> clazz) {
192     try {
193 alfonx 813 return extractMinVersionFromString(getVersion(clazz));
194 alfonx 800 } catch (final Exception e) {
195 alfonx 801 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 alfonx 800 return 0;
199     }
200    
201     }
202    
203 alfonx 813 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 alfonx 800 /**
211 alfonx 801 * @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 alfonx 800 */
218     public static void logGPLCopyright(final Logger logger) {
219     logger
220     .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"
222     + "the Free Software Foundation, either version 3 of the License, or\n"
223     + "(at your option) any later version.\n"
224     + "\n"
225     + "This program is distributed in the hope that it will be useful,\n"
226     + "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
227     + "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
228     + "GNU General Public License for more details.\n");
229     }
230    
231     /**
232 alfonx 801 * @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 alfonx 800 */
239     public static void logLGPLCopyright(final Logger logger) {
240     logger
241     .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"
243     + "the Free Software Foundation, either version 3 of the License, or\n"
244     + "(at your option) any later version.\n"
245     + "\n"
246     + "This program is distributed in the hope that it will be useful,\n"
247     + "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
248     + "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
249     + "GNU Lesser General Public License for more details.\n");
250     }
251     }

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