实现GeoServer的工作空间复制。
概述:在使用GeoServer作为地图容器的时候,在测试机器上发布了大量的图层,需要配置新的工作空间副本的时候,还需要手动发布,经过测试,通过复制工作空间的方法非常方便。
Overview: When using GeoServer as a map container, a large number of layers were published on the testing machine. When a new workspace copy needs to be configured, manual publishing is also required. After testing, the method of copying workspaces is very convenient.
一、复制工作空间-Copy Workspace
将要复制的工作空间的文件夹复制一份在~GeoServer-Path/data_dir/workspaces
下,命名为期望的工作空间名称如New-Test
Copy a folder of the workspace to be copied and place it under ‘~GeoServer Path/data-dir/workspaces’. Name it the desired workspace name, such as’ New Test’`
二、修改index.mjs中的如下参数-Modify the following parameters in index.mjs
// GeoServer 的workspace工作空间路径
const geoserverWorkspaceDir = `/path/geoserver/data_dir/workspaces`;
// 新的工作空间名称
const newWorkSpaceName = "New-Test";
三、运行index.mjs
-run index.mjs
#首次执行
npm i;
#开始运行
node index.mjs
重启GeoServer新的工作空间生效。
Restart GeoServer to make it takes effect.
看到输出如下内容时运行完成:
When the following output is seen, the operation is completed:
replace over.
核心代码
import fs from "fs";
import path from "path";
const geoserverWorkspaceDir = `D:/programs/geoserver-2.18.0.9090/data_dir/workspaces`;
const newWorkSpaceName = "New-Test";
const workspaceRootDir = `${
geoserverWorkspaceDir}/${
newWorkSpaceName}`;
const workspaceFile = `${
workspaceRootDir}/workspace.xml`;
const namespaceFile = `${
workspaceRootDir}/namespace.xml`;
const stylesDir = `${
workspaceRootDir}/styles`;
/**
* traverse Directory
* @param {String} directory the directory to search
* @param {Function} callback 回调函数
*/
function traverseDirectory(directory, callback) {
fs.readdir(directory, (err, files) => {
if (err) throw err;
files.forEach((file) => {
let fullPath = path.join(directory, file);
fs.stat(fullPath, (err, stats) => {
if (err) throw err;
if (stats.isDirectory()) {
traverseDirectory(fullPath, callback);
} else {
if (!fullPath