You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

非Spring Java项目获取Maven Profile名称/ID失败,求可行方案

解决非Spring Java项目获取激活Maven Profile的问题

你的核心问题在于Maven资源过滤未正确生效,导致properties文件里的占位符没有被替换成实际的Profile属性值。以下是可行的修复方案:

一、修正Maven资源过滤配置

放弃在maven-war-plugin里配置资源过滤,改用专门的maven-resources-plugin处理,它是Maven默认负责资源复制和过滤的插件,配置更可靠:

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <includes>
                <include>**/*.properties</include>
            </includes>
        </resource>
    </resources>

    <plugins>
        <!-- 确保资源过滤在构建时执行 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>3.3.1</version>
            <executions>
                <execution>
                    <id>default-resources</id>
                    <phase>process-resources</phase>
                    <goals>
                        <goal>resources</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

二、调整Profile与Properties配置

在Profile里直接定义要暴露的Profile ID和名称,避免多层变量引用的混乱:

1. Pom.xml中的Profile配置

<profiles>
    <profile>
        <id>turchia</id>
        <activation>
            <activeByDefault>false</activeByDefault>
        </activation>
        <properties>
            <!-- 直接定义profile的ID和显示名称 -->
            <active.profile.id>turchia</active.profile.id>
            <active.profile.name>-- TURCHIA --</active.profile.name>
        </properties>
    </profile>
    <!-- 可添加其他Profile示例 -->
    <profile>
        <id>italy</id>
        <properties>
            <active.profile.id>italy</active.profile.id>
            <active.profile.name>-- ITALY --</active.profile.name>
        </properties>
    </profile>
</profiles>

2. app.properties文件内容

# 直接引用Profile中定义的属性
active.profile.id=${active.profile.id}
active.profile.name=${active.profile.name}

三、正确读取Properties文件

确保代码读取的是target目录下经过过滤后的properties文件(构建后生成的文件),而不是源码目录里的原始文件:

Properties prop = new Properties();
try (InputStream stream = Archicon.class.getClassLoader().getResourceAsStream("config/archicon.properties")) {
    if (stream == null) {
        throw new FileNotFoundException("Properties file not found in classpath");
    }
    prop.load(stream);
    System.out.println("激活的Profile ID: " + prop.getProperty("active.profile.id"));
    System.out.println("激活的Profile名称: " + prop.getProperty("active.profile.name"));
} catch (IOException e) {
    e.printStackTrace();
}

四、构建时激活Profile

执行Maven构建命令时,通过-P参数指定要激活的Profile:

mvn clean package -P turchia

额外方案:直接生成项目属性文件

如果不想用资源过滤,可以用maven-help-plugin将所有Maven属性(包括激活的Profile信息)写入一个properties文件,然后在Java中读取:

Pom.xml添加插件配置

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-help-plugin</artifactId>
    <version>3.4.0</version>
    <executions>
        <execution>
            <id>write-project-properties</id>
            <phase>process-resources</phase>
            <goals>
                <goal>write-project-properties</goal>
            </goals>
            <configuration>
                <outputFile>${project.build.outputDirectory}/project.properties</outputFile>
            </configuration>
        </execution>
    </executions>
</plugin>

该插件会在target/classes下生成project.properties,里面包含activeProfiles属性,直接读取即可:

Properties prop = new Properties();
try (InputStream stream = Archicon.class.getClassLoader().getResourceAsStream("project.properties")) {
    prop.load(stream);
    System.out.println("激活的Profiles: " + prop.getProperty("activeProfiles"));
} catch (IOException e) {
    e.printStackTrace();
}

内容的提问来源于stack exchange,提问作者Robs

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 14:01:30