Geotools/Maven 依赖顺序
Geotools/Maven dependency order
我一直在努力追查以下异常的原因
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 时,我会得到异常,所以我知道它必须与我的 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 时,我会看到 "Success!" 并且不会发生异常。但是如果我的依赖项是按这个顺序
<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 项目中并不重要。有人可以解释发生了什么吗?感谢任何反馈!
您说得对,通常 mvn 不关心依赖关系的顺序,但是它确实按照它们在 pom 文件中出现的顺序处理它们。
当您打包 jar 时,这确实变得很重要,因为默认的 maven 打包程序不能很好地处理 GeoTools 用来加载工厂的 SPI 文件,在本例中是 EPSG 引用工厂。
如果您使用 Maven Shade packager it will build the files correctly. There is a longer discussion and an example in the GeoTools FAQ.
我一直在努力追查以下异常的原因
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 时,我会得到异常,所以我知道它必须与我的 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 时,我会看到 "Success!" 并且不会发生异常。但是如果我的依赖项是按这个顺序
<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 项目中并不重要。有人可以解释发生了什么吗?感谢任何反馈!
您说得对,通常 mvn 不关心依赖关系的顺序,但是它确实按照它们在 pom 文件中出现的顺序处理它们。
当您打包 jar 时,这确实变得很重要,因为默认的 maven 打包程序不能很好地处理 GeoTools 用来加载工厂的 SPI 文件,在本例中是 EPSG 引用工厂。
如果您使用 Maven Shade packager it will build the files correctly. There is a longer discussion and an example in the GeoTools FAQ.