JUnit + Maven:访问$ {project.build.directory}值
在我的unit testing中,我想在$ {project.build.directory}中创建一个tmp目录。 如何在unit testing中访问$ {project.build.directory}的值?
我能想到的一种方法是在测试资源中提供过滤的属性文件,该文件保留该值。 (我还没有尝试,但我认为这应该有效。)
是否有直接访问/传递此属性值的方法?
此致,弗洛里安
我以前用过这样的东西并取得了一些成功。 即使不使用Maven,unit testing仍将运行,目标目录仍然会相对于运行测试的地方的cwd创建两个目录。
public File targetDir(){ String relPath = getClass().getProtectionDomain().getCodeSource().getLocation().getFile(); File targetDir = new File(relPath+"../../target"); if(!targetDir.exists()) { targetDir.mkdir(); } return targetDir; }
我认为如果你按照http://maven.apache.org/plugins/maven-surefire-plugin/examples/system-properties.html中的说明配置surefire-plugin,使用系统属性是非常简单的。 即便是直接回答你问题的例子:
[...] org.apache.maven.plugins maven-surefire-plugin 2.9 propertyValue ${project.build.directory} [...] [...]
请记住,您的unit testing不必从Maven surefire插件执行,因此${project.build.directory}
属性可能不可用。 为了使您的测试更具可移植性,我宁愿建议使用File.createTempFile()
。