最近公司重整CenterOs 服務器資源,項目組一直使用的基於 nexus2 搭建的 Maven 私服所在的服務器要資源回收。於是我打算在部門現有的一臺閒置的Wiin10 開發主機上重新搭建一個 Maven 私服。
說做就做!
第一步將原服務器倉庫裏的jar包和其他文件按照倉庫目錄結構拉取到本地
通過分析Nexus 私服頁面可以歸納出 ,倉庫的路徑是通過構造 table 表格的方式展示在頁面上的,倉庫文件夾和文件名稱在Html 標籤的位置可以通過“table>tbody>tr>td>a” 定位獲得,通過頁面內容可以分析出倉庫的文件夾名稱以"/"結尾,文件已後綴名結尾,基於此使用java 遞歸邏輯遍歷 倉庫目錄樹,在本地磁盤創建對應目錄 並下載文件。
java 實現代碼:
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
/**
* 復刻Nexus 遠程倉庫目錄樹到本地
*/
public class CloneNexusRepository {
public static final Logger log = LoggerFactory.getLogger(CloneNexusRepository.class);
private static final String NEXUS_REPOSITORY_ROOTURL = "http://10.47.142.194:8081/nexus/content/repositories/releases/";
private static final String LOCAL_DISK_NAME = "D:" + File.separator + "repository";
private static final int jsoup_timeout = 10000;
private static int countPom = 0;
public static void main(String[] args) throws IOException {
String firstPath = NEXUS_REPOSITORY_ROOTURL + "com/";
String Localroot = LOCAL_DISK_NAME + File.separator + File.separator + "com";
cloneNexusTree(firstPath, Localroot);
System.out.println();
System.out.println("共下載 :" + countPom + " 個文件.");
}
private static void cloneNexusTree(String cureentNextNode, String localPath) throws IOException {
// 修建本地目錄。
File dir = new File(localPath);
if (!dir.exists()) {
dir.mkdir();
System.out.println("創建路徑:" + dir.getPath());
}
Document doc = Jsoup.connect(cureentNextNode).userAgent("Mozilla").timeout(jsoup_timeout).get();
if (doc != null) {
Elements elements = doc.select("table>tbody>tr>td>a");
if (elements != null && elements.size() > 0) {
for (Element element : elements) {
//判斷是文件或文件夾
String nodeName = element.text();
if (!nodeName.equalsIgnoreCase("Parent Directory")) {
if (nodeName.endsWith("/")) {
// 非葉子節點.
String nextNodePath = cureentNextNode + nodeName;
String nextLocalPath = localPath + File.separator + nodeName.replaceFirst("/", "");
cloneNexusTree(nextNodePath, nextLocalPath);
} else {
// 葉子節點,下載文件.
download(cureentNextNode + nodeName, dir, nodeName);
}
}
}
}
}
}
public static void download(String downloadUrl, File localDir, String fileName) {
try {
URL url = new URL(downloadUrl);
URLConnection connection = url.openConnection();
connection.setConnectTimeout(5 * 1000);
InputStream inputStream = connection.getInputStream();
byte[] byteArr = new byte[1024];
int len;
if (localDir.exists() == false)
localDir.mkdirs();
OutputStream outputStream = new FileOutputStream(localDir + File.separator + fileName);
while ((len = inputStream.read(byteArr)) != -1) {
outputStream.write(byteArr, 0, len);
}
outputStream.close();
inputStream.close();
} catch (IOException e) {
System.out.println("下載文件失敗:" + downloadUrl);
}
System.out.println("下載文件完成:" + downloadUrl);
if (fileName.endsWith(".pom")) {
countPom++;
}
}
}
執行代碼在將在本地復刻一份倉庫。
安裝nexus 私服,版本nexus-2.12.0-01-bundle.zip,解壓Zip文件,依次執行下列操作:
1、打開C:\nexus-2.12.0-01\conf\nexus.properties 文件, 在文件尾部增加“wrapper.startup.delay=30”。
2、進入nexus bin文件夾 執行nexus install,執行完成輸出如圖。
3、繼續執行 nexus start 執行完成輸出如圖。
4、打開nexus 存儲倉庫目錄“C:\sonatype-work\nexus\storage” ,將本地復刻的倉庫目錄拷貝到對應的庫目錄下即可。
5、訪問nexus 私服 倉庫管理頁面。