Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

支持下载 SNAPSHOT 版本 #192

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 58 additions & 3 deletions src/main/java/org/itxtech/mcl/component/Repository.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -86,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);
Expand Down Expand Up @@ -117,11 +120,12 @@ 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();
}
}
else return data.getElementsByTagName("latest").item(0).getTextContent();
}
return data.getElementsByTagName("release").item(0).getTextContent();
}
Expand Down Expand Up @@ -149,8 +153,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, getPackageFromId(pkg.id), pkg.version);
if (!real.isEmpty()) return real;
} catch (Exception e) {
loader.logger.logException(e);
}
continue;
}
for (var suf : loader.config.archiveSuffix) {
var real = base + suf;
try {
Expand All @@ -165,6 +178,48 @@ public String getJarUrl(MclPackage pkg, PackageInfo info) {
return "";
}

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<String,String>();
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;

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 "";
}

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.getTextContent().trim();
}
}
return defValue;
}

public String getMetadataUrl(MclPackage pkg, PackageInfo info) {
if (info != null && info.repo != null) {
var repoInfo = info.repo.get(pkg.version);
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/org/itxtech/mcl/module/builtin/Updater.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down