![](/img/trans.png)
[英]Get list of activated profile name during run time in maven java project
[英]Get Maven profile name or id in java
我有一個 java 項目(沒有彈簧),我試圖在 java 中獲取當前配置文件,但我找不到如何做
我嘗試使用:
Properties prop = new Properties();
// ClassLoader loader = Thread.currentThread().getContextClassLoader();
// InputStream stream = loader.getResourceAsStream("/config/archicon.properties");
try {
prop.load(Archicon.class.getClassLoader().getResourceAsStream("config/archicon.properties"));
System.out.println(prop.getProperty("profile.na"));
System.out.println(prop.getProperty("profile.me"));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
這是我的 pom:
<properties>
<profile.name>${current.profile}</profile.name>
和:
<profile>
<id>turchia</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<current.profile>-- TURCHIA --</current.profile>
</properties>
</profile>
這是我的應用程序屬性:
profile.na= ${profile.name}
profile.me= ${current.profile}
這是我的過濾器資源:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>copy-resources-1</id>
<configuration>
<goals>
<goal>write-project-properties</goal>
</goals>
<resources>
<resource>
<directory>src/main/resources/config</directory>
<filtering>true</filtering>
<includes>
<include>*.properties</include>
</includes>
<targetPath>${project.basedir}/target</targetPath>
</resource>
</resources>
</configuration>
</execution>
<execution>
<id>copy-resources-2</id>
<configuration>
<!-- <warSourceDirectory>${project.basedir}/WebContent</warSourceDirectory> -->
<webXml>${current.webXml}</webXml>
<failOnMissingWebXml>false</failOnMissingWebXml>
<!-- <warSourceExcludes>css/</warSourceExcludes> -->
<webResources>
<resource>
<directory>${jbossWeb.folder}/${current.jbossWeb}</directory>
<targetPath>WEB-INF</targetPath>
</resource>
</webResources>
</configuration>
</execution>
</executions>
</plugin>
但它不起作用,還有其他建議嗎?
我不確定你為什么在 war 插件的執行中添加資源配置。
過濾可以發生在各種構建中,而不僅僅是打包戰爭時,所以我會在 pom.xml 中添加以下內容。 (我只是添加了外部標簽以顯示在層次結構中放置它的位置,它不是一個完整的 pom)
<project>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>application.properties</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<excludes>
<exclude>application.properties</exclude>
</excludes>
</resource>
</resources>
</build>
</project>
我必須在那里包含資源的部分,所以只有application.properties被過濾,但其他資源也被復制,就像原樣一樣。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.