简体   繁体   中英

Get Maven profile name or id in java

I have a java project (no spring) and I'm trying to get the current profile in java, but I can't find how to do it

I tried to use:

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();
    }

this is my pom:

<properties>
    <profile.name>${current.profile}</profile.name>

and:

<profile>
    <id>turchia</id>
    <activation>
        <activeByDefault>false</activeByDefault>
    </activation>
    <properties>
        <current.profile>-- TURCHIA --</current.profile>
    </properties>
</profile>

this is my app.properties:

profile.na= ${profile.name}
profile.me= ${current.profile}

this is my filter resources:

<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>

but it doesn't work, any other suggests?

I am not sure why you added the resource configuration inside executions of the war plugin.

The filtering can happen in all kinds of build, not just when packaging the war, so I would add the following to the pom. (I just add the outer tags to show where in the hierarchy to place it, it is not a complete 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>

I have to sections with resources there so only application.properties is filtered, but the other resources are copied as well, just as-is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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