2015-07-20 496 views
1

我一直在試圖追查下列異常的原因Geotools/Maven的依賴順序

org.geotools.feature.SchemaException:錯誤解碼SRS:4326

,我不斷看到有人說,我需要以下依賴

<dependency> 
     <groupId>org.geotools</groupId> 
     <artifactId>gt-epsg-hsql</artifactId> 
     <version>${geotools.version}</version> 
    </dependency> 

我用的是M2E Eclipse插件來建立我的罐子,我總是能夠通過Eclipse的成功運行我的應用程序,但是當我嘗試運行命令行我的罈子,會得到例外,所以我知道它有o用我的罐子建造的方式做些事情。我用下面的代碼創建了一個簡單的應用程序來複制問題。

import org.geotools.data.DataUtilities; 
import org.geotools.feature.SchemaException; 
import org.opengis.feature.simple.SimpleFeatureType; 


public class Main { 

/** 
* @param args 
* @throws SchemaException 
*/ 
public static void main(String[] args) throws SchemaException { 

    SimpleFeatureType lineFeatureType = DataUtilities.createType("LINE", "geom:LineString:srid=4326,name:String"); 

    System.out.println("Success!"); 
} 

}

我POM文件看起來像這樣

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
<modelVersion>4.0.0</modelVersion> 
<groupId>groupid</groupId> 
<artifactId>artifactid</artifactId> 
<version>0.0.1-SNAPSHOT</version> 
<name>example</name> 
<packaging>jar</packaging> 

<properties> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    <geotools.version>12-RC1</geotools.version> 
</properties> 

<dependencies> 
<!--  <dependency> --> 
<!--   <groupId>org.geotools</groupId> --> 
<!--   <artifactId>gt-shapefile</artifactId> --> 
<!--   <version>${geotools.version}</version> --> 
<!--  </dependency> --> 
    <dependency> 
     <groupId>org.geotools</groupId> 
     <artifactId>gt-epsg-hsql</artifactId> 
     <version>${geotools.version}</version> 
    </dependency> 
    <dependency> 
     <groupId>org.geotools</groupId> 
     <artifactId>gt-geometry</artifactId> 
     <version>${geotools.version}</version> 
    </dependency> 
    <dependency> 
     <groupId>org.geotools</groupId> 
     <artifactId>gt-shapefile</artifactId> 
     <version>${geotools.version}</version> 
    </dependency> 
</dependencies> 

<repositories> 
    <repository> 
     <id>OSGEO GeoTools repo</id> 
     <url>http://download.osgeo.org/webdav/geotools</url> 
    </repository> 
</repositories> 

<build> 
    <plugins> 
     <plugin> 
      <artifactId>maven-assembly-plugin</artifactId> 
      <configuration> 
       <archive> 
        <manifest> 
         <mainClass>Main</mainClass> 
        </manifest> 
       </archive> 
       <descriptorRefs> 
        <descriptorRef>jar-with-dependencies</descriptorRef> 
       </descriptorRefs> 
      </configuration> 
      <executions> 
       <execution> 
        <id>make-my-jar-with-dependencies</id> <!-- this is used for inheritance merges --> 
        <phase>package</phase> <!-- bind to the packaging phase --> 
        <goals> 
         <goal>single</goal> 
        </goals> 
       </execution> 
      </executions> 
     </plugin> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-compiler-plugin</artifactId> 
      <version>3.2</version> 
      <configuration> 
       <source>1.7</source> 
       <target>1.7</target> 
      </configuration> 
     </plugin> 
    </plugins> 
</build> 

我開始看到的是,如果我的依賴是順序

<dependencies> 
<!--  <dependency> --> 
<!--   <groupId>org.geotools</groupId> --> 
<!--   <artifactId>gt-shapefile</artifactId> --> 
<!--   <version>${geotools.version}</version> --> 
<!--  </dependency> --> 
    <dependency> 
     <groupId>org.geotools</groupId> 
     <artifactId>gt-epsg-hsql</artifactId> 
     <version>${geotools.version}</version> 
    </dependency> 
    <dependency> 
     <groupId>org.geotools</groupId> 
     <artifactId>gt-geometry</artifactId> 
     <version>${geotools.version}</version> 
    </dependency> 
    <dependency> 
     <groupId>org.geotools</groupId> 
     <artifactId>gt-shapefile</artifactId> 
     <version>${geotools.version}</version> 
    </dependency> 
</dependencies> 

然後當我執行我的從命令行輸入jar,我會看到「成功!」並且不會發生異常。但是,如果我的依賴關係是按此順序的

<dependencies> 
    <dependency> 
     <groupId>org.geotools</groupId> 
     <artifactId>gt-shapefile</artifactId> 
     <version>${geotools.version}</version> 
    </dependency> 
    <dependency> 
     <groupId>org.geotools</groupId> 
     <artifactId>gt-epsg-hsql</artifactId> 
     <version>${geotools.version}</version> 
    </dependency> 
    <dependency> 
     <groupId>org.geotools</groupId> 
     <artifactId>gt-geometry</artifactId> 
     <version>${geotools.version}</version> 
    </dependency> 
<!--  <dependency> --> 
<!--   <groupId>org.geotools</groupId> --> 
<!--   <artifactId>gt-shapefile</artifactId> --> 
<!--   <version>${geotools.version}</version> --> 
<!--  </dependency> --> 
</dependencies> 

則會發生異常。

我認爲依賴關係的順序在maven項目中並不重要。有人可以解釋發生了什麼事嗎?我很欣賞任何反饋!

+0

它不應該,但可能會發生奇怪;是否有多個完全相同的包/類的實現? –

+0

這就是我的想法。我對geotools相當陌生,所以我不確定它們的包在幕後發生了什麼。 – keitheweber

+0

我想可以解壓所有的罐子並檢查。 –

回答

0

你是正確的,通常mvn不關心依賴關係的順序,但是它確實按照他們在pom文件中出現的順序工作。

由於默認的maven打包程序與GeoTools用來加載工廠的SPI文件不兼容,在這種情況下,EPSG引用工廠,因此這在打包jar時變得非常重要。

如果使用Maven Shade packager它將正確構建文件。 GeoTools FAQ有更長的討論和示例。