java - 有没有办法找出一个类在哪个 Maven Artifact 中?

标签 java spring maven dependencies intellij-14

我正在尝试升级我们的 Spring 版本并使用 Spring IO Platform BOM 来这样做,但是我们的一些类已经丢失(移动到其他 Artifact 中)或者不再依赖于我引入的某些东西。我试图找出它们最初属于哪个包(一个例子是 CSVStrategy )。其中一些依赖项(如 WhitespaceTokenizer)有十几个可能提供它的 Artifact 名称,为了找到正确的升级路径,我需要弄清楚它当前来自哪里。

最佳答案

一种可能的方法是获取资源(类)位置。如果该类来自 jar 文件,您至少会得到 jar 名称。从中您应该能够识别 Maven Artifact 。

someClass.getProtectionDomain().getCodeSource().getLocation().toURI();

或者使用 ResourceLoader 和记录器,您可以打印类路径/servlet 路径上所有类的列表。

@Autowired 
ResourceLoader resourceLoader;

public void printResourceLocations() {
    PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(resourceLoader);
    Resource[] resources = resolver.getResources("classpath*:com/**/*.class"));
    for (Resource resource : resources) {
        log.info(resource.getURI()); 
        // Not sure if that works, probably getFile() is ok?
    }
}    

关于java - 有没有办法找出一个类在哪个 Maven Artifact 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28175227/

相关文章:

java - 如何在对 Spring MVC Controller 的 GET 请求中接受日期参数?

java - 使用 Lucene 获得更好的搜索结果

spring - 为什么 Spring 3 在使用 Maven 时需要显式的 cglib 依赖声明?

java - 由于多次引用同一 Java 类,Eclipse(Java 项目)中出现构建错误

maven - 用于类似管道的 One Jenkinsfile 或专用 Jenkinsfile 哪个更好

java - 了解 Java 匿名类?

java - WSServletContextListener 和 WSServlet 的职责是什么

java - 在 spring 中使用第三方库的多个实现 : stuck finding a clean approach

java - 使用 Spring Boot 查询 MySQL 数据库时,SQL 查询返回未找到列 'id'

node.js - 无法在目录中运行程序 "npm"