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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 801 - (hide annotations)
Wed Apr 14 19:30:11 2010 UTC (14 years, 10 months ago) by alfonx
Original Path: trunk/src/skrueger/versionnumber/ReleaseUtil.java
File MIME type: text/plain
File size: 7771 byte(s)
Added comments to new ReleaseUtil class
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 alfonx 801 * This class provides static utility classes that help releasing and versioning
11     * applications. Especially usefull in combination with maven2's
12     * buildnumber-maven plugin.<br/>
13 alfonx 800 *
14 alfonx 801 * @author Stefan A. Tzeggai
15     *
16     * Wer sein Maven Projekt wie folgt konfiguriert kann die Maven +
17     * SVN-Revisionnummer als Programmversion verwenden, z.B.
18     * "v1.5-SNAPSHOT r123"
19     *
20     * <br/>
21     * 1. pom.xml anpassen: <code>
22 alfonx 800 <plugin>
23     <groupId>org.codehaus.mojo</groupId>
24     <artifactId>buildnumber-maven-plugin</artifactId>
25     <version>1.0-beta-4</version>
26     <executions>
27     <execution>
28     <id>buildnumber in phase initialize</id>
29     <phase>initialize</phase>
30     <goals>
31     <goal>create</goal>
32     </goals>
33     </execution>
34     </executions>
35     <configuration>
36    
37     <doCheck>false</doCheck>
38     <doUpdate>false</doUpdate>
39     <providerImplementations>
40     <svn>javasvn</svn>
41     </providerImplementations>
42     </configuration>
43     </plugin>
44     </code><br/>
45 alfonx 801 * 2. Datei src/main/resources.properties mit foglenden Inhalt anlegen:
46     * <code>
47     #Properties describing this release/build
48     version=${project.version}
49     build=${buildNumber}
50     </code><br/>
51     * 3. Filtering für die src/main/resources.properties - Datei
52     * einschalten:<code>
53     *
54     <resources>
55     <resource>
56     <filtering>true</filtering>
57     <directory>src/main/resources</directory>
58     <includes><include>release.properties</include></includes>
59     </resource>
60     </resources>
61     </code> <br/>
62     * <br/>
63     * Eine maven Fehlermeldung "The scm url cannot be null." ist
64     * irreführend. In wirklichkeit wird ein Eintrag
65     * <scm>...<developerConnection>..</scm> verlangt.
66 alfonx 800 */
67     public class ReleaseUtil {
68 alfonx 801 private static Logger log = Logger.getLogger(ReleaseUtil.class);
69 alfonx 800
70     /**
71 alfonx 801 * This is the main public API method which never throws an exception.
72     *
73     * @param clazz
74     * Pass a class that resides in the same "project" or jar, where
75     * the /release.properties resides as well.
76     *
77     * @return A {@link String} like "v1.4 r243" where the first number is the
78     * maven project version and the second number is the SCM revision.
79     * Returns "unknown version" if it can't be determined.
80 alfonx 800 */
81     public static String getVersionInfo(Class<?> clazz) {
82     try {
83     return "v" + getVersion(clazz) + "-r" + getVersionBuild(clazz);
84     } catch (final Exception e) {
85     return "unknown version";
86     }
87     }
88    
89     /**
90 alfonx 801 * @param clazz
91     * Pass a class that resides in the same "project" or jar, where
92     * the /release.properties resides as well.
93 alfonx 800 *
94 alfonx 801 * @Return the major part of the software version or 0 if a problem occurs.
95 alfonx 800 */
96     public static int getVersionBuild(Class<?> clazz) {
97     try {
98     final URL releasePropsURL = clazz
99     .getResource("/release.properties");
100    
101     final Properties releaseProps = new Properties();
102     final InputStream openStream = releasePropsURL.openStream();
103     try {
104     releaseProps.load(openStream);
105     } finally {
106     openStream.close();
107     }
108     final String str = releaseProps.getProperty("build", "0");
109    
110     if (str.equals("${buildNumber}")) {
111     // We are in development or Maven didn't filter the properties
112     // while building.
113     return 0;
114     }
115    
116     return Integer.parseInt(str);
117     } catch (final Exception e) {
118 alfonx 801 log.error("/release.properties could not be read from "
119     + clazz.getSimpleName(), e);
120     return 0;
121 alfonx 800 }
122    
123     }
124    
125     /**
126 alfonx 801 * @param clazz
127     * Pass a class that resides in the same "project" or jar, where
128     * the /release.properties resides as well.
129 alfonx 800 *
130 alfonx 801 * @return A Version String like "1.3" or "1.6". Returns "0.0" if a problem
131     * occurred.
132 alfonx 800 */
133     public static String getVersion(Class<?> clazz) {
134 alfonx 801 final String defaultVer = "0.0";
135    
136 alfonx 800 try {
137    
138     final URL releasePropsURL = clazz
139     .getResource("/release.properties");
140    
141     final Properties releaseProps = new Properties();
142     final InputStream openStream = releasePropsURL.openStream();
143     try {
144     releaseProps.load(openStream);
145     } finally {
146     openStream.close();
147     }
148    
149     final String versionProperty = releaseProps.getProperty("version",
150     defaultVer);
151     if (versionProperty.equals("${project.version}"))
152     return defaultVer;
153     return versionProperty;
154     } catch (final Exception e) {
155 alfonx 801 log.error("/release.properties could not be read from "
156     + clazz.getSimpleName(), e);
157     return defaultVer;
158 alfonx 800 }
159    
160     }
161    
162 alfonx 801 /**
163     * @param clazz
164     * Pass a class that resides in the same "project" or jar, where
165     * the /release.properties resides as well.
166     *
167     * @return Mayor version number part, e.g. "1" for version "1.3". Returns 0
168     * if a problem occurred.
169     */
170 alfonx 800 public static int getVersionMaj(Class<?> clazz) {
171     try {
172     return Integer.parseInt(getVersion(clazz).split("\\.")[0]);
173     } catch (final Exception e) {
174 alfonx 801 log.error("Major version number '" + getVersion(clazz)
175     + "' part could not be parsed from could not parsed (from "
176     + clazz.getSimpleName() + "). Must be numeric!", e);
177 alfonx 800 return 0;
178     }
179     }
180    
181 alfonx 801 /**
182     * @param clazz
183     * Pass a class that resides in the same "project" or jar, where
184     * the /release.properties resides as well.
185     *
186     * @return Minor version number part, e.g. "3" for version "1.3". Returns 0
187     * if a problem occurred.
188     */
189 alfonx 800 public static int getVersionMin(Class<?> clazz) {
190     try {
191     return Integer.parseInt(getVersion(clazz).split("\\.")[1]);
192     } catch (final Exception e) {
193 alfonx 801 log.error("Minor version number '" + getVersion(clazz)
194     + "' part could not be parsed from could not parsed (from "
195     + clazz.getSimpleName() + "). Must be numeric!", e);
196 alfonx 800 return 0;
197     }
198    
199     }
200    
201     /**
202 alfonx 801 * @param clazz
203     * Pass a class that resides in the same "project" or jar, where
204     * the /release.properties resides as well.
205     *
206     * Print the GPL disclaimer to the given {@link Logger} on INFO
207     * level.
208 alfonx 800 */
209     public static void logGPLCopyright(final Logger logger) {
210     logger
211     .info("\nThis program is free software: you can redistribute it and/or modify\n"
212     + "it under the terms of the GNU General Public License as published by\n"
213     + "the Free Software Foundation, either version 3 of the License, or\n"
214     + "(at your option) any later version.\n"
215     + "\n"
216     + "This program is distributed in the hope that it will be useful,\n"
217     + "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
218     + "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
219     + "GNU General Public License for more details.\n");
220     }
221    
222     /**
223 alfonx 801 * @param clazz
224     * Pass a class that resides in the same "project" or jar, where
225     * the /release.properties resides as well.
226     *
227     * Print the LGPL disclaimer to the given {@link Logger} on INFO
228     * level.
229 alfonx 800 */
230     public static void logLGPLCopyright(final Logger logger) {
231     logger
232     .info("\nThis program is free software: you can redistribute it and/or modify\n"
233     + "it under the terms of the GNU Lesser General Public License as published by\n"
234     + "the Free Software Foundation, either version 3 of the License, or\n"
235     + "(at your option) any later version.\n"
236     + "\n"
237     + "This program is distributed in the hope that it will be useful,\n"
238     + "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
239     + "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
240     + "GNU Lesser General Public License for more details.\n");
241     }
242     }

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