指定属性文件的Java路径

问题描述 投票:5回答:3

我有一个使用Maven配置的Java Spring项目。随着单元测试及其配置文件的数量快速增加,我正在尝试将测试配置集中到一个属性文件(同一个属性文件,用于构建项目)。

单元测试位于树中(当然相对于项目路径)

src/test/java/com (...)

这些测试的资源文件位于

src/test/resources(...)

最后,资源文件应该读取的属性文件位于目录中

src/main/filters

现在,我有一个Junit类,我在其中指定配置文件位置,如下所示:


@ContextConfiguration(locations = { "classpath:com/initrode/quartz/SyncManagerJobTest-context.xml"})

在配置文件SyncManagerJobTest-context.xml中有一行


<context:property-placeholder location="/src/main/filters/deploy.local.properties"/>

这导致从目录中读取属性文件。我想读的是属性文件,它位于src / main / filters下。我尝试使用../../向上遍历目录,但这没有帮助。使用classpath:也没有用。我可以使用带有“file:”的绝对路径,但这需要项目中的每个开发人员修改配置,这也不好。

总而言之,问题是:如何强制src / test / resources /中的配置文件读取src / main / filters中的属性文件?

还有一个相关的奖励问题:在Java环境中处理文件时,还有其他修饰符而不是“file:”和“classpath:”吗?

java spring file maven-2 properties
3个回答
5
投票

如果将src/main/filters指定为资源位置,Maven会将资源移动到target/classes,并在构建期间将类编译到同一位置。然后,您没有相对路径来处理,因为它们具有相同的根。如果您不这样做,您的过滤器目录将不会包含在构建中。

更新:当然,您的测试代码会输出到目标/测试类,因此为了简化测试,您可以指定在process-test-resources阶段将src/main/filters复制到target/test-classes。我修改了示例以显示该行为。

如果您还没有这样做,可以使用build-helper-maven-plugin将filters文件夹添加为资源位置。

这样做的配置如下所示:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>build-helper-maven-plugin</artifactId>
  <version>1.3</version>
  <executions>
    <execution>
      <id>add-resource</id>
      <phase>process-test-sources</phase>
      <goals>
        <goal>add-test-resource</goal>
      </goals>
      <configuration>
        <resources>
          <resource>
            <directory>scr/main/filters</directory>
          </resource>
        </resources>
      </configuration>
    </execution> 
  </executions>
</plugin>

5
投票

对我来说,如果您不打算将此文件用作...过滤器,则将属性文件放在src/main/filters中听起来有点奇怪。如果此文件用作标准测试资源,为什么不将它放在src/test/resources中?如果要过滤某些资源文件,为什么不这样做并使用过滤后的资源?我希望看到类似的东西:

<build>
  <!-- Filter resources -->
  <filters>
    <filter>src/main/filters/my-filter.properties</filter>
  </filters>
  <!-- Resources for src/main -->
  <resources>
    <resource>
      <directory>src/main/resources</directory>
      <filtering>true</filtering>
    </resource> 
  </resources>
  <!-- Resources for src/test -->
  <testResources>
    <testResource>
      <directory>src/test/resources</directory>
      <filtering>true</filtering>
    </testResource>
  </testResources>
</build> 

我可能会遗漏一些东西,但我认为你正在混合一些概念。在您的情况下(如果我理解您正在尝试做什么),我将使用maven配置文件和过滤器来管理多个环境部署。看一下:


0
投票

Spring允许我们分别指定测试资源的位置,以便它们仅在测试阶段被占用。通过使用@TestPropertySource("/test.properties")注释,您可以指定要为测试加载的测试属性文件。

@TestPropertySource是一个类级别注释,可用于配置属性文件的位置和内联属性,以便为集成测试加载的ApplicationContext添加到环境中的PropertySource集合中。

您可以在单独的配置类中指定此批注,可以指定在测试期间仅扫描。示例:

@Component
public class ClassUsingProperty {

@Value("${testpropertysource.one}")
private String propertyOne;

public String retrievePropertyOne() {
    return propertyOne;
 }
}

@RunWith(SpringRunner.class)
@ContextConfiguration(classes = ClassUsingProperty.class)
@TestPropertySource
public class DefaultTest {


@Autowired
ClassUsingProperty classUsingProperty;

@Test
public void givenDefaultTPS_whenVariableRetrieved_thenDefaultFileReturned() {
    String output = classUsingProperty.retrievePropertyOne();

    assertThat(output).isEqualTo("default-value");
    }
 }

此外,我们可以更改默认配置文件位置,或添加具有更高优先级的额外属性:

@TestPropertySource(locations = "/other-location.properties",
  properties = "baeldung.testpropertysource.one=other-property-value")
© www.soinside.com 2019 - 2024. All rights reserved.