/[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 1187 by alfonx, Wed Oct 27 22:46:45 2010 UTC
# Line 3  package skrueger.versionnumber; Line 3  package skrueger.versionnumber;
3  import java.io.InputStream;  import java.io.InputStream;
4  import java.net.URL;  import java.net.URL;
5  import java.util.Properties;  import java.util.Properties;
6    import java.util.regex.Matcher;
7    import java.util.regex.Pattern;
8    
9  import org.apache.log4j.Logger;  import org.apache.log4j.Logger;
10    
11  /**  /**
12   * Wer sein Maven Projekt wie folgt konfiguriert (oder ähnlich) kann die Maven +   * This class provides static utility classes that help releasing and versioning
13   * SVN revision nummer als Programmversion verwenden. <br/>   * applications. Especially usefull in combination with maven2's
14     * buildnumber-maven plugin.<br/>
15   *   *
16   * 1. pom.xml anpassen: <code>   * @author Stefan A. Tzeggai
17                          <plugin>   *
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                                            <plugin>
25                                  <groupId>org.codehaus.mojo</groupId>                                  <groupId>org.codehaus.mojo</groupId>
26                                  <artifactId>buildnumber-maven-plugin</artifactId>                                  <artifactId>buildnumber-maven-plugin</artifactId>
27                                  <version>1.0-beta-4</version>                                  <version>1.0-beta-4</version>
# Line 25  import org.apache.log4j.Logger; Line 35  import org.apache.log4j.Logger;
35                                          </execution>                                          </execution>
36                                  </executions>                                  </executions>
37                                  <configuration>                                  <configuration>
38                                                                            <!-- <doCheck>false</doCheck> <doUpdate>false</doUpdate> <providerImplementations>
39                                          <doCheck>false</doCheck>                                                  <svn>javasvn</svn> </providerImplementations> -->
40                                          <doUpdate>false</doUpdate>                                          <format>{0,date,yyyyMMddHHmm}</format>
41                                          <providerImplementations>                                          <items>
42                                                  <svn>javasvn</svn>                                                  <item>timestamp</item>
43                                          </providerImplementations>                                          </items>
44                                  </configuration>                                  </configuration>
45                          </plugin>                          </plugin>
46                  </code><br/>                  </code><br/>
47   * 2. Datei src/main/resources.properties mit foglenden Inhalt anlegen:<code>   *         2. Datei src/main/resources.properties mit foglenden Inhalt anlegen:
48  #Properties describing this release/build   *         <code>
49  version=${project.version}                                  #Properties describing this release/build
50  build=${buildNumber}                                  version=${project.version}
51  </code><br/>                                  build=${buildNumber}
52   * 3. Filtering für die src/main/resources.properties - Datei einschalten:<code>                  </code><br/>
53                  <resources>   *         3. Filtering für die src/main/resources.properties - Datei
54                          <resource>   *         einschalten:<code>
55                                  <filtering>true</filtering>   *        
56                                  <directory>src/main/resources</directory>                          <resources>
57                                  <includes><include>release.properties</include></includes>                                  <resource>
58                          </resource>                                          <filtering>true</filtering>
59                  </resources>                                          <directory>src/main/resources</directory>
60                                            <includes><include>release.properties</include></includes>
61  </code>                                  </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   */   */
69  public class ReleaseUtil {  public class ReleaseUtil {
70            private static Logger log = Logger.getLogger(ReleaseUtil.class);
71    
72          /**          /**
73           * @return The major.minor version, build number and build date           * Returns all (the most) information in one sting. This is a public method
74             * which never throws an Exception.
75             *
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           */           */
84          public static String getVersionInfo(Class<?> clazz) {          public static String getVersionInfo(Class<?> clazz) {
                 /**  
                  * Release properties einlesen  
                  */  
85                  try {                  try {
86                          return "v" + getVersion(clazz) + "-r" + getVersionBuild(clazz);                          return "v" + getVersion(clazz) + "-r" + getVersionBuild(clazz);
87                  } catch (final Exception e) {                  } catch (final Exception e) {
# Line 67  public class ReleaseUtil { Line 90  public class ReleaseUtil {
90          }          }
91    
92          /**          /**
93           * Return the major part of the software version of GP/AV/AS.           * @param clazz
94             *            Pass a class that resides in the same "project" or jar, where
95             *            the /release.properties resides as well.
96           *           *
97           * @throws Exception           * @Return the major part of the software version or 0 if a problem occurs.
          *             if release.properties not found  
98           */           */
99          public static int getVersionBuild(Class<?> clazz) {          public static String getVersionBuild(Class<?> clazz) {
100                  try {                  try {
101                          final URL releasePropsURL = clazz                          final URL releasePropsURL = clazz
102                                          .getResource("/release.properties");                                          .getResource("/release.properties");
# Line 89  public class ReleaseUtil { Line 113  public class ReleaseUtil {
113                          if (str.equals("${buildNumber}")) {                          if (str.equals("${buildNumber}")) {
114                                  // We are in development or Maven didn't filter the properties                                  // We are in development or Maven didn't filter the properties
115                                  // while building.                                  // while building.
116                                  return 0;                                  return "0";
117                          }                          }
118    
119                          return Integer.parseInt(str);                          return str;
120                  } catch (final Exception e) {                  } catch (final Exception e) {
121                          throw new RuntimeException(                          log.error(
122                                          "/release.properties could not be read from "                                          "/release.properties could not be read from "
123                                                          + clazz.getSimpleName(), e);                                                          + clazz.getSimpleName(), e);
124                            return "0";
125                  }                  }
126    
127          }          }
128    
129          /**          /**
130           * Return the major part of the software version of GP/AV/AS.           * @param clazz
131             *            Pass a class that resides in the same "project" or jar, where
132             *            the /release.properties resides as well.
133           *           *
134           * @throws Exception           * @return A Version String like "1.3" or "1.6". Returns "0.0" if a problem
135           *             if release.properties not found           *         occurred.
136           */           */
137          public static String getVersion(Class<?> clazz) {          public static String getVersion(Class<?> clazz) {
138                    final String defaultVer = "0.0";
139    
140                  try {                  try {
141    
142                          final URL releasePropsURL = clazz                          final URL releasePropsURL = clazz
# Line 121  public class ReleaseUtil { Line 150  public class ReleaseUtil {
150                                  openStream.close();                                  openStream.close();
151                          }                          }
152    
                         final String defaultVer = "DEV";  
153                          final String versionProperty = releaseProps.getProperty("version",                          final String versionProperty = releaseProps.getProperty("version",
154                                          defaultVer);                                          defaultVer);
155                          if (versionProperty.equals("${project.version}"))                          if (versionProperty.equals("${project.version}"))
156                                  return defaultVer;                                  return defaultVer;
157                          return versionProperty;                          return versionProperty;
158                  } catch (final Exception e) {                  } catch (final Exception e) {
159                          throw new RuntimeException(                          log.error(
160                                          "/release.properties could not be read from "                                          "/release.properties could not be read from "
161                                                          + clazz.getSimpleName(), e);                                                          + clazz.getSimpleName(), e);
162                            return defaultVer;
163                  }                  }
164    
165          }          }
166    
167            /**
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          public static int getVersionMaj(Class<?> clazz) {          public static int getVersionMaj(Class<?> clazz) {
176                  try {                  try {
177                          return Integer.parseInt(getVersion(clazz).split("\\.")[0]);                          return Integer.parseInt(getVersion(clazz).split("\\.")[0]);
178                  } catch (final Exception e) {                  } catch (final Exception e) {
179                            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                          return 0;                          return 0;
183                  }                  }
184          }          }
185    
186            /**
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          public static int getVersionMin(Class<?> clazz) {          public static int getVersionMin(Class<?> clazz) {
195                  try {                  try {
196                          return Integer.parseInt(getVersion(clazz).split("\\.")[1]);                          return extractMinVersionFromString(getVersion(clazz));
197                  } catch (final Exception e) {                  } catch (final Exception e) {
198                            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                          return 0;                          return 0;
202                  }                  }
203    
204          }          }
205    
206            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          /**          /**
214           * Print the GPL disclaimer to the given {@link Logger} as on INFO level.           * Different types of licenses supported.
215           */           */
216          public static void logGPLCopyright(final Logger logger) {          public enum License {
217                    LGPL3, GPL3
218                  logger          };
                                 .info("\nThis program is free software: you can redistribute it and/or modify\n"  
                                                 + "it under the terms of the GNU General Public License as published by\n"  
                                                 + "the Free Software Foundation, either version 3 of the License, or\n"  
                                                 + "(at your option) any later version.\n"  
                                                 + "\n"  
                                                 + "This program is distributed in the hope that it will be useful,\n"  
                                                 + "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"  
                                                 + "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"  
                                                 + "GNU General Public License for more details.\n");  
         }  
219    
220          /**          /**
221           * Print the LGPL disclaimer to the given {@link Logger} as on INFO level.           * Print the GPL disclaimer to the given {@link Logger} on INFO level.
222           */           *
223          public static void logLGPLCopyright(final Logger logger) {           * @param progname
224             *            Name of the program as printed in the license. Will
225                  logger           *            automatically be starting with an upper-case letter.
226                                  .info("\nThis program is free software: you can redistribute it and/or modify\n"           *            <code>null</code> will fall-back to <code>This program</code>
227                                                  + "it under the terms of the GNU Lesser General Public License as published by\n"           */
228                                                  + "the Free Software Foundation, either version 3 of the License, or\n"          public static String getLicense(License l, String progname) {
229                                                  + "(at your option) any later version.\n"                  if (progname == null || progname.isEmpty())
230                                                  + "\n"                          progname = "This program";
231                                                  + "This program is distributed in the hope that it will be useful,\n"                  switch (l) {
232                                                  + "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"                  case GPL3:
233                                                  + "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"                          return ("\n"
234                                                  + "GNU Lesser General Public License for more details.\n");                                          + 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            }
259    
260            public static boolean isSnapshot(Class<?> clazz) {
261                    return getVersionInfo(clazz).contains("SNAPSHOT");
262          }          }
263  }  }

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26