繁体   English   中英

在 java 中获取 Maven profile name or id

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM