From 0ae37b1ec554253f53f0131cb0f997d07a6bdd11 Mon Sep 17 00:00:00 2001 From: MrXiaoM Date: Sun, 24 Dec 2023 12:22:51 +0800 Subject: [PATCH 1/7] snapshots repository support --- .../org/itxtech/mcl/component/Repository.java | 52 ++++++++++++++++++- 1 file changed, 50 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/itxtech/mcl/component/Repository.java b/src/main/java/org/itxtech/mcl/component/Repository.java index 8ba707f..17c0a2e 100644 --- a/src/main/java/org/itxtech/mcl/component/Repository.java +++ b/src/main/java/org/itxtech/mcl/component/Repository.java @@ -5,6 +5,8 @@ import org.itxtech.mcl.Loader; import org.itxtech.mcl.pkg.MclPackage; import org.w3c.dom.Document; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; import org.xml.sax.InputSource; import javax.xml.XMLConstants; @@ -149,8 +151,17 @@ public String getJarUrl(MclPackage pkg, PackageInfo info) { } } for (var repo : loader.config.mavenRepo) { - var base = repo + "/" + transformId(pkg.id) + "/" + pkg.version + "/" - + getPackageFromId(pkg.id) + "-" + pkg.version; + var baseFolder = repo + "/" + transformId(pkg.id) + "/" + pkg.version + "/"; + var base = baseFolder + getPackageFromId(pkg.id) + "-" + pkg.version; + if (pkg.version.endsWith("-SNAPSHOT")) { + try { + var real = getSnapshotJarUrl(baseFolder); + if (!real.isEmpty()) return real; + } catch (Exception e) { + loader.logger.logException(e); + } + continue; + } for (var suf : loader.config.archiveSuffix) { var real = base + suf; try { @@ -165,6 +176,43 @@ public String getJarUrl(MclPackage pkg, PackageInfo info) { return ""; } + private String getSnapshotJarUrl(String baseFolder) throws Exception { + if (httpHead(baseFolder + "maven-metadata.xml").statusCode() != 200) return ""; + var content = httpGet(baseFolder + "maven-metadata.xml"); + var factory = DocumentBuilderFactory.newInstance(); + factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); + var document = factory.newDocumentBuilder().parse(new InputSource(new StringReader(content))); + var elements = document.getElementsByTagName("snapshotVersion"); + for (int i = 0; i < elements.getLength(); i++) { + var version = elements.item(i).getChildNodes(); + var classifier = findNodeValue(version, "classifier", ""); + var extension = findNodeValue(version, "extension", ""); + var value = findNodeValue(version, "value", ""); + var suffix = (classifier.isEmpty() ? "" : ("-" + classifier)) + "." + extension; + if (loader.config.archiveSuffix.contains(suffix)) { + var real = baseFolder + value + suffix; + try { + if (httpHead(real).statusCode() == 200) { + return real; + } + } catch (Exception e) { + loader.logger.logException(e); + } + } + } + return ""; + } + + public static String findNodeValue(NodeList nodes, String name, String defValue) { + for (int i = 0; i < nodes.getLength(); i++) { + var node = nodes.item(i); + if (node.getNodeName().equals(name)) { + return node.getNodeValue(); + } + } + return defValue; + } + public String getMetadataUrl(MclPackage pkg, PackageInfo info) { if (info != null && info.repo != null) { var repoInfo = info.repo.get(pkg.version); From 265561ba0661cd3897ff2ddd35179d290d466a09 Mon Sep 17 00:00:00 2001 From: MrXiaoM Date: Sun, 24 Dec 2023 13:23:35 +0800 Subject: [PATCH 2/7] fix typo --- src/main/java/org/itxtech/mcl/component/Repository.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/itxtech/mcl/component/Repository.java b/src/main/java/org/itxtech/mcl/component/Repository.java index 17c0a2e..8f0e3c5 100644 --- a/src/main/java/org/itxtech/mcl/component/Repository.java +++ b/src/main/java/org/itxtech/mcl/component/Repository.java @@ -119,7 +119,7 @@ public String getLatestVersionFromMaven(String id, String channel) throws Except } } if (map.size() == 0) { - loader.logger.error("Cannot find any version matches channel \"" + channel + "`\" for \"" + id + "\", using default version."); + loader.logger.error("Cannot find any version matches channel \"" + channel + "\" for \"" + id + "\", using default version."); } else { return map.lastEntry().getValue(); } From 7eea0efce0dde0305df757f787dee1196440361c Mon Sep 17 00:00:00 2001 From: MrXiaoM Date: Sun, 24 Dec 2023 13:24:22 +0800 Subject: [PATCH 3/7] if not http 200, skip decode maven metadata --- src/main/java/org/itxtech/mcl/component/Repository.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/org/itxtech/mcl/component/Repository.java b/src/main/java/org/itxtech/mcl/component/Repository.java index 8f0e3c5..53158d2 100644 --- a/src/main/java/org/itxtech/mcl/component/Repository.java +++ b/src/main/java/org/itxtech/mcl/component/Repository.java @@ -88,6 +88,7 @@ public PackageInfo fetchPackage(String id) throws Exception { public Document fetchMavenMetadata(String id) throws Exception { for (var repo : loader.config.mavenRepo) { try { + if (httpHead(repo + "/" + transformId(id)+ "/maven-metadata.xml").statusCode() != 200) continue; var content = httpGet("/" + transformId(id) + "/maven-metadata.xml", repo); var factory = DocumentBuilderFactory.newInstance(); factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); From 2e25501ee088d92c02b279d26caef694356f0cfd Mon Sep 17 00:00:00 2001 From: MrXiaoM Date: Sun, 24 Dec 2023 13:25:28 +0800 Subject: [PATCH 4/7] nightly version not contains `release` tag --- src/main/java/org/itxtech/mcl/component/Repository.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/org/itxtech/mcl/component/Repository.java b/src/main/java/org/itxtech/mcl/component/Repository.java index 53158d2..00d9279 100644 --- a/src/main/java/org/itxtech/mcl/component/Repository.java +++ b/src/main/java/org/itxtech/mcl/component/Repository.java @@ -125,6 +125,7 @@ public String getLatestVersionFromMaven(String id, String channel) throws Except return map.lastEntry().getValue(); } } + else return data.getElementsByTagName("latest").item(0).getTextContent(); } return data.getElementsByTagName("release").item(0).getTextContent(); } From c2d1d197718a35d24c64778741afa4d74785e27e Mon Sep 17 00:00:00 2001 From: MrXiaoM Date: Sun, 24 Dec 2023 13:26:40 +0800 Subject: [PATCH 5/7] node text content is correct --- src/main/java/org/itxtech/mcl/component/Repository.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/itxtech/mcl/component/Repository.java b/src/main/java/org/itxtech/mcl/component/Repository.java index 00d9279..7977ebb 100644 --- a/src/main/java/org/itxtech/mcl/component/Repository.java +++ b/src/main/java/org/itxtech/mcl/component/Repository.java @@ -157,7 +157,7 @@ public String getJarUrl(MclPackage pkg, PackageInfo info) { var base = baseFolder + getPackageFromId(pkg.id) + "-" + pkg.version; if (pkg.version.endsWith("-SNAPSHOT")) { try { - var real = getSnapshotJarUrl(baseFolder); + var real = getSnapshotJarUrl(baseFolder, getPackageFromId(pkg.id), pkg.version); if (!real.isEmpty()) return real; } catch (Exception e) { loader.logger.logException(e); @@ -209,7 +209,7 @@ public static String findNodeValue(NodeList nodes, String name, String defValue) for (int i = 0; i < nodes.getLength(); i++) { var node = nodes.item(i); if (node.getNodeName().equals(name)) { - return node.getNodeValue(); + return node.getTextContent().trim(); } } return defValue; From dedbc22dbe91c8820faf56bd3ab148ab01724d66 Mon Sep 17 00:00:00 2001 From: MrXiaoM Date: Sun, 24 Dec 2023 13:27:29 +0800 Subject: [PATCH 6/7] fix bad url --- src/main/java/org/itxtech/mcl/component/Repository.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/itxtech/mcl/component/Repository.java b/src/main/java/org/itxtech/mcl/component/Repository.java index 7977ebb..1f3e8d0 100644 --- a/src/main/java/org/itxtech/mcl/component/Repository.java +++ b/src/main/java/org/itxtech/mcl/component/Repository.java @@ -180,7 +180,7 @@ public String getJarUrl(MclPackage pkg, PackageInfo info) { private String getSnapshotJarUrl(String baseFolder) throws Exception { if (httpHead(baseFolder + "maven-metadata.xml").statusCode() != 200) return ""; - var content = httpGet(baseFolder + "maven-metadata.xml"); + var content = httpGet(baseFolder + "maven-metadata.xml", ""); var factory = DocumentBuilderFactory.newInstance(); factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); var document = factory.newDocumentBuilder().parse(new InputSource(new StringReader(content))); From dacb7eefeac8df011c5e6eef7987b5abaf87801b Mon Sep 17 00:00:00 2001 From: MrXiaoM Date: Sun, 24 Dec 2023 13:28:23 +0800 Subject: [PATCH 7/7] override snapshots download file name --- .../org/itxtech/mcl/component/Repository.java | 23 +++++++++++-------- .../itxtech/mcl/module/builtin/Updater.java | 12 +++++++--- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/itxtech/mcl/component/Repository.java b/src/main/java/org/itxtech/mcl/component/Repository.java index 1f3e8d0..0410a5c 100644 --- a/src/main/java/org/itxtech/mcl/component/Repository.java +++ b/src/main/java/org/itxtech/mcl/component/Repository.java @@ -178,28 +178,33 @@ public String getJarUrl(MclPackage pkg, PackageInfo info) { return ""; } - private String getSnapshotJarUrl(String baseFolder) throws Exception { + private String getSnapshotJarUrl(String baseFolder, String packageName, String packageVersion) throws Exception { if (httpHead(baseFolder + "maven-metadata.xml").statusCode() != 200) return ""; var content = httpGet(baseFolder + "maven-metadata.xml", ""); var factory = DocumentBuilderFactory.newInstance(); factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); var document = factory.newDocumentBuilder().parse(new InputSource(new StringReader(content))); var elements = document.getElementsByTagName("snapshotVersion"); + var versions = new HashMap(); for (int i = 0; i < elements.getLength(); i++) { var version = elements.item(i).getChildNodes(); var classifier = findNodeValue(version, "classifier", ""); var extension = findNodeValue(version, "extension", ""); var value = findNodeValue(version, "value", ""); var suffix = (classifier.isEmpty() ? "" : ("-" + classifier)) + "." + extension; - if (loader.config.archiveSuffix.contains(suffix)) { - var real = baseFolder + value + suffix; - try { - if (httpHead(real).statusCode() == 200) { - return real; - } - } catch (Exception e) { - loader.logger.logException(e); + + var real = baseFolder + packageName + "-" + value + suffix; + versions.put(suffix, packageName + "-" + packageVersion + suffix + "|" + real); + } + for (var suf : loader.config.archiveSuffix) { + var real = versions.get(suf); + if (real == null) continue; + try { + if (httpHead(real.split("\\|")[1]).statusCode() == 200) { + return real; } + } catch (Exception e) { + loader.logger.logException(e); } } return ""; diff --git a/src/main/java/org/itxtech/mcl/module/builtin/Updater.java b/src/main/java/org/itxtech/mcl/module/builtin/Updater.java index 393d943..42ada75 100644 --- a/src/main/java/org/itxtech/mcl/module/builtin/Updater.java +++ b/src/main/java/org/itxtech/mcl/module/builtin/Updater.java @@ -181,9 +181,15 @@ public void downloadFile(MclPackage pack, Repository.PackageInfo info) { ); return; } - var index = jarUrl.lastIndexOf(name); - if (index != -1) { - jar = jarUrl.substring(index); + if (jarUrl.contains("|")) { + var split = jarUrl.split("\\|"); + jar = split[0]; + jarUrl = split[1]; + } else { + var index = jarUrl.lastIndexOf(name); + if (index != -1) { + jar = jarUrl.substring(index); + } } down(jarUrl, new File(dir, jar));