如果清单中没有提到,如何找到Jar的版本



我有一个项目,其中签入了许多JARS,但没有它们的版本信息。

我必须使用ApacheIvy实现依赖关系管理,但我不知道在我的ivy.xml中应该指向哪个版本。我检查了Manifests,其中许多都没有上面提到的JAR版本。

有没有其他方法可以找到JARS的版本?我知道查找校验和并比较它们是另一种选择,但为此,我需要对照所有可能的JAR版本的校验和来检查我的每个JAR,所以这不是一种选择。

还有其他建议吗?

我想说,没有其他办法了。如果你在某个地方有所有的jar版本(有问题的库),你可以例如md5它们,然后与你所拥有的未知jar版本的md5总和进行比较。我可能错了,但我看不出其他办法。

要添加到前面的一些注释中,您可以在上找到有关Maven REST API的详细信息http://search.maven.org/#api.您感兴趣的URL是http://search.maven.org/solrsearch/select?q=1:"SHA-1校验和"。

我有一个案例,我需要从两个来源找到数百个JAR的版本,作为Java应用程序的一部分。首先,我打开了JAR,并首先在字段"Implementation version"中检查Manifest.MF文件中的JAR版本,如果没有,则检查"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;
}

最新更新