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

如何基于Maven Profile条件控制前端构建任务的执行?

问题

我们通过Maven将代码部署到Adobe AEM环境,pom.xml里已经为开发(dev)和预发布(staging)等环境配置了对应的Profile。当前使用frontend-maven-plugin执行SCSS转CSS、JS压缩等前端构建任务。

需求:

  • 执行命令mvn clean install -PaemDev1部署到Dev1环境时,不运行minify-js任务(开发环境不需要)
  • 部署到aemStaging环境时,必须运行minify-js任务

以下是简化后的pom.xml(已省略属性配置):

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<build>
    <resources>
        <!-- a list of stuff -->
    </resources>

    <plugins>
        <plugin>
            <groupId>com.github.eirslett</groupId>
            <artifactId>frontend-maven-plugin</artifactId>
            <version>1.6</version>
            <executions>
                <execution>
                    <id>install node and npm</id>
                    <goals>
                        <goal>install-node-and-npm</goal>
                    </goals>
                    <configuration>
                        <nodeVersion>v20.10.0</nodeVersion>
                        <npmVersion>10.2.3</npmVersion>
                        <workingDirectory>src/main/content/ui</workingDirectory>
                    </configuration>
                </execution>
                <execution>
                    <id>npm install</id>
                    <goals>
                        <goal>npm</goal>
                    </goals>
                    <configuration>
                        <arguments>install</arguments>
                        <workingDirectory>src/main/content/ui</workingDirectory>
                    </configuration>
                </execution>
                <execution>
                    <id>Compile sass to css</id>
                    <phase>generate-resources</phase>
                    <goals>
                        <goal>npm</goal>
                    </goals>
                    <configuration>
                        <arguments>run compileCss</arguments>
                        <workingDirectory>src/main/content/ui</workingDirectory>
                    </configuration>
                </execution>
                <execution>
                    <id>minify-js</id>
                    <phase>generate-resources</phase>
                    <goals>
                        <goal>npm</goal>
                    </goals>
                    <configuration>
                        <arguments>run minifyJs</arguments>
                        <workingDirectory>src/main/content/ui</workingDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
<profiles>
    <profile>
        <id>aemDev1</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>com.day.jcr.vault</groupId>
                    <artifactId>content-package-maven-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>install-package-author</id>
                            <goals>
                                <goal>install</goal>
                            </goals>
                            <configuration>
                                <targetURL>http://${aem.dev1.author.host}:${aem.dev1.author.port}/crx/packmgr/service.jsp</targetURL>
                                <failOnError>true</failOnError>
                                <failOnMissingEmbed>true</failOnMissingEmbed>
                                <userId>${aem.dev1.author.username}</userId>
                                <password>${aem.dev1.author.password}</password>
                            </configuration>
                        </execution>
                        <execution>
                            <id>install-package-publish1</id>
                            <goals>
                                <goal>install</goal>
                            </goals>
                            <configuration>
                                <targetURL>http://${aem.dev1.publish1.host}:${aem.dev1.publish1.port}/crx/packmgr/service.jsp</targetURL>
                                <failOnError>true</failOnError>
                                <failOnMissingEmbed>true</failOnMissingEmbed>
                                <userId>${aem.dev1.publish1.username}</userId>
                                <password>${aem.dev1.publish1.password}</password>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
    <profile>
        <id>aemStaging</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>com.day.jcr.vault</groupId>
                    <artifactId>content-package-maven-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>install-package-author</id>
                            <goals>
                                <goal>install</goal>
                            </goals>
                            <configuration>
                                <targetURL>http://${aem.stg.author.host}:${aem.stg.jenkins.author.port}/crx/packmgr/service.jsp</targetURL>
                                <failOnError>true</failOnError>
                                <failOnMissingEmbed>true</failOnMissingEmbed>
                                <userId>${aem.stg.author.username}</userId>
                                <password>${aem.stg.author.password}</password>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>
</project>
解决方案

推荐两种轻量改动的实现方式,都不需要大幅修改原有pom结构:

方法一:在aemDev1 Profile中禁用minify-js任务

直接在aemDev1的<build><plugins>里,添加对frontend-maven-plugin的配置,通过匹配execution id来禁用指定任务:

修改后的aemDev1 Profile代码:

<profile>
    <id>aemDev1</id>
    <build>
        <plugins>
            <!-- 保留原有的content-package-maven-plugin配置 -->
            <plugin>
                <groupId>com.day.jcr.vault</groupId>
                <artifactId>content-package-maven-plugin</artifactId>
                <executions>
                    <!-- 原execution内容不变 -->
                </executions>
            </plugin>
            <!-- 新增:禁用minify-js任务 -->
            <plugin>
                <groupId>com.github.eirslett</groupId>
                <artifactId>frontend-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>minify-js</id>
                        <!-- 设置phase为none,Maven将跳过该任务 -->
                        <phase>none</phase>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</profile>

原理:Maven会合并主build和激活Profile的插件配置,这里通过相同的execution id覆盖主配置中的任务阶段,设为none后该任务不会执行;而staging环境无此覆盖,会正常运行minify-js。

方法二:将minify-js任务移至aemStaging Profile

如果更倾向于“仅staging环境加载该任务”,可以:

  1. 删除主build中frontend-maven-plugin里的minify-js execution;
  2. 在aemStaging Profile的<plugins>中添加该任务:
<plugin>
    <groupId>com.github.eirslett</groupId>
    <artifactId>frontend-maven-plugin</artifactId>
    <version>1.6</version>
    <executions>
        <execution>
            <id>minify-js</id>
            <phase>generate-resources</phase>
            <goals>
                <goal>npm</goal>
            </goals>
            <configuration>
                <arguments>run minifyJs</arguments>
                <workingDirectory>src/main/content/ui</workingDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

推荐选择

方法一改动最小,仅需在dev profile中添加几行配置,无需修改主build内容,完全符合“不大幅修改pom.xml”的要求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 16:15:53