You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opennlp.apache.org by jo...@apache.org on 2011/05/30 13:34:15 UTC

svn commit: r1129130 - in /incubator/opennlp/trunk/opennlp-tools: ./ src/main/java/opennlp/tools/util/ src/main/java/opennlp/tools/util/model/ src/main/resources/ src/main/resources/opennlp/ src/main/resources/opennlp/tools/ src/main/resources/opennlp/...

Author: joern
Date: Mon May 30 11:34:14 2011
New Revision: 1129130

URL: http://svn.apache.org/viewvc?rev=1129130&view=rev
Log:
OPENNLP-189 Now maven version is used instead of hard coded version

Added:
    incubator/opennlp/trunk/opennlp-tools/src/main/resources/
    incubator/opennlp/trunk/opennlp-tools/src/main/resources/opennlp/
    incubator/opennlp/trunk/opennlp-tools/src/main/resources/opennlp/tools/
    incubator/opennlp/trunk/opennlp-tools/src/main/resources/opennlp/tools/util/
    incubator/opennlp/trunk/opennlp-tools/src/main/resources/opennlp/tools/util/opennlp.version
Modified:
    incubator/opennlp/trunk/opennlp-tools/pom.xml
    incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/util/Version.java
    incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/util/model/BaseModel.java
    incubator/opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/util/VersionTest.java

Modified: incubator/opennlp/trunk/opennlp-tools/pom.xml
URL: http://svn.apache.org/viewvc/incubator/opennlp/trunk/opennlp-tools/pom.xml?rev=1129130&r1=1129129&r2=1129130&view=diff
==============================================================================
--- incubator/opennlp/trunk/opennlp-tools/pom.xml (original)
+++ incubator/opennlp/trunk/opennlp-tools/pom.xml Mon May 30 11:34:14 2011
@@ -63,6 +63,12 @@
 	</dependencies>
 
 	<build>
+		<resources>
+		    <resource>
+		      <directory>src/main/resources</directory>
+		      <filtering>true</filtering>
+		    </resource>
+		</resources>
 		<plugins>
 			<plugin>
 				<groupId>org.apache.maven.plugins</groupId>

Modified: incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/util/Version.java
URL: http://svn.apache.org/viewvc/incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/util/Version.java?rev=1129130&r1=1129129&r2=1129130&view=diff
==============================================================================
--- incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/util/Version.java (original)
+++ incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/util/Version.java Mon May 30 11:34:14 2011
@@ -18,6 +18,10 @@
 
 package opennlp.tools.util;
 
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Properties;
+
 /**
  * The {@link Version} class represents the OpenNlp Tools library version.
  * <p>
@@ -32,12 +36,20 @@ package opennlp.tools.util;
  */
 public class Version {
 
+  private static final String DEV_VERSION_STRING = "0.0.0-SNAPSHOT";
+  
+  public static final Version DEV_VERSION = Version.parse(DEV_VERSION_STRING);
+  
+  private static final String SNAPSHOT_MARKER = "-SNAPSHOT";
+  
   private final int major;
 
   private final int minor;
 
   private final int revision;
 
+  private final boolean snapshot;
+  
   /**
    * Initializes the current instance with the provided
    * versions.
@@ -45,14 +57,29 @@ public class Version {
    * @param major
    * @param minor
    * @param revision
+   * @param snapshot
    */
-  public Version(int major, int minor, int revision) {
+  public Version(int major, int minor, int revision, boolean snapshot) {
     this.major = major;
     this.minor = minor;
     this.revision = revision;
+    this.snapshot = snapshot;
   }
 
   /**
+   * Initializes the current instance with the provided
+   * versions. The version will not be a snapshot version.
+   *
+   * @param major
+   * @param minor
+   * @param revision
+   */
+  public Version(int major, int minor, int revision) {
+   this(major, minor, revision, false); 
+  }
+
+  
+  /**
    * Retrieves the major version.
    *
    * @return major version
@@ -79,6 +106,10 @@ public class Version {
     return revision;
   }
 
+  public boolean isSnapshot() {
+    return snapshot;
+  }
+  
   /**
    * Retrieves the version string.
    *
@@ -89,7 +120,7 @@ public class Version {
    */
   public String toString() {
     return Integer.toString(getMajor()) + "." + Integer.toString(getMinor()) +
-      "." + Integer.toString(getRevision());
+      "." + Integer.toString(getRevision()) + (isSnapshot() ? SNAPSHOT_MARKER : "");
   }
 
   @Override
@@ -101,7 +132,8 @@ public class Version {
 
       return getMajor() == version.getMajor()
           && getMinor() == version.getMinor()
-          && getRevision() == version.getRevision();
+          && getRevision() == version.getRevision()
+          && isSnapshot() == version.isSnapshot();
     }
     else {
       return false;
@@ -128,9 +160,21 @@ public class Version {
     if (indexFirstDot == -1 || indexSecondDot == -1)
         throw new NumberFormatException("Invalid version!");
 
+    int indexFirstDash = version.indexOf('-');
+    
+    int versionEnd;
+    if (indexFirstDash == -1) {
+      versionEnd = version.length();
+    }
+    else {
+      versionEnd = indexFirstDash;
+    }
+    
+    boolean snapshot = version.endsWith(SNAPSHOT_MARKER);
+    
     return new Version(Integer.parseInt(version.substring(0, indexFirstDot)),
         Integer.parseInt(version.substring(indexFirstDot + 1, indexSecondDot)),
-        Integer.parseInt(version.substring(indexSecondDot + 1)));
+        Integer.parseInt(version.substring(indexSecondDot + 1, versionEnd)), snapshot);
   }
 
   /**
@@ -139,6 +183,35 @@ public class Version {
    * @return the current version
    */
   public static Version currentVersion() {
-    return new Version(1, 5, 2);
+    
+    Properties manifest = new Properties();
+    
+    // Try to read the version from the version file if it is available,
+    // otherwise set the version to the development version
+    
+    InputStream versionIn = Version.class.getResourceAsStream("opennlp.version");
+    
+    if (versionIn != null) {
+      try {
+        manifest.load(versionIn);
+      } catch (IOException e) {
+        // ignore error
+      }
+      finally {
+        try {
+          versionIn.close();
+        } catch (IOException e) {
+          // ignore error
+        }
+      }
+    }
+    
+    String versionString = 
+      manifest.getProperty("OpenNLP-Version", DEV_VERSION_STRING);
+    
+    if (versionString.equals("${pom.version}"))
+      versionString = DEV_VERSION_STRING;
+    
+    return Version.parse(versionString);
   }
 }

Modified: incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/util/model/BaseModel.java
URL: http://svn.apache.org/viewvc/incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/util/model/BaseModel.java?rev=1129130&r1=1129129&r2=1129130&view=diff
==============================================================================
--- incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/util/model/BaseModel.java (original)
+++ incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/util/model/BaseModel.java Mon May 30 11:34:14 2011
@@ -235,6 +235,12 @@ public abstract class BaseModel {
         throw new InvalidFormatException("Model version " + version + " is not supported by this (" 
             + Version.currentVersion() +") version of OpenNLP!");
       }
+      
+      // Reject loading a snapshot model with a non-snapshot version
+      if (!Version.currentVersion().isSnapshot() && version.isSnapshot()) {
+        throw new InvalidFormatException("Model is a snapshot models are not" +
+        		"supported by release versions!");
+      }
     }
     else {
       throw new InvalidFormatException("Missing " + VERSION_PROPERTY + " property in " +

Added: incubator/opennlp/trunk/opennlp-tools/src/main/resources/opennlp/tools/util/opennlp.version
URL: http://svn.apache.org/viewvc/incubator/opennlp/trunk/opennlp-tools/src/main/resources/opennlp/tools/util/opennlp.version?rev=1129130&view=auto
==============================================================================
--- incubator/opennlp/trunk/opennlp-tools/src/main/resources/opennlp/tools/util/opennlp.version (added)
+++ incubator/opennlp/trunk/opennlp-tools/src/main/resources/opennlp/tools/util/opennlp.version Mon May 30 11:34:14 2011
@@ -0,0 +1,17 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreemnets.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Version is injected by the maven build, fall back version is 0.0.0-SNAPSHOT
+OpenNLP-Version: ${pom.version}
\ No newline at end of file

Modified: incubator/opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/util/VersionTest.java
URL: http://svn.apache.org/viewvc/incubator/opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/util/VersionTest.java?rev=1129130&r1=1129129&r2=1129130&view=diff
==============================================================================
--- incubator/opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/util/VersionTest.java (original)
+++ incubator/opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/util/VersionTest.java Mon May 30 11:34:14 2011
@@ -31,6 +31,15 @@ public class VersionTest {
   public void testParse() {
     Version referenceVersion = Version.currentVersion();
     assertEquals(referenceVersion, Version.parse(referenceVersion.toString()));
+    
+    assertEquals(new Version(1,5,2, false), Version.parse("1.5.2-incubating"));
+    assertEquals(new Version(1,5,2, false), Version.parse("1.5.2"));
+  }
+  
+  @Test
+  public void testParseSnapshot() {
+    assertEquals(new Version(1,5,2, true), Version.parse("1.5.2-incubating-SNAPSHOT"));
+    assertEquals(new Version(1,5,2, true), Version.parse("1.5.2-SNAPSHOT"));
   }
 
   @Test