带有Gradle的OpenCSV:如何在src / main / resources中引用资源文件

时间:2019-05-22 16:57:03

标签: gradle opencsv

我正在使用OpenCSV方法“ CsvToBeanBuilder”读取要从src / main / resources中的csv文件加载的数据。

    private static FileReader readDataFromCSV(String filename) throws IOException {

    return new FileReader("src/main/resources/"+filename);

}

private static void loadJobFunctions() throws IOException {

    List<JobFunctions> jobFunctions = new CsvToBeanBuilder(readDataFromCSV("JobFunctionNames.csv"))
            .withType(JobFunctions.class).build().parse();
    ..... 
}

为了使OpenCSV在我的IDE中运行我的主方法时可以拾取文件,我需要提供相对于项目根目录的路径。

("src/main/resources/"+filename)

当我使用Gradle构建jar时,它是从resources文件夹中拾取文件,但将其放置在JAR本身的根目录中。因此,当我执行jar时,出现文件未找到错误。这里首选的解决方案是什么?

仅供参考-我的build.gradle文件包含此代码...

jar {
    manifest {
        attributes 'Main-Class': 'DataLoad.DataLoad'
    }
    from configurations.runtime.collect { zipTree(it) }
}

0 个答案:

没有答案
相关问题