一、 Windows部署GDAL
二、 JAVA开发环境整合GDAL
三、 部署Java集成gdal项目
四、 Linux系统编译、发布gdal项目
文末有源码下载地址
三、部署Java集成gdal项目
为了项目开发调试和部署资源自动复制,我们需要使用Maven插件完成资源拷贝和打包任务,因此在pom进行以下配置:2和3对应项目类型二选一
1、资源拷贝使用Maven插件maven-resources-plugin复制文件到WEB-INF/lib
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>copy-resources-jars</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/lib</outputDirectory>
<resources>
<resource>
<directory>${gdal.bindir}</directory>
<filtering>false</filtering>
<includes>
<include>**/*.jar</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
2 tomcat部署SpringMvc打包War
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warName>${project.artifactId}</warName>
<webResources>
<resource>
<directory>${gdal.bindir}</directory>
<targetPath>WEB-INF/lib</targetPath>
<includes>
<include>**/*.jar</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>
3 Docker部署SpringBoot打包Jar
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<warName>${project.artifactId}</warName>
<webResources>
<resource>
<directory>${gdal.bindir}</directory>
<targetPath>WEB-INF/lib</targetPath>
<includes>
<include>**/*.jar</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>
(4)控制器
加载动态库要通过接口加载,在Windows环境通过static{加载动态库}是可行的,而在Linux环境动态库在JVM启动时已经加载完成了,再次载入报错 libgdalalljni.so already loaded in another classloader
- Windows环境需要通过代码加载动态库,可以在前端调用API来加载
- Linux环境JVM启动自行加载,在项目中无需加载
/*
* 加载动态库
*/
@RequestMapping("/api/gdal/load")
public JsonResult<String> load() {
GdalHolder.loadLibrary();
return this.renderSuccess("加载完成!");
}
/*
* 注册驱动
*/
@RequestMapping("/api/gdal/register")
public JsonResult<String> register() {
GdalHolder.registerAll();
return this.renderSuccess("注册驱动!");
}
/*
* 测试接口
*/
@RequestMapping("/api/gdal/test")
public JsonResult<String> test() {
...
}
OK,赶快发布一个试试水吧,对应进行配置修改
<gdal.bindir>src/main/resources/gdal/win32</gdal.bindir>
<gdal.bindir>src/main/resources/gdal/linux</gdal.bindir>