GeoTools Eclipse 快速入门02

时间:2021-09-05 12:44:17

我们继续上节的翻译,GeoTools Eclipse 快速入门,今天开始这部分内容的第二节(向项目中添加Jar包)

Adding Jars to your Project

The pom.xml file is used to describe the care and feeding of your maven project; we are going to focus on the dependencies needed for your project 

When downloading jars maven makes use of a "local repository" to store jars.

PLATFORM LOCAL REPOSITORY
Windows XP: C:\Documents and Settings\You\.m2\repository
Windows: C:\Users\You\.m2repository
Linux and Mac: ~/.m2/repository

To download jars maven makes use of public maven repositories on the internet where projects such as GeoTools publish their work.

1.Open up pom.xml in your new project. You can see some of the information we entered earlier.

GeoTools Eclipse 快速入门02

2.This editor allows you to describe all kinds of things; in the interest of time we are going to skip the long drawn out explanation and ask you to click on thepom.xml/tab.

3.To make use of GeoTools we are going to add three things to this pom.xml file.

4.At the top after module Version add a properties element defining the version of GeoTools we want to use. This workbook was written for 17-SNAPSHOT although you may wish to try a different version.

For production a stable release is recommended:

    <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<geotools.version>15.1</geotools.version>
</properties>
To make use of nightly build set the  geotools.version property to 17-SNAPSHOT.

    <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- use the latest snapshot -->
<geotools.version>17-SNAPSHOT</geotools.version>
</properties>
5.We are going to add a dependence to GeoTools gt-main and gt-swing jars.Note we are making use of the geotools.version defined above.

    <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-shapefile</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-swing</artifactId>
<version>${geotools.version}</version>
</dependency>
</dependencies>
6.Finally we need to list the external repositories where maven can download GeoTools and other required jars from.

    <repositories>
<repository>
<id>maven2-repository.dev.java.net</id>
<name>Java.net repository</name>
<url>http://download.java.net/maven/2</url>
</repository>
<repository>
<id>osgeo</id>
<name>Open Source Geospatial Foundation Repository</name>
<url>http://download.osgeo.org/webdav/geotools/</url>
</repository>
</repositories>

Note

If you are using a nightly build (such as 17-SNAPSHOT) and add a reference to the snapshot repository.

    <repositories>
<repository>
<id>maven2-repository.dev.java.net</id>
<name>Java.net repository</name>
<url>http://download.java.net/maven/2</url>
</repository>
<repository>
<id>osgeo</id>
<name>Open Source Geospatial Foundation Repository</name>
<url>http://download.osgeo.org/webdav/geotools/</url>
</repository>
<repository>
<snapshots>
<enabled>true</enabled>
</snapshots>
<id>boundless</id>
<name>Boundless Maven Repository</name>
<url>http://repo.boundlessgeo.com/main</url>
</repository>
</repositories>
7.GeoTools now requires Java 8 language level features(eg.lambdas) - you need to tell Maven to use the 1.8 source level.

    <build>
<plugins>
<plugin>
<inherited>true</inherited>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
8.For comparison here is the completed pom.xml file for download.

   You may find cutting and pasting to be easier than typing, you can choose Source -> Fomat to fix indentation 

Tips:

  • If maven has trouble downloading any jar; you can try again by selecting Project ‣ Update All Maven Dependencies.
  • If the dependencies do not update automatically use Project ‣ Clean

向您的项目中添加Jar包

pom.xml文件用来描述您的Maven 项目中所关注和依赖的东西;我们将着眼于您项目中的依赖关系。

当您下载jar包时,maven 会用"本地仓库"("local repository")来存储这些jar包。

平台 本地仓库路径
Windows XP: C:\Documents and Settings\You\.m2\reposity
Windows: C:\Users\You\.m2repository
Linux and Mac: ~/.m2/repository

Maven 会到网上公用的存储库,比如GeoTools发布项目的库;来下载jar包。

1、在您新建的项目中打开pom.xml 文件,如您所见,有些信息已经事先填好了。

GeoTools Eclipse 快速入门02

2、编辑器允许您对各种事物进行描述;由于时间缘故,我们跳过长篇大论的描述并请您直接点击pom.xml选项卡

3、为了使用GeoTools,我们要向 pom.xml 文件中添加三处改动。

4、在开头,模块版本的后面添加一个属性元素来定义我们要使用的GeoTools的版本号。此教程是针对 17—快照版编写的,尽管如此,您还可以尝试使用其他的版本。

我们推荐使用一个稳定的发行版产品:

    <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<geotools.version>15.1</geotools.version>
</properties>
由于我们每天(都经常会)构建项目,不妨将geotools的版本 geotools.version 设置为 17- SNAPSHOT 快照版比较合适。

    <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- use the latest snapshot -->
<geotools.version>17-SNAPSHOT</geotools.version>
</properties>
5、我们将为 gt-main   和 gt-swing 两个jar包添加依赖关系,注意,我们使用的是上面提到的geotool的版本。

    <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-shapefile</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-swing</artifactId>
<version>${geotools.version}</version>
</dependency>
</dependencies>
6、最后,我们需要列出GeoTools 和其他需要用到的jar包的下载源——外部存储库  repositories.

    <repositories>
<repository>
<id>maven2-repository.dev.java.net</id>
<name>Java.net repository</name>
<url>http://download.java.net/maven/2</url>
</repository>
<repository>
<id>osgeo</id>
<name>Open Source Geospatial Foundation Repository</name>
<url>http://download.osgeo.org/webdav/geotools/</url>
</repository>
</repositories>

Note

If you are using a nightly build (such as 17-SNAPSHOT) and add a reference to the snapshot repository.

    <repositories>
<repository>
<id>maven2-repository.dev.java.net</id>
<name>Java.net repository</name>
<url>http://download.java.net/maven/2</url>
</repository>
<repository>
<id>osgeo</id>
<name>Open Source Geospatial Foundation Repository</name>
<url>http://download.osgeo.org/webdav/geotools/</url>
</repository>
<repository>
<snapshots>
<enabled>true</enabled>
</snapshots>
<id>boundless</id>
<name>Boundless Maven Repository</name>
<url>http://repo.boundlessgeo.com/main</url>
</repository>
</repositories>

7、GeoTools 目前需要 Java 8 语言环境支持(比如 lamdas) — 您需要让 Maven 选择 1.8 级别 的源。

    <build>
<plugins>
<plugin>
<inherited>true</inherited>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
8、为了方便您进行对比,这里提供了 pom.xml 文件的下载链接。

您会发现,剪切和粘贴要比打字方便多了,您可以选择 源 -> 格式 来修复缩进

提示:

  • 如果maven无法下载任何jar; 您可以通过选择 项目 ‣ 更新所有Maven依赖关系 再次尝试 。
  • 如果依赖不能自动更新,可使用 项目 ‣ 清除