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

配置Maven Profile:启用k8s时仅排除两类指定开发文件

Maven Profile 配置调整:K8s环境保留指定文件

现有production Profile会从最终Jar包中排除三类开发文件,配置如下:

<profile>
    <id>production</id>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>**/DevAccessConfig.*</exclude>
                        <exclude>application-dev.properties</exclude>
                        <exclude>**/DevFileClient.*</exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</profile>

现在需要实现:执行指定命令时,仅排除前两类文件,将DevFileClient.*重新纳入Jar包,以下是两种可行方案:


方案一:新增k8s Profile

直接新增一个k8s Profile,覆盖maven-jar-plugin的排除规则,只保留前两项排除配置。当同时激活production和k8s时,k8s的插件配置会覆盖production中的对应配置:

<profile>
    <id>k8s</id>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>**/DevAccessConfig.*</exclude>
                        <exclude>application-dev.properties</exclude>
                        <!-- 移除DevFileClient.*的排除规则,将其纳入Jar包 -->
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</profile>

使用方式:执行以下命令即可生效

mvn clean package -Pproduction,k8s

方案二:使用构建属性控制排除规则

在production Profile中引入自定义属性,通过属性值决定是否排除DevFileClient.*,这样可以通过-Dk8s=true参数来动态调整:

首先修改production Profile的配置:

<profile>
    <id>production</id>
    <properties>
        <!-- 默认排除DevFileClient.*,k8s环境下设为true则不排除 -->
        <exclude.dev.file.client>true</exclude.dev.file.client>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>**/DevAccessConfig.*</exclude>
                        <exclude>application-dev.properties</exclude>
                        <!-- 根据属性值决定是否添加该排除规则 -->
                        <exclude>${exclude.dev.file.client}?**/DevFileClient.*</exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</profile>

然后新增一个k8s属性触发的Profile:

<profile>
    <id>k8s</id>
    <activation>
        <property>
            <name>k8s</name>
            <value>true</value>
        </property>
    </activation>
    <properties>
        <!-- k8s环境下不排除DevFileClient.* -->
        <exclude.dev.file.client>false</exclude.dev.file.client>
    </properties>
</profile>

使用方式:执行以下任意命令都可生效

# 方式1:同时激活两个Profile
mvn clean package -Pproduction,k8s
# 方式2:通过命令行参数直接覆盖属性
mvn clean package -Pproduction -Dk8s=true

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 05:32:37