[英]Specifying Java path for properties file
我有一個使用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:”嗎?
如果指定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>
對我來說,如果您不打算將此文件用作...過濾src/main/filters
則將屬性文件放在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配置文件和過濾器來管理多個環境部署。 看一下:
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")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.