[Schmitzm-commits] r801 - trunk/src/skrueger/versionnumber

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Wed Apr 14 21:30:12 CEST 2010


Author: alfonx
Date: 2010-04-14 21:30:11 +0200 (Wed, 14 Apr 2010)
New Revision: 801

Modified:
   trunk/src/skrueger/versionnumber/ReleaseUtil.java
Log:
Added comments to new ReleaseUtil class

Modified: trunk/src/skrueger/versionnumber/ReleaseUtil.java
===================================================================
--- trunk/src/skrueger/versionnumber/ReleaseUtil.java	2010-04-14 16:33:30 UTC (rev 800)
+++ trunk/src/skrueger/versionnumber/ReleaseUtil.java	2010-04-14 19:30:11 UTC (rev 801)
@@ -7,10 +7,18 @@
 import org.apache.log4j.Logger;
 
 /**
- * Wer sein Maven Projekt wie folgt konfiguriert (oder ähnlich) kann die Maven +
- * SVN revision nummer als Programmversion verwenden. <br/>
+ * This class provides static utility classes that help releasing and versioning
+ * applications. Especially usefull in combination with maven2's
+ * buildnumber-maven plugin.<br/>
  * 
- * 1. pom.xml anpassen: <code>
+ * @author Stefan A. Tzeggai
+ * 
+ *         Wer sein Maven Projekt wie folgt konfiguriert kann die Maven +
+ *         SVN-Revisionnummer als Programmversion verwenden, z.B.
+ *         "v1.5-SNAPSHOT r123"
+ * 
+ * <br/>
+ *         1. pom.xml anpassen: <code>
 			<plugin>
 				<groupId>org.codehaus.mojo</groupId>
 				<artifactId>buildnumber-maven-plugin</artifactId>
@@ -34,31 +42,43 @@
 				</configuration>
 			</plugin>
 		</code><br/>
- * 2. Datei src/main/resources.properties mit foglenden Inhalt anlegen:<code>
-#Properties describing this release/build
-version=${project.version}
-build=${buildNumber}
-</code><br/>
- * 3. Filtering für die src/main/resources.properties - Datei einschalten:<code>
-		<resources>
-			<resource>
-				<filtering>true</filtering>
-				<directory>src/main/resources</directory>
-				<includes><include>release.properties</include></includes>
-			</resource>
-		</resources>
-
-</code>
+ *         2. Datei src/main/resources.properties mit foglenden Inhalt anlegen:
+ *         <code>
+				#Properties describing this release/build
+				version=${project.version}
+				build=${buildNumber}
+		</code><br/>
+ *         3. Filtering für die src/main/resources.properties - Datei
+ *         einschalten:<code>
+ *         
+			<resources>
+				<resource>
+					<filtering>true</filtering>
+					<directory>src/main/resources</directory>
+					<includes><include>release.properties</include></includes>
+				</resource>
+			</resources>
+	    </code> <br/>
+ * <br/>
+ *         Eine maven Fehlermeldung "The scm url cannot be null." ist
+ *         irreführend. In wirklichkeit wird ein Eintrag
+ *         <scm>...<developerConnection>..</scm> verlangt.
  */
 public class ReleaseUtil {
+	private static Logger log = Logger.getLogger(ReleaseUtil.class);
 
 	/**
-	 * @return The major.minor version, build number and build date
+	 * This is the main public API method which never throws an exception.
+	 * 
+	 * @param clazz
+	 *            Pass a class that resides in the same "project" or jar, where
+	 *            the /release.properties resides as well.
+	 * 
+	 * @return A {@link String} like "v1.4 r243" where the first number is the
+	 *         maven project version and the second number is the SCM revision.
+	 *         Returns "unknown version" if it can't be determined.
 	 */
 	public static String getVersionInfo(Class<?> clazz) {
-		/**
-		 * Release properties einlesen
-		 */
 		try {
 			return "v" + getVersion(clazz) + "-r" + getVersionBuild(clazz);
 		} catch (final Exception e) {
@@ -67,10 +87,11 @@
 	}
 
 	/**
-	 * Return the major part of the software version of GP/AV/AS.
+	 * @param clazz
+	 *            Pass a class that resides in the same "project" or jar, where
+	 *            the /release.properties resides as well.
 	 * 
-	 * @throws Exception
-	 *             if release.properties not found
+	 * @Return the major part of the software version or 0 if a problem occurs.
 	 */
 	public static int getVersionBuild(Class<?> clazz) {
 		try {
@@ -94,20 +115,24 @@
 
 			return Integer.parseInt(str);
 		} catch (final Exception e) {
-			throw new RuntimeException(
-					"/release.properties could not be read from "
-							+ clazz.getSimpleName(), e);
+			log.error("/release.properties could not be read from "
+					+ clazz.getSimpleName(), e);
+			return 0;
 		}
 
 	}
 
 	/**
-	 * Return the major part of the software version of GP/AV/AS.
+	 * @param clazz
+	 *            Pass a class that resides in the same "project" or jar, where
+	 *            the /release.properties resides as well.
 	 * 
-	 * @throws Exception
-	 *             if release.properties not found
+	 * @return A Version String like "1.3" or "1.6". Returns "0.0" if a problem
+	 *         occurred.
 	 */
 	public static String getVersion(Class<?> clazz) {
+		final String defaultVer = "0.0";
+
 		try {
 
 			final URL releasePropsURL = clazz
@@ -121,42 +146,67 @@
 				openStream.close();
 			}
 
-			final String defaultVer = "DEV";
 			final String versionProperty = releaseProps.getProperty("version",
 					defaultVer);
 			if (versionProperty.equals("${project.version}"))
 				return defaultVer;
 			return versionProperty;
 		} catch (final Exception e) {
-			throw new RuntimeException(
-					"/release.properties could not be read from "
-							+ clazz.getSimpleName(), e);
+			log.error("/release.properties could not be read from "
+					+ clazz.getSimpleName(), e);
+			return defaultVer;
 		}
 
 	}
 
+	/**
+	 * @param clazz
+	 *            Pass a class that resides in the same "project" or jar, where
+	 *            the /release.properties resides as well.
+	 * 
+	 * @return Mayor version number part, e.g. "1" for version "1.3". Returns 0
+	 *         if a problem occurred.
+	 */
 	public static int getVersionMaj(Class<?> clazz) {
 		try {
 			return Integer.parseInt(getVersion(clazz).split("\\.")[0]);
 		} catch (final Exception e) {
+			log.error("Major version number '" + getVersion(clazz)
+					+ "' part could not be parsed from could not parsed (from "
+					+ clazz.getSimpleName() + "). Must be numeric!", e);
 			return 0;
 		}
 	}
 
+	/**
+	 * @param clazz
+	 *            Pass a class that resides in the same "project" or jar, where
+	 *            the /release.properties resides as well.
+	 * 
+	 * @return Minor version number part, e.g. "3" for version "1.3". Returns 0
+	 *         if a problem occurred.
+	 */
 	public static int getVersionMin(Class<?> clazz) {
 		try {
 			return Integer.parseInt(getVersion(clazz).split("\\.")[1]);
 		} catch (final Exception e) {
+			log.error("Minor version number '" + getVersion(clazz)
+					+ "' part could not be parsed from could not parsed (from "
+					+ clazz.getSimpleName() + "). Must be numeric!", e);
 			return 0;
 		}
 
 	}
 
 	/**
-	 * Print the GPL disclaimer to the given {@link Logger} as on INFO level.
+	 * @param clazz
+	 *            Pass a class that resides in the same "project" or jar, where
+	 *            the /release.properties resides as well.
+	 * 
+	 *            Print the GPL disclaimer to the given {@link Logger} on INFO
+	 *            level.
 	 */
 	public static void logGPLCopyright(final Logger logger) {
-
 		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"
@@ -170,10 +220,14 @@
 	}
 
 	/**
-	 * Print the LGPL disclaimer to the given {@link Logger} as on INFO level.
+	 * @param clazz
+	 *            Pass a class that resides in the same "project" or jar, where
+	 *            the /release.properties resides as well.
+	 * 
+	 *            Print the LGPL disclaimer to the given {@link Logger} on INFO
+	 *            level.
 	 */
 	public static void logLGPLCopyright(final Logger logger) {
-
 		logger
 				.info("\nThis program is free software: you can redistribute it and/or modify\n"
 						+ "it under the terms of the GNU Lesser General Public License as published by\n"
@@ -184,6 +238,5 @@
 						+ "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
 						+ "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
 						+ "GNU Lesser General Public License for more details.\n");
-
 	}
 }



More information about the Schmitzm-commits mailing list