【问题标题】:Geotools/Maven dependency orderGeotools/Maven 依赖顺序
【发布时间】:2015-07-21 16:39:53
【问题描述】:

我一直在尝试追查以下异常的原因

org.geotools.feature.SchemaException:解码 srs 时出错:4326

我一直看到人们说我需要以下依赖项

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

我使用 m2e eclipse 插件来构建我的 jar,我总是能够通过 eclipse 成功运行我的应用程序,但是当我尝试从命令行运行我的 jar 时,我会得到异常,所以我知道它有成为我的罐子建造方式的东西。我使用以下代码创建了一个简单的应用程序来复制问题。

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 项目中并不重要。有人可以解释发生了什么吗?感谢您提供任何反馈!

【问题讨论】:

  • 不应该,但可能会出现奇怪的情况;是否有完全相同的包/类的多个实现?
  • 我就是这么想的。我对 geotools 相当陌生,所以我不确定他们的包在幕后发生了什么。
  • 我想你可以解压缩所有的 jar 文件并检查一下。

标签: maven geotools


【解决方案1】:

你说得对,通常 mvn 并不关心依赖关系的顺序,但是它确实按照它们在 pom 文件中出现的顺序来处理它们。

当您打包 jars 时,这一点变得很重要,因为默认的 maven 打包程序不能很好地与 GeoTools 用来加载工厂的 SPI 文件配合使用,在这种情况下是 EPSG 引用工厂。

如果您使用Maven Shade packager,它将正确构建文件。 GeoTools FAQ 中有更长的讨论和示例。

【讨论】:

    猜你喜欢
    • 2012-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-04
    • 2015-10-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多