Maven快速入门

Maven快速入门

(译自http://docs.geotools.org/latest/userguide/tutorial/quickstart/maven.html)

欢迎

本教程适用于喜欢文本编辑器和可靠命令提示符的愉快公司的用户。即使您经常使用IDE,您会发现从命令行编译,测试和安装应用程序通常更快更容易。我们将使用Maven(http://maven.apache.org/)来管理GeoTools项目依赖的大量jar。不要担心,如果你不熟悉Maven,因为我们会一步一步地解释一切。

示例应用程序与NetBeans和Eclipse Quickstart教程中使用的应用程序相同:用于加载和显示shapefile的简单程序。

当我们准备课程材料时,我们要感谢GeoTools用户邮件列表中的成员的反馈,特别感谢Eva Shon进行测试/审查早期草案。如果您对本教程有任何问题或意见,请将其发布到用户列表中。

Java安装

我们将要使用Java,所以如果您现在没有安装Java开发工具包(JDK),那么现在就可以这么做了。

  1. 下载最新的Java 8 JDK:

  2. 在撰写本文时,最新的Java 8版本是:

    • JDK-8u66-Windows的i586.exe

    GeoTools尚未通过Java 9测试,我们受到构建基础设施和志愿者的限制。

  3. 点击安装程序,您将需要设置接受许可协议等等。默认情况下,这将安装到:

    C:\ Program Files (x86)\ Javajdk1.8.0_66

注意

在本教程中,我们引用Windows使用的文件和目录路径。如果您足够幸运地使用其他操作系统(如Linux或OSX),以下所有命令和源代码都可以正常工作,只需修改适合的路径即可。

Maven(为什么不那么糟糕)

Maven是一个广泛使用的构建工具,通过描述项目的内容来起作用。这是一个不同于Make或Ant工具使用的方法,它列出了构建所需的步骤。

需要一段时间才能习惯于Maven,而对于某些人来说,它仍然是一种爱恨的关系,但这绝对会使GeoTools的工作变得更加容易:

  • 您只需下载与应用程序相同的GeoTools。
  • Jars被下载到您的主目录中的单个位置(例如Windows上的C:\ Documents and Settings <user> \.m2 \)。这是您的本地存储库
  • 将为您下载GeoTools所需的所有第三方jar的正确版本。这可以帮助您避免由错误匹配的依赖关系引起的模糊错误,这可能非常难以追踪。
  • 单个本地存储库可以更轻松地处理其他多个开源项目。

安装Maven

  1. http://maven.apache.org/download.html下载Maven

    在本教程中,我们参考Maven 3.2.3版,我们在Maven版本3中遇到了一些麻烦。

  2. 解压文件apache-maven-3.2.3-bin.zip

  3. 您需要为maven设置几个环境变量才能工作。导航到 控制面板‣系统‣高级。切换到高级选项卡,然后单击环境变量按钮。

    添加以下系统变量:

    • JAVA_HOME = C:\ Program Files (x86)\ Java \ jdk1.8.0_66
    • M2_HOME = C:\ java \ apache-maven-3.2.3

    并将以下内容添加到您的路径中:

    • PATH = %JAVA_HOME%\ bin;%M2_HOME%\ bin
    ../../_images/env-variables.png
  4. 打开命令提示符附件‣命令提示符

  5. 键入以下命令以确认您设置正确:

    C:java> mvn --version
  6. 这应该产生类似于以下输出的内容:

    C:\ java> mvn -version
    Apache Maven 3.2.3(33f8c3e1027c3ddde99d3cdebad2656a31e8fdf4; 2014-08-11T13:58:10-07:00)
    Maven主页:C:\ java \ apache-maven-3.2.3
    Java版本:1.8.0_66,供应商:Oracle Corporation
    Java home:C:\ Program Files(x86)\ Java \ jdk1.8.0_66 \ jre
    默认语言环境:en_US,平台编码:Cp1252
    操作系统名称:“windows 7”,版本:“6.1”,arch:“x86”,系列:“windows”
    ../../_images/maven-version.png

创建一个新项目

  1. 我们现在可以从maven-archetype-quickstart生成我们的项目:

    C:> cd C:\ java
    C:java> mvn archetype:generate -DgroupId = org.geotools -DartifactId = tutorial -Dversion = 1.0-SNAPSHOT -DarchetypeGroupId = org.apache.maven.archetypes -DarchetypeArtifactId = maven-archetype-quickstart
  2. 上述命令创建以下文件和目录:

    tutorial
    tutorial\pom.xml
    tutorial\src
    tutorial\src\main
    tutorial\src\main\java
    tutorial\src\main\java\org
    tutorial\src\main\java\org\geotools
    tutorial\src\main\java\org\geotools\App.java
    tutorial\src\test
    tutorial\src\test\java
    tutorial\src\test\java\org
    tutorial\src\test\java\org\geotools
    tutorial\src\test\java\org\geotools\AppTest.java

    App.java和AppTest.java只是本教程中未使用的占位符文件。

  3. 在构建过程中,您的本地maven存储库将用于存储这两个下载的jar,以及您在本地构建的jar。

    您的本地Maven存储库位于您的主文件夹中。

    平台当地报告
    Windows XP:C:\Documents and Settings\You\.m2\repository
    Windows:C:\Users\You.m2\repository
    Linux和Mac:~/.m2/repository
  4. 在您喜欢的文本编辑器中打开pom.xml文件。如果你的编辑器有一个XML语法模式切换到现在,因为它会使它更容易找到错误,如错误匹配的括号。一些编辑器(如vim)会在加载文件时自动执行此操作。

  5. 我们将首先定义我们希望使用的GeoTools的版本号。此工作簿是为17-SNAPSHOT编写的,但您可能希望尝试其他版本。

    对于生产,推荐使用稳定的释放:

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

    为了利用每晚的构建,将geotools.version属性设置为17-SNAPSHOT。

        <properties> 
            <project.build.sourceEncoding> UTF-8 </project.build.sourceEncoding> 
            <! - 使用最新的快照 - > 
            <geotools.version> 17-SNAPSHOT </geotools.version> </ properties> 
  6. 我们指定以下依赖项(应用程序需要的GeoTools模块):

    <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>
  7. 我们告诉maven哪些存储库下载jars:

        <repository> 
          <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> 

    如果您正在使用夜间构建(如17-SNAPSHOT)并添加对快照存储库的引用。

        <repository> 
            <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> 
  8. 如果您想使用Java 8语言级别的功能(例如lambdas),则需要告诉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> 
  9. 返回命令行,并使用以下命令获取maven下载您项目所需的jar:

    C:\ java \ example> mvn install
  10. 如果maven在下载任何jar时遇到问题,可以随时重试。国家镜像通常比默认的maven中心更快。

创建快速入门应用程序

现在我们准备好创建应用程序了。

  1. 克里特org.geotools.tutorial.quickstart包导航到该目录 tutorial并创建目录src\main\java\org\geotools\tutorial\quickstart

  2. 在新的子目录中,使用文本编辑器创建一个新文件Quickstart.java

  3. 填写以下代码:

    package org.geotools.tutorial.quickstart; import java.io.File; import org.geotools.data.FileDataStore; import org.geotools.data.FileDataStoreFinder; import org.geotools.data.simple.SimpleFeatureSource; import org.geotools.map.FeatureLayer; import org.geotools.map.Layer; import org.geotools.map.MapContent; import org.geotools.styling.SLD; import org.geotools.styling.Style; import org.geotools.swing.JMapFrame; import org.geotools.swing.data.JFileDataStoreChooser; /**  * Prompts the user for a shapefile and displays the contents on the screen in a map frame.  * <p>  * This is the GeoTools Quickstart application used in documentationa and tutorials. *  */ public class Quickstart { /**  * GeoTools Quickstart demo application. Prompts the user for a shapefile and displays its  * contents on the screen in a map frame  */ public static void main(String[] args) throws Exception { // display a data store file chooser dialog for shapefiles File file = JFileDataStoreChooser.showOpenFile("shp", null); if (file == null) { return; } FileDataStore store = FileDataStoreFinder.getDataStore(file); SimpleFeatureSource featureSource = store.getFeatureSource(); // Create a map content and add our shapefile to it MapContent map = new MapContent(); map.setTitle("Quickstart"); Style style = SLD.createSimpleStyle(featureSource.getSchema()); Layer layer = new FeatureLayer(featureSource, style); map.addLayer(layer); // Now display the map JMapFrame.showMap(map); } }
  4. 返回顶部的项目目录(包含您的pom.xml文件的目录),并使用以下命令构建应用程序:

    mvn clean install

运行应用程序

  1. 如果您需要一些形状文件,您将在 http://www.naturalearthdata.com/项目中找到一份由北美制图信息学会支持的数据。前往下面的链接,下载一些文化媒介。您可以使用“下载所有50m文化主题”。

    将上述数据解压缩到您可以轻松找到的位置,如桌面。

  2. 您可以在命令行中使用Maven运行应用程序:

    mvn exec:java -Dexec.mainClass = org.geotools.tutorial.quickstart.Quickstart
  3. 应用程序将连接到您的shapefile,生成地图上下文,并显示shapefile。

    ../../_images/QuickstartMap.png
  4. 有几件事情要注意的代码示例:

  • shapefile未加载到内存中。而是每次需要从磁盘读取磁盘。这种方法允许您处理大于可用内存的数据集。
  • 我们正在使用一个非常基本的显示风格,只显示功能轮廓。在下面的例子中,我们将看到如何指定更复杂的样式。

事情要尝试

  • 尝试不同的样本数据集。

  • 您可以放大,缩小并显示完整的范围,并使用信息工具来检查示例countries.shp文件中的各个国家/地区。

  • 下载最大的shapefile,您可以找到并查看可以呈现的速度。你会发现这是第一次需要一段时间作为空间索引。之后,渲染将变得更快。

  • 快:我们知道人们选择空间库的方式之一是基于速度。通过设计,GeoTools不会将上述shapefile加载到内存中(而是在每次使用空间索引绘制时仅将其显示所需的内容)从磁盘中流出。

    如果您想要让GeoTools在内存中缓存shapefile,请尝试以下代码:

    /**
         * This method demonstrates using a memory-based cache to speed up the display (e.g. when
         * zooming in and out).
         * 
         * There is just one line extra compared to the main method, where we create an instance of
     * CachingFeatureStore.  */ public static void main(String[] args) throws Exception { // display a data store file chooser dialog for shapefiles File file = JFileDataStoreChooser.showOpenFile("shp", null); if (file == null) { return; } FileDataStore store = FileDataStoreFinder.getDataStore(file); SimpleFeatureSource featureSource = store.getFeatureSource(); // CachingFeatureSource is deprecated as experimental (not yet production ready) CachingFeatureSource cache = new CachingFeatureSource(featureSource); // Create a map content and add our shapefile to it MapContent map = new MapContent(); map.setTitle("Using cached features"); Style style = SLD.createSimpleStyle(featureSource.getSchema()); Layer layer = new FeatureLayer(cache, style); map.addLayer(layer); // Now display the map JMapFrame.showMap(map); }

    您还需要添加此import语句:

    import  org.geotools.data.CachingFeatureSource ;
    

    暗示

    在文本编辑器而不是IDE中使用GeoTools javadocs来确定源中需要哪些import语句。javadoc还列出了找到每个类的GeoTools模块。

    注意

    构建时,您可能会看到不推荐使用CachingFeatureSource的消息。可以忽略它,这只是一个警告。该课程仍在考试中,但可用。

  • 尝试并整理出所有不同的“侧车”文件 - 以及它们是什么。样本数据集包括“shp”,“dbf”和“shx”。有多少其他侧车文件?
  • 高级:使用FileDataStoreFinder可以方便我们使用文件。另一种做事情的方法是连接参数的映射。这种技术使我们能够更好地控制我们如何使用shapefile,并且还允许我们连接到数据库和Web功能服务器。
     File file = JFileDataStoreChooser.showOpenFile("shp", null); Map<String,Object> params = new HashMap<>(); params.put( "url", file.toURI().toURL() ); params.put( "create spatial index", false ); params.put( "memory mapped buffer", false ); params.put( "charset", "ISO-8859-1" ); DataStore store = DataStoreFinder.getDataStore( params ); SimpleFeatureSource featureSource = store.getFeatureSource( store.getTypeNames()[0] );
  • 那么,快速启动应用程序中实际使用的jar是什么?在命令行上尝试以下操作:

    mvn依赖:树

    我们将在剩下的教程中更深入地利用一些项目。

 

转载于:https://www.cnblogs.com/Prettiest/p/7009352.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值