简体   繁体   English

如果清单中未提及,如何查找Jar版本

[英]How to find version of Jar if not mentioned in Manifest

I have a project in which many JARS are checked in but there is no version information for them. 我有一个项目,其中签入了许多JARS,但没有针对它们的版本信息。

I have to implement dependency management using Apache Ivy, but I don't know which versions to point to in my ivy.xml . 我必须使用Apache Ivy来实现依赖关系管理,但是我不知道在ivy.xml要指向哪个版本。 I checked the Manifests and many of them do not have the JAR versions mentioned there. 我检查了清单,其中许多没有此处提到的JAR版本。

Is there another way to find version of JARS? 还有另一种方法来查找JARS版本吗? I understand that finding checksum and comparing them is another option but for that I need to check each of my JAR against checksums of all possible JAR versions and so that is not an option. 我知道查找校验和并进行比较是另一种选择,但是为此,我需要对照所有可能的JAR版本的校验和检查每个JAR,因此这不是一个选择。

Any other suggestions please? 还有其他建议吗?

I would say - there's no other way. 我会说-没有别的办法。 If you have somewhere all the jar versions (of the library in question), you can eg md5 them and then compare with the md5 sum of that unknown jar version you have. 如果某个地方有所有jar版本(所涉及的库),则可以对它们进行md5运算,然后与您拥有的未知jar版本的md5总和进行比较。 I might be wrong but I don't see any other way. 我可能是错的,但没有其他办法。

To add to some previous comments, you can find details about the Maven REST API at http://search.maven.org/#api . 要添加到以前的评论中,您可以在http://search.maven.org/#api上找到有关Maven REST API的详细信息。 The URL of interest to you here is http://search.maven.org/solrsearch/select?q=1 :"SHA-1 checksum". 您在此处感兴趣的URL是http://search.maven.org/solrsearch/select?q=1:“SHA-1校验和”。

I had a case where I needed to find the versions of hundreds of JARs as part of a Java application and did so from 2 sources. 我有一个案例,我需要从2个来源中找到数百个JAR的版本作为Java应用程序的一部分。 First I cracked open the JAR and checked the Manifest.MF file for the JAR version first in the field 'Implementation-Version' and if not there, then 'Bundle-Version'. 首先,我打开JAR并首先在“ Implementation-Version”字段中检查了JAR版本的Manifest.MF文件,如果没有,则在“ Bundle-Version”字段中进行了检查。 The code I used for that is below (obviously you will want some better error handling): 我用于此的代码如下(显然,您将需要更好的错误处理):

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

If I was unable to to get the version from the Manifest file then I computed a SHA-1 checksum of the JAR and searched Maven for it. 如果无法从清单文件中获取版本,则我计算了JAR的SHA-1校验和,并在Maven中进行了搜索。 That code (with again not great error checking) is below: 该代码(再次不是很大的错误检查)如下:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何找到jar文件的清单类? - How to find the manifest class of a jar file? 如何获取 jar 版本 - 校验和方法和清单文件未提供版本 - How to get jar versions - checksum method and manifest file not giving version 当清单文件不包含它时获取jar版本 - Get jar version when manifest file not contains it 尽管明显,罐子找不到主类 - jar could not find main class, despite manifest 找不到主要类别…罐子或清单 - Could not find main class…jar or manifest 如何使用Maven将实现版本值添加到jar清单? - How do I add an Implementation-Version value to a jar manifest using Maven? 如何使用Maven的程序集插件向jar清单添加Implementation-Version值? - How do I add an Implementation-Version value to a jar manifest using Maven's assembly plugin? 如何使用 Package 类的 getImplementationVersion() 方法从 JAR 的清单中获取包版本 - How to obtain a package version from the JAR's manifest, using the getImplementationVersion() method of the Package class Weblogic - 为什么 Java ClassLoader 不从空清单 jar 中提到的 jars 加载类 - Weblogic - Why Java ClassLoader does not load classes from jars mentioned in empty manifest jar 如何找到Spring Data JPA和Spring版本的正确jar文件 - How to find correct jar file of Spring Data JPA and Spring version
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM