简体   繁体   English

是否有一种可移植的方式来获取Maven项目中的资源路径?

[英]Is there a portable way to get the path of a resource in a Maven project?

One of my unit tests is driven by test data, stored in src/test/resources/testData.txt . 我的一个单元测试由测试数据驱动,存储在src/test/resources/testData.txt Since Maven will copy the contents of src/test/resources to target/test-classes/ which is on the classpath, I can read that file conveniently from the classpath without caring about its absolute file name. 由于Maven会将src/test/resources的内容复制到target/test-classes/路径上的target/test-classes/ ,因此我可以方便地从类路径中读取该文件而无需关心其绝对文件名。 So far, so good. 到现在为止还挺好。

Now I'm in the process of witing some code that will update that file with data fetched from a remote system. 现在我正在尝试使用从远程系统获取的数据来更新该文件的一些代码。 That code will be run rarely, and not as part of the build. 该代码很少运行,而不是构建的一部分。 The updated file will be committed to the source repository. 更新的文件将提交到源存储库。 In order to append to the file, I need to know the path to the original file in src/test/resources (appending to the one in target/test-classes/ will obviously not work). 为了附加到文件,我需要知道src/test/resources原始文件的路径(追加到target/test-classes/的路径显然不起作用)。

How do I determine that path in a portable way? 如何以便携方式确定路径? I'd rather not hard code src/test/resources/ , because those directory names may actually change. 我宁愿不用硬编码src/test/resources/ ,因为那些目录名实际上可能会改变。 Also, this would break if executed with a current working directory other than my project base directory. 此外,如果使用除项目基目录之外的当前工作目录执行,则会中断。

Any ideas? 有任何想法吗?

The best way would probably be with ${project.basedir}/src/test/resources . 最好的方法可能是使用${project.basedir}/src/test/resources The test resource directory is unlikely to change, and it will safely resolve to the correct path regardless of your working directory. 测试资源目录不太可能更改,无论您的工作目录如何,它都将安全地解析为正确的路径。 Trying to reference a test resource directory via a property is problematic because there can be, and often are, multiple test resources defined. 尝试通过属性引用测试资源目录是有问题的,因为可能并且通常是定义了多个测试资源。 How would you know which one to pick? 你怎么知道选哪一个?

My preferred solution to this is to configure the surefire plugin to expose the ${project.basedir} as an environment variable during test execution. 我首选的解决方案是配置surefire插件,以便在测试执行期间将${project.basedir}公开为环境变量。 Unit tests can then read that information from the environment if required. 如果需要,单元测试可以从环境中读取该信息。 This keeps all configuration in the POM and will require no changes should that path ever change: 这样可以保留POM中的所有配置,并且如果路径发生变化,则不需要更改:

<build>
    <plugins>
        ...
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            ...
            <configuration>
                ...
                <environmentVariables>
                    <maven.project.basedir>${project.basedir}</maven.project.basedir>
                </environmentVariables>
            </configuration>
        </plugin>
        ...
    </plugins>
</build>

Other solutions could be implemented using resource filtering (as suggested by Ryan) or by configuring the surefire plugin to add ${project.basedir}/src/test/resources/ to the classpath. 其他解决方案可以使用资源过滤(如Ryan建议)或配置surefire插件以将${project.basedir}/src/test/resources/到类路径中来实现。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM