【发布时间】:2014-05-24 11:40:15
【问题描述】:
我有一个项目,其中签入了许多 JARS,但没有它们的版本信息。
我必须使用 Apache Ivy 实现依赖管理,但我不知道在我的ivy.xml 中指向哪些版本。我检查了清单,其中许多没有提到的 JAR 版本。
还有其他方法可以找到 JARS 的版本吗?我知道找到校验和并比较它们是另一种选择,但为此我需要根据所有可能的 JAR 版本的校验和检查我的每个 JAR,因此这不是一种选择。
还有其他建议吗?
【问题讨论】:
我有一个项目,其中签入了许多 JARS,但没有它们的版本信息。
我必须使用 Apache Ivy 实现依赖管理,但我不知道在我的ivy.xml 中指向哪些版本。我检查了清单,其中许多没有提到的 JAR 版本。
还有其他方法可以找到 JARS 的版本吗?我知道找到校验和并比较它们是另一种选择,但为此我需要根据所有可能的 JAR 版本的校验和检查我的每个 JAR,因此这不是一种选择。
还有其他建议吗?
【问题讨论】:
我会说 - 没有其他办法。如果你在某个地方有所有 jar 版本(有问题的库),你可以例如md5 它们,然后与您拥有的未知 jar 版本的 md5 总和进行比较。我可能是错的,但我没有看到任何其他方式。
【讨论】:
要添加到以前的一些 cmets,您可以在 http://search.maven.org/#api 找到有关 Maven REST API 的详细信息。您感兴趣的 URL 是 http://search.maven.org/solrsearch/select?q=1:"SHA-1 checksum"。
我有一个案例,我需要查找数百个 JAR 的版本作为 Java 应用程序的一部分,并从 2 个来源中找到。首先,我破解了 JAR,并首先在“Implementation-Version”字段中检查了 JAR 版本的 Manifest.MF 文件,如果没有,则在“Bundle-Version”字段中检查。我使用的代码如下(显然你会想要更好的错误处理):
public String getVersionFromJarManifest(File jarFile){
try {
Manifest manifest = new JarFile(jarFile).getManifest();
Attributes mainAttribs = manifest.getMainAttributes();
String version = mainAttribs.getValue("Implementation-Version");
if(version == null || version == "" || version.isEmpty()){
version = mainAttribs.getValue("Bundle-Version");
}
return version;
} catch (Exception e) {
LOGGER.warn("Manifest not found for {}", jarFile.getPath());
return null;
}
}
如果我无法从 Manifest 文件中获取版本,那么我会计算 JAR 的 SHA-1 校验和并在 Maven 中搜索它。该代码(同样没有很好的错误检查)如下:
public String getVersionFromMavenByChecksum(File jarFile){
String sha = null;
try {
MessageDigest md = MessageDigest.getInstance("SHA1");
FileInputStream fis = new FileInputStream(jarFile);
byte[] dataBytes = new byte[1024];
int nread = 0;
while ((nread = fis.read(dataBytes)) != -1) {
md.update(dataBytes, 0, nread);
}
byte[] mdbytes = md.digest();
//convert the byte to hex format
StringBuffer sb = new StringBuffer("");
for (int i = 0; i < mdbytes.length; i++) {
sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));
}
sha = sb.toString();
} catch (Exception e) {
LOGGER.warn("ERROR processing SHA-1 value for {}", jarFile.getPath());
return null;
}
return getVersionBySha(sha);
}
public String getVersionBySha(String sha){
String version = null;
CloseableHttpClient httpClient = HttpClients.createDefault();
List<NameValuePair> suQueryParams = new ArrayList<>();
suQueryParams.add(new BasicNameValuePair("q", "1: \"" + sha + "\""));
String result = null;
try {
result = MavenApiUtil.apiGETCall("http://search.maven.org/solrsearch/select", suQueryParams, null, httpClient);
} catch (Exception e){
LOGGER.warn("ERROR querying Maven for version for SHA {}", sha);
return null;
}
//Parse response
return version;
}
【讨论】: