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

如何基于Maven Profile控制npm run e2e执行及配置问题解决

问题描述

需要通过Maven Profile控制frontend-maven-plugin中ID为npm run e2e的执行步骤:

  • 默认激活的default Profile:跳过e2e测试
  • 指定-P e2eTests参数时:执行所有步骤,包括e2e测试

此前尝试将配置移到<profiles>标签时出现<sourceDirectory>报错,不清楚如何在Profile中正确配置插件。


解决方案1:通过属性控制执行跳过(推荐)

这种方式不用拆分原有插件配置,仅通过属性开关控制e2e步骤是否执行,逻辑简单且不易出错。

修改后的完整POM示例:

<project xmlns="http://maven.apache.org/POM/4.0.0">
    <modelVersion>X.0.0</modelVersion>
    <parent>
        <groupId>asdasd</groupId>
        <artifactId>asdasd</artifactId>
        <version>0.334.0-SNAPSHOT</version>
        <relativePath>../something/pom.xml</relativePath>
    </parent>

    <!-- ======================================================================= -->
    <!-- P R O J E C T                                                           -->
    <!-- ======================================================================= -->
    <artifactId>dfdfd</artifactId>
    <name>xxxxx</name>
    <packaging>content-package</packaging>

    <properties>
        <sonar.sources>${basedir}/src/main/angular/src</sonar.sources>
        <sonar.tests>${basedir}/src/main/angular/src</sonar.tests>
        <!-- 默认开启e2e跳过 -->
        <skip.e2e.tests>true</skip.e2e.tests>
    </properties>

    <profiles>
        <profile>
            <id>default</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <!-- 保持默认跳过属性不变 -->
        </profile>
        <profile>
            <id>e2eTests</id>
            <properties>
                <!-- 激活此Profile时关闭跳过 -->
                <skip.e2e.tests>false</skip.e2e.tests>
            </properties>
        </profile>
    </profiles>

    <build>
        <resources>
            <resource>
                <directory>${basedir}/src/main/content/jcr_root</directory>
                <excludes>
                    <exclude>**/*.iml</exclude>
                </excludes>
                <targetPath>jcr_root</targetPath>
            </resource>
            <resource>
                <filtering>true</filtering>
                <directory>${basedir}/src/main/content/META-INF/xxx/definition</directory>
                <targetPath>../xxx/META-INF/vault/definition</targetPath>
            </resource>
        </resources>
        <sourceDirectory>${basedir}/src/main/content/jcr_root</sourceDirectory>

        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>xml-maven-plugin</artifactId>
            </plugin>

            <!-- start frontend -->
            <plugin>
                <groupId>com.github.eirslett</groupId>
                <artifactId>frontend-maven-plugin</artifactId>
                <version>1.8.0</version>
                <inherited>false</inherited>

                <configuration>
                    <nodeVersion>v16.17.0</nodeVersion>
                    <npmVersion>8.15.0</npmVersion>
                    <nodeDownloadRoot>xxxxx</nodeDownloadRoot>
                    <npmDownloadRoot>xxxxx</npmDownloadRoot>
                    <installDirectory>src/main/angular</installDirectory>
                    <workingDirectory>src/main/angular</workingDirectory>
                </configuration>

                <executions>
                    <execution>
                        ......
                    </execution>

                    <execution>
                        <id>npm rebuild node-sass</id>
                        <goals>
                            <goal>npm</goal>
                        </goals>
                        <configuration>
                            <arguments>rebuild node-sass</arguments>
                        </configuration>
                    </execution>

                    <execution>
                        <id>npm run e2e</id>
                        <goals>
                            <goal>npm</goal>
                        </goals>
                        <configuration>
                            <arguments>run e2e</arguments>
                            <!-- 绑定属性,控制是否跳过此步骤 -->
                            <skip>${skip.e2e.tests}</skip>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <!-- end frontend -->
        </plugins>
    </build>
</project>

用法说明

  • 默认执行mvn clean install时,e2e步骤会被跳过
  • 需要执行e2e测试时,运行命令:mvn clean install -P e2eTests

解决方案2:将e2e执行步骤放到Profile中

如果一定要把e2e的执行逻辑拆分到Profile里,可按以下方式配置,Maven会自动合并全局插件与Profile中的插件配置:

修改后的完整POM示例:

<project xmlns="http://maven.apache.org/POM/4.0.0">
    <modelVersion>X.0.0</modelVersion>
    <parent>
        <groupId>asdasd</groupId>
        <artifactId>asdasd</artifactId>
        <version>0.334.0-SNAPSHOT</version>
        <relativePath>../something/pom.xml</relativePath>
    </parent>

    <!-- ======================================================================= -->
    <!-- P R O J E C T                                                           -->
    <!-- ======================================================================= -->
    <artifactId>dfdfd</artifactId>
    <name>xxxxx</name>
    <packaging>content-package</packaging>

    <properties>
        <sonar.sources>${basedir}/src/main/angular/src</sonar.sources>
        <sonar.tests>${basedir}/src/main/angular/src</sonar.tests>
    </properties>

    <profiles>
        <profile>
            <id>default</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <!-- 默认不加载e2e执行步骤 -->
        </profile>
        <profile>
            <id>e2eTests</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>com.github.eirslett</groupId>
                        <artifactId>frontend-maven-plugin</artifactId>
                        <version>1.8.0</version>
                        <inherited>false</inherited>
                        <!-- 仅在激活此Profile时添加e2e执行步骤 -->
                        <executions>
                            <execution>
                                <id>npm run e2e</id>
                                <goals>
                                    <goal>npm</goal>
                                </goals>
                                <configuration>
                                    <arguments>run e2e</arguments>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

    <build>
        <resources>
            <resource>
                <directory>${basedir}/src/main/content/jcr_root</directory>
                <excludes>
                    <exclude>**/*.iml</exclude>
                </excludes>
                <targetPath>jcr_root</targetPath>
            </resource>
            <resource>
                <filtering>true</filtering>
                <directory>${basedir}/src/main/content/META-INF/xxx/definition</directory>
                <targetPath>../xxx/META-INF/vault/definition</targetPath>
            </resource>
        </resources>
        <sourceDirectory>${basedir}/src/main/content/jcr_root</sourceDirectory>

        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>xml-maven-plugin</artifactId>
            </plugin>

            <!-- start frontend -->
            <plugin>
                <groupId>com.github.eirslett</groupId>
                <artifactId>frontend-maven-plugin</artifactId>
                <version>1.8.0</version>
                <inherited>false</inherited>

                <configuration>
                    <nodeVersion>v16.17.0</nodeVersion>
                    <npmVersion>8.15.0</npmVersion>
                    <nodeDownloadRoot>xxxxx</nodeDownloadRoot>
                    <npmDownloadRoot>xxxxx</npmDownloadRoot>
                    <installDirectory>src/main/angular</installDirectory>
                    <workingDirectory>src/main/angular</workingDirectory>
                </configuration>

                <executions>
                    <execution>
                        ......
                    </execution>

                    <execution>
                        <id>npm rebuild node-sass</id>
                        <goals>
                            <goal>npm</goal>
                        </goals>
                        <configuration>
                            <arguments>rebuild node-sass</arguments>
                        </configuration>
                    </execution>
                    <!-- 移除原有e2e执行步骤,放到Profile中 -->
                </executions>
            </plugin>
            <!-- end frontend -->
        </plugins>
    </build>
</project>

用法说明

  • 默认执行构建时,不会包含e2e测试步骤
  • 需要执行e2e测试时,运行命令:mvn clean install -P e2eTests

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 08:20:27