对于Maven项目来访问自己的资源

问题描述 投票:1回答:1

我有两个项目:项目B项目(将A列为B的依赖项之一)

A有一种依赖A资源的方法

当B调用A的方法时,A最终访问B的资源文件夹,因此无法找到A自己的资源文件。

有什么方法吗?

maven resources dependencies
1个回答
0
投票

为了确保项目始终访问自己的资源,您需要使用Class#getResource方法加载它们。

例:

public class MyCalledClass{
    public static void loadResource() {
        new File(MyCalledClass.class.getResource("file.txt").getPath()); // Will retrieve the file called "file.txt"
                                                                         // in the project where this class is

        new File("file.txt");  // Will retrieve the file called "file.txt" in the project calling this method
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.