本文介绍了JUnit + Maven:访问$ {project.build.directory}值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的单元测试中,我想在$ {project.build.directory}中创建一个tmp目录。如何在单元测试中访问$ {project.build.directory}的值?

In my unit tests I want to create a tmp directory inside the ${project.build.directory}. How can I access the value of ${project.build.directory} inside my unit test?

我能想到的一种方法是提供过滤属性在测试资源中存档,其中包含该值。 (我还没有尝试,但我认为这应该有用。)

One way, which I could think of, is to provide a filtered properties file in the test resources, which holdes that value. (I haven't tried yet, but I think that should work.)

是否有直接访问/传递此属性值的方法?

Is there a direct way to access/ pass this property value?

问候,
Florian

Regards,Florian

推荐答案

我用过这样的东西之前的成功。即使不使用Maven,单元测试仍将运行,目标目录仍然会相对于运行测试的地方的cwd创建两个目录。

I've used something like this with some success before. The unit test will still run even if not using Maven, the target directory will still get created two dirs up relative to the cwd of wherever the tests are run.

public File targetDir(){
  String relPath = getClass().getProtectionDomain().getCodeSource().getLocation().getFile();
  File targetDir = new File(relPath+"../../target");
  if(!targetDir.exists()) {
    targetDir.mkdir();
  }
  return targetDir;
}

这篇关于JUnit + Maven:访问$ {project.build.directory}值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 21:36