如何查看jar包的版本信息


=Start=

缘由:

需要知道一种通用的方法以获取jar包的版本信息,因此有了此文。

搜索关键字:
how to check spring version
JAR 文件 规范
获取jar包版本信息
参考解答:

JAR文件本质上是一个zip文件,包含一个可选的META-INF目录。多数情况下,JAR文件并不是类文件和/或资源文件的简单聚合档案。他们经常用作应用程序和扩展的构建基础。如果存在的话,META-INF目录用来存储包和扩展的配置数据,包括安全、版本、扩展和服务。

将jar包解压后读取文件:
./META-INF/MANIFEST.MF
即可找到对应的版本信息。

$ unzip -p catalina.jar "META-INF/MANIFEST.MF"
...
Manifest-Version: Defines the manifest file version. The value is a legitimate version number, as described in the above spec.
...
Signature-Version: Defines the signature version of the jar file. The value should be a valid version-number string.
...
Implementation-Version: The value is a string that defines the version of the extension implementation.
...
Specification-Version: The value is a string that defines the version of the extension specification.
...

==

// 下面这个是获取Spring版本信息的代码(http://stackoverflow.com/a/947679)
import org.springframework.core.SpringVersion;
public class VersionChecker
{
    public static void main(String [] args)
    {
        System.out.println("version: " + SpringVersion.getVersion());
    }
}

==

针对Maven管理的Jar包
在 pom.properties 中包含了jar包的版本号信息

==

针对ANT管理的Jar包
在 build.xml 里建立一个存放版本号的属性,并在打Jar包时,将版本号压入MANIFEST文件。程序在执行的时候,读取Jar包的MANIFEST来获取版本号。

====

#查看jar包中都有哪些文件:
$ unzip -l catalina.jar
$ jar -tvf catalina.jar

#从 catalina.jar 中解压出文件 ServerInfo.properties (需要知道文件的路径信息):
$ jar xf catalina.jar org/apache/catalina/util/ServerInfo.properties

#将修改后的文件重新打包至 catalina.jar 中:
$ jar uf catalina.jar org/apache/catalina/util/ServerInfo.properties

#示例 1: 将两个类文件归档到一个名为 classes.jar 的归档文件中:
$ jar cvf classes.jar Foo.class Bar.class

#示例 2: 使用现有的清单文件 'mymanifest' 并将 foo/ 目录中的所有文件归档到 'classes.jar' 中:
$ jar cvfm classes.jar mymanifest -C foo/ .

=EOF=

,

《“如何查看jar包的版本信息”》 有 23 条评论

  1. 在部分情况下,通过pom.xml无法找到「间接」依赖的jar包信息;
    但是我们可以通过Maven把运行时依赖到的内容也统计出来,更准确地讲,是把pom.xml中scope为compile和runtime级别的依赖都进行统计,这里可以使用 `mvn dependency:tree -Dverbose` 查看所有的依赖,简单的 `mvn dependency:tree` 可能显示不全。

  2. 【漏洞预警】Fastjson 远程代码执行漏洞(暂无PoC)
    http://bobao.360.cn/learning/detail/3617.html
    https://bbs.aliyun.com/read/309931.html
    https://github.com/alibaba/fastjson/wiki/security_update_20170315
    `
    检测当前使用版本的是否有问题

    方法一:通过maven dependency检测 如果是maven工程,在代码根目录下执行如下命令,如果有返回,就是需要升级
    mvn dependency:tree | grep “com.alibaba.fastjson:” | grep -v sec01 | grep -v 1.2.25 | grep -v 1.2.26 | grep -v 1.2.27

    方法二:在lib目录下执行如下脚本命令,可以判断版本是否有问题
    ls | grep fastjson | grep jar | grep -v sec01 | grep -v 1.2.25 | grep -v 1.2.26 | grep -v 1.2.27 | grep -v 1.2.28 | grep -v 1.2.29

    方法三:看打开的文件中是否包含fastjson
    sudo -u tomcat lsof -X | grep -v 1.2.25 | grep -v 1.2.26 | grep -v 1.2.27 | grep -v 1.2.28 | grep -v 1.2.29

    通过lsof检测,在tomcat某些场景是检测不出来的,最好在lib目录下用ls检测(第2种方法)。
    `

  3. 依赖检查(OWASP dependency-check is a software composition analysis utility that detects publicly disclosed vulnerabilities in application dependencies.)
    https://github.com/jeremylong/DependencyCheck
    https://www.owasp.org/index.php/OWASP_Dependency_Check

    【知识库】Dependency-check依赖检查工具
    https://mp.weixin.qq.com/s/j37Y1rpQphFFSoAtbzSnMQ

    GitHub引入依赖图和安全预警
    http://www.infoq.com/cn/news/2017/10/github-dependency-graph-security

  4. Mac上用 homebrew 安装的Maven执行报错:
    错误: 找不到或无法加载主类 org.codehaus.plexus.classworlds.launcher.Launcher

    https://tieba.baidu.com/p/3142104595
    http://blog.51cto.com/houenxun/1761956
    http://www.clarkchen.com/mac-%E9%85%8D%E7%BD%AE-maven%E7%9A%84%E4%B8%80%E4%B8%AA%E9%97%AE%E9%A2%98/
    `
    网上的一些解决办法:
    配置PATH时将 $M2_HOME/bin 放在系统 $PATH 前就可以了。
    export M2_HOME=/opt/maven
    export PATH=$M2_HOME/bin:$PATH

    我的实际解决办法,只要在命令前面添加 sudo 即可,例如:
    sudo mvn -version
    `

  5. spring-messaging Remote Code Execution 分析-【CVE-2018-1270】
    https://xz.aliyun.com/t/2252
    https://pivotal.io/security/cve-2018-1270

    CVE-2018-1270:spring-messaging远程代码执行漏洞分析预警
    https://mp.weixin.qq.com/s/zW53wlWm5Bk1ZJV8i3b6QQ
    `
    影响版本
    Spring Framework 5.0 to 5.0.4.
    Spring Framework 4.3 to 4.3.14
    已不支持的旧版本仍然受影响

    修复版本
    5.0.x 用户升级到5.0.5版本
    4.3.x 用户升级到4.3.15版本

    对应jar包名称
    spring-messaging-4.2.1.RELEASE.jar
    `

  6. Maven生成可以直接运行的jar包的多种方式
    https://paper.tuisec.win/detail/5b730de2f09c1d0
    https://blog.csdn.net/xiao__gui/article/details/47341385
    `
    Maven可以使用mvn package指令对项目进行打包,如果使用java -jar xxx.jar执行运行jar文件,会出现”no main manifest attribute, in xxx.jar”(没有设置Main-Class)、ClassNotFoundException(找不到依赖包)等错误。

    要想jar包能直接通过java -jar xxx.jar运行,需要满足:
    1、在jar包中的META-INF/MANIFEST.MF中指定Main-Class,这样才能确定程序的入口在哪里;
    2、要能加载到依赖包。

    使用Maven有以下几种方法可以生成能直接运行的jar包,可以根据需要选择一种合适的方法。
    方法一:使用maven-jar-plugin和maven-dependency-plugin插件打包
    方法二:使用maven-assembly-plugin插件打包
    方法三:使用maven-shade-plugin插件打包
    `

  7. Can’t execute jar- file: “no main manifest attribute”
    直接通过java命令执行jar包报错的解决方法
    https://stackoverflow.com/questions/9689793/cant-execute-jar-file-no-main-manifest-attribute
    `
    1、如果不希望额外指定参数,那就需要jar包本身是一个「可执行的jar包」
    这点可以通过在Maven项目的 pom.xml 中指定 mainClass 实现生成可执行jar包的功能;

    2、虽然jar包本身不是可执行的,但是其中的某个 Class 里有main方法可以执行,可以通过下面的命令选项实现该方法的调用
    java -cp app.jar com.somepackage.SomeClass
    `

  8. 如何快速找到POC/EXP依赖的jar?
    http://gv7.me/articles/2019/quickly-find-jars-that-depend-on-poc-exp/
    http://www.liuhaihua.cn/archives/575573.html
    `
    标题主要是针对安全人员,如果针对是开发人员的话,应该是 如何快速从众多jar中找到目标类?

    在编写Java相关中间件或者CMS的POC/EXP时一般都会依赖它们的某个jar,但它们的jar往往非常多,并且会分散在各个目录下,那么如何快速找到它们呢?

    可以通过编程实现一个小工具,以快速从众多jar中,搜索目标class所在的jar。不区分大小写,支持通配符搜索。
    `

  9. Log4j2报错ERROR StatusLogger Unrecognized format specifier
    https://www.cnblogs.com/yeyang/p/10485790.html
    `
    ERROR StatusLogger Unrecognized format specifier [d]
    ERROR StatusLogger Unrecognized conversion specifier [d] starting at position 16 in conversion pattern.
    `
    https://stackoverflow.com/questions/34945438/log4j2-configuration-not-found-when-running-standalone-application-built-by-shad
    https://stackoverflow.com/questions/27361052/failing-to-load-log4j2-while-running-fatjar

  10. 一行命令同时修改maven项目中多个mudule的版本号
    https://mp.weixin.qq.com/s/RdIStsQbn3WGTGZ2DHQBDg
    `
    maven之所以强大,是因为他有一个牛X的插件机制。我们可以借助一个插件来实现这个功能。

    这个插件就是versions-maven-plugin。使用方法也很简单,就是在最外层的pom文件中的plugins块中,增加一下对应的插件配置。
    `

  11. maven deploy时报错
    https://my.oschina.net/wjzk/blog/602372

    repository element was not specified in the POM inside distributionManagement element or in -DaltDep loymentRepository=id::layout::url parameter
    https://stackoverflow.com/questions/27153024/repository-element-was-not-specified-in-the-pom-inside-distributionmanagement-el
    `
    [ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy (default-deploy) on project client: Deployment failed: repository element was not specified in the POM inside distributionManagement element or in -DaltDeploymentRepository=id::layout::url parameter -> [Help 1]
    [ERROR]
    [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
    [ERROR] Re-run Maven using the -X switch to enable full debug logging.
    [ERROR]
    [ERROR] For more information about the errors and possible solutions, please read the following articles:
    `
    http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

回复 a-z 取消回复

您的电子邮箱地址不会被公开。 必填项已用*标注