如何基于EnvName参数让exec-maven-plugin有条件执行npm install命令
Angular应用打包Liberty时按环境控制npm可选依赖安装
问题描述
我有一个名为MyApp的Angular应用,需要打包为MyApp.war部署到Liberty服务器。已通过exec-maven-plugin配置在prepare-package阶段执行npm install,但当前会始终安装package.json中所有依赖。希望通过传入EnvName参数,控制是否安装optionalDependencies:当EnvName=test时执行npm install --include=optional,其他情况执行npm install --omit=optional。
之前尝试的配置无效,无论传入什么参数都不会安装可选依赖:
<arguments> <argument>install</argument> <argument> <!-- If EnvName is 'test', include optional dependencies --> ${EnvName.equals('test') ? '--include=optional' : '--omit=optional'} </argument> </arguments>
错误原因
Maven的属性表达式不支持直接调用Java方法(比如equals()),上述配置会把整个表达式字符串直接传递给npm,而非解析为条件判断后的参数,导致npm无法识别该参数,因此不会按预期安装可选依赖。
解决方案
方案一:使用Maven Profiles(兼容性好)
通过定义两个Profile,分别对应test环境和非test环境,自动根据EnvName参数激活对应的Profile,执行不同的npm命令:
<profiles> <!-- test环境:安装可选依赖 --> <profile> <id>env-test</id> <activation> <property> <name>EnvName</name> <value>test</value> </property> </activation> <build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>3.0.0</version> <executions> <execution> <id>npm-install</id> <phase>prepare-package</phase> <goals> <goal>exec</goal> </goals> <configuration> <executable>npm</executable> <arguments> <argument>install</argument> <argument>--include=optional</argument> </arguments> </configuration> </execution> </executions> </plugin> </plugins> </build> </profile> <!-- 非test环境:忽略可选依赖 --> <profile> <id>env-non-test</id> <activation> <property> <name>EnvName</name> <value>!test</value> </property> </activation> <build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>3.0.0</version> <executions> <execution> <id>npm-install</id> <phase>prepare-package</phase> <goals> <goal>exec</goal> </goals> <configuration> <executable>npm</executable> <arguments> <argument>install</argument> <argument>--omit=optional</argument> </arguments> </configuration> </execution> </executions> </plugin> </plugins> </build> </profile> </profiles>
使用方式:
执行Maven命令时传入EnvName参数即可:
- 安装可选依赖:
mvn clean package -DEnvName=test - 忽略可选依赖:
mvn clean package -DEnvName=prod(或其他非test值)
方案二:使用Maven条件表达式(简洁,需Maven 3.2.1+)
利用Maven 3.2.1及以上版本支持的${if}条件表达式,提前定义npm参数属性,再在exec配置中引用:
<properties> <!-- 根据EnvName判断npm安装参数 --> <npm.install.option>${if:equals(${EnvName},test)--include=optional;--omit=optional}</npm.install.option> </properties> <build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>3.0.0</version> <executions> <execution> <id>npm-install</id> <phase>prepare-package</phase> <goals> <goal>exec</goal> </goals> <configuration> <executable>npm</executable> <arguments> <argument>install</argument> <argument>${npm.install.option}</argument> </arguments> </configuration> </execution> </executions> </plugin> </plugins> </build>
使用方式:
和方案一相同,执行命令时传入EnvName参数即可,例如:mvn clean package -DEnvName=test 或 mvn clean package -DEnvName=uat
内容的提问来源于stack exchange,提问作者pixel
相关产品推荐
相关产品推荐

