Skip to content

Commit

Permalink
fix version
Browse files Browse the repository at this point in the history
  • Loading branch information
yangyang0507 committed Oct 26, 2024
1 parent 9a777c6 commit 123593b
Showing 1 changed file with 34 additions and 4 deletions.
38 changes: 34 additions & 4 deletions src/components/LastedDependency.astro
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ const {groupId, artifactId} = Astro.props as Props;
// 调用中央库 API 返回最新的 Jar 包版本信息
const fetchJarVersion = async (groupId: string, artifactId: string) => {
// Maven 中央库的搜索 API,这个 URL 可能需要根据 Maven API 的最新文档进行更新
const searchUrl = `https://search.maven.org/solrsearch/select?q=g:"${encodeURIComponent(groupId)}"+AND+a:"${encodeURIComponent(artifactId)}"&rows=1&wt=json`;
try {
// Maven 中央库的搜索 API,这个 URL 可能需要根据 Maven API 的最新文档进行更新
const searchUrl = `https://search.maven.org/solrsearch/select?q=g:"${encodeURIComponent(groupId)}"+AND+a:"${encodeURIComponent(artifactId)}"&rows=1&wt=json`;
const response = await fetch(searchUrl);
if (!response.ok) {
console.error(`Failed to fetch version for ${groupId}:${artifactId}`);
Expand All @@ -27,8 +26,39 @@ const fetchJarVersion = async (groupId: string, artifactId: string) => {
}
};
const fetchJarVersionByXML = async (groupId: string, artifactId: string) => {
try {
// 构建 metadata XML URL
const groupPath = groupId.replace(/\./g, '/');
const url = `https://repo1.maven.org/maven2/${groupPath}/${artifactId}/maven-metadata.xml`;
// 发送请求
const response = await fetch(url);
if (!response.ok) {
console.error(`Failed to fetch version for ${groupId}:${artifactId}`);
return 'lasted-version';
}
// 解析 XML 响应
const xmlText = await response.text();
// 使用正则表达式获取最新版本信息
const latestVersionMatch = xmlText.match(/<latest>([^<]+)<\/latest>/);
const latestVersion = latestVersionMatch ? latestVersionMatch[1] : null;
if (latestVersion) {
return latestVersion;
} else {
return 'lasted-version';
}
} catch (error) {
console.error("Error fetching Jar version:", error);
return 'lasted-version';
}
}
// 获取版本
const version = await fetchJarVersion(groupId, artifactId);
const version = await fetchJarVersionByXML(groupId, artifactId);
---

<div>
Expand Down

0 comments on commit 123593b

Please sign in to comment.