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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 800 by alfonx, Wed Apr 14 16:33:30 2010 UTC revision 801 by alfonx, Wed Apr 14 19:30:11 2010 UTC
# Line 7  import java.util.Properties; Line 7  import java.util.Properties;
7  import org.apache.log4j.Logger;  import org.apache.log4j.Logger;
8    
9  /**  /**
10   * Wer sein Maven Projekt wie folgt konfiguriert (oder ähnlich) kann die Maven +   * This class provides static utility classes that help releasing and versioning
11   * SVN revision nummer als Programmversion verwenden. <br/>   * applications. Especially usefull in combination with maven2's
12     * buildnumber-maven plugin.<br/>
13   *   *
14   * 1. pom.xml anpassen: <code>   * @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                          <plugin>                          <plugin>
23                                  <groupId>org.codehaus.mojo</groupId>                                  <groupId>org.codehaus.mojo</groupId>
24                                  <artifactId>buildnumber-maven-plugin</artifactId>                                  <artifactId>buildnumber-maven-plugin</artifactId>
# Line 34  import org.apache.log4j.Logger; Line 42  import org.apache.log4j.Logger;
42                                  </configuration>                                  </configuration>
43                          </plugin>                          </plugin>
44                  </code><br/>                  </code><br/>
45   * 2. Datei src/main/resources.properties mit foglenden Inhalt anlegen:<code>   *         2. Datei src/main/resources.properties mit foglenden Inhalt anlegen:
46  #Properties describing this release/build   *         <code>
47  version=${project.version}                                  #Properties describing this release/build
48  build=${buildNumber}                                  version=${project.version}
49  </code><br/>                                  build=${buildNumber}
50   * 3. Filtering für die src/main/resources.properties - Datei einschalten:<code>                  </code><br/>
51                  <resources>   *         3. Filtering für die src/main/resources.properties - Datei
52                          <resource>   *         einschalten:<code>
53                                  <filtering>true</filtering>   *        
54                                  <directory>src/main/resources</directory>                          <resources>
55                                  <includes><include>release.properties</include></includes>                                  <resource>
56                          </resource>                                          <filtering>true</filtering>
57                  </resources>                                          <directory>src/main/resources</directory>
58                                            <includes><include>release.properties</include></includes>
59  </code>                                  </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   */   */
67  public class ReleaseUtil {  public class ReleaseUtil {
68            private static Logger log = Logger.getLogger(ReleaseUtil.class);
69    
70          /**          /**
71           * @return The major.minor version, build number and build date           * 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           */           */
81          public static String getVersionInfo(Class<?> clazz) {          public static String getVersionInfo(Class<?> clazz) {
                 /**  
                  * Release properties einlesen  
                  */  
82                  try {                  try {
83                          return "v" + getVersion(clazz) + "-r" + getVersionBuild(clazz);                          return "v" + getVersion(clazz) + "-r" + getVersionBuild(clazz);
84                  } catch (final Exception e) {                  } catch (final Exception e) {
# Line 67  public class ReleaseUtil { Line 87  public class ReleaseUtil {
87          }          }
88    
89          /**          /**
90           * Return the major part of the software version of GP/AV/AS.           * @param clazz
91             *            Pass a class that resides in the same "project" or jar, where
92             *            the /release.properties resides as well.
93           *           *
94           * @throws Exception           * @Return the major part of the software version or 0 if a problem occurs.
          *             if release.properties not found  
95           */           */
96          public static int getVersionBuild(Class<?> clazz) {          public static int getVersionBuild(Class<?> clazz) {
97                  try {                  try {
# Line 94  public class ReleaseUtil { Line 115  public class ReleaseUtil {
115    
116                          return Integer.parseInt(str);                          return Integer.parseInt(str);
117                  } catch (final Exception e) {                  } catch (final Exception e) {
118                          throw new RuntimeException(                          log.error("/release.properties could not be read from "
119                                          "/release.properties could not be read from "                                          + clazz.getSimpleName(), e);
120                                                          + clazz.getSimpleName(), e);                          return 0;
121                  }                  }
122    
123          }          }
124    
125          /**          /**
126           * Return the major part of the software version of GP/AV/AS.           * @param clazz
127             *            Pass a class that resides in the same "project" or jar, where
128             *            the /release.properties resides as well.
129           *           *
130           * @throws Exception           * @return A Version String like "1.3" or "1.6". Returns "0.0" if a problem
131           *             if release.properties not found           *         occurred.
132           */           */
133          public static String getVersion(Class<?> clazz) {          public static String getVersion(Class<?> clazz) {
134                    final String defaultVer = "0.0";
135    
136                  try {                  try {
137    
138                          final URL releasePropsURL = clazz                          final URL releasePropsURL = clazz
# Line 121  public class ReleaseUtil { Line 146  public class ReleaseUtil {
146                                  openStream.close();                                  openStream.close();
147                          }                          }
148    
                         final String defaultVer = "DEV";  
149                          final String versionProperty = releaseProps.getProperty("version",                          final String versionProperty = releaseProps.getProperty("version",
150                                          defaultVer);                                          defaultVer);
151                          if (versionProperty.equals("${project.version}"))                          if (versionProperty.equals("${project.version}"))
152                                  return defaultVer;                                  return defaultVer;
153                          return versionProperty;                          return versionProperty;
154                  } catch (final Exception e) {                  } catch (final Exception e) {
155                          throw new RuntimeException(                          log.error("/release.properties could not be read from "
156                                          "/release.properties could not be read from "                                          + clazz.getSimpleName(), e);
157                                                          + clazz.getSimpleName(), e);                          return defaultVer;
158                  }                  }
159    
160          }          }
161    
162            /**
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          public static int getVersionMaj(Class<?> clazz) {          public static int getVersionMaj(Class<?> clazz) {
171                  try {                  try {
172                          return Integer.parseInt(getVersion(clazz).split("\\.")[0]);                          return Integer.parseInt(getVersion(clazz).split("\\.")[0]);
173                  } catch (final Exception e) {                  } catch (final Exception e) {
174                            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                          return 0;                          return 0;
178                  }                  }
179          }          }
180    
181            /**
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          public static int getVersionMin(Class<?> clazz) {          public static int getVersionMin(Class<?> clazz) {
190                  try {                  try {
191                          return Integer.parseInt(getVersion(clazz).split("\\.")[1]);                          return Integer.parseInt(getVersion(clazz).split("\\.")[1]);
192                  } catch (final Exception e) {                  } catch (final Exception e) {
193                            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                          return 0;                          return 0;
197                  }                  }
198    
199          }          }
200    
201          /**          /**
202           * Print the GPL disclaimer to the given {@link Logger} as on INFO level.           * @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           */           */
209          public static void logGPLCopyright(final Logger logger) {          public static void logGPLCopyright(final Logger logger) {
   
210                  logger                  logger
211                                  .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"
212                                                  + "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"
# Line 170  public class ReleaseUtil { Line 220  public class ReleaseUtil {
220          }          }
221    
222          /**          /**
223           * Print the LGPL disclaimer to the given {@link Logger} as on INFO level.           * @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           */           */
230          public static void logLGPLCopyright(final Logger logger) {          public static void logLGPLCopyright(final Logger logger) {
   
231                  logger                  logger
232                                  .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"
233                                                  + "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"
# Line 184  public class ReleaseUtil { Line 238  public class ReleaseUtil {
238                                                  + "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"                                                  + "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
239                                                  + "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"                                                  + "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
240                                                  + "GNU Lesser General Public License for more details.\n");                                                  + "GNU Lesser General Public License for more details.\n");
   
241          }          }
242  }  }

Legend:
Removed from v.800  
changed lines
  Added in v.801

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26