文档首页> 编译构建 CodeArts Build> 常见问题> Maven构建> 使用exec-maven-plugin插件实现Maven和npm混合编译
更新时间:2024-05-10 GMT+08:00
分享

使用exec-maven-plugin插件实现Maven和npm混合编译

问题现象

Maven项目里包含前端代码,需要npm构建,而系统提供的Maven镜像不包含npm构建环境。

处理办法

Maven插件exec-maven-plugin实现混合编译,首先配置插件,其次配置npm环境,最后执行构建。

  1. pom文件配置。

    每条npm命令都是<executions>标签中的一个<execution>,不建议配置代理和私有的npm镜像仓,而是使用华为开源镜像站,其配置如下:

    <build>
    	<plugins>
    		<plugin>
    			<groupId>org.codehaus.mojo</groupId>
    			<artifactId>exec-maven-plugin</artifactId>
    			<executions>
    				<execution>
    					<id>exec-npm-config</id>
    					<phase>prepare-package</phase>
    					<goals>
    						<goal>exec</goal>
    					</goals>
    					<configuration>
    						<executable>npm</executable>
    						<arguments>
    							<argument>config</argument>
    							<argument>set</argument>
    							<argument>registry</argument>
    							<argument>https://mirrors.xxcloud.com/repository/npm/</argument>
    						</arguments>
    						<!-- <workingDirectory>${basedir}</workingDirectory>-->
    					</configuration>
    				</execution>
    
    				<execution>
    					<id>exec-npm-config-4</id>
    					<phase>prepare-package</phase>
    					<goals>
    						<goal>exec</goal>
    					</goals>
    					<configuration>
    						<executable>npm</executable>
    						<arguments>
    							<argument>config</argument>
    							<argument>set</argument>
    							<argument>sass_binary_site</argument>
    							<argument>https://repo.huaweicloud.com/node-sass</argument>
    						</arguments>
    						<!-- <workingDirectory>${basedir}</workingDirectory>-->
    					</configuration>
    				</execution>
    
    				<execution>
    					<id>exec-npm-install</id>
    					<phase>prepare-package</phase>
    					<goals>	
    						<goal>exec</goal>
    					</goals>
    					<configuration>
    						<executable>npm</executable>
    						<arguments>
    							<argument>install</argument>
    						</arguments>
    						<workingDirectory>${basedir}</workingDirectory>
    					</configuration>
    				</execution>
    
    				<execution>
    					<id>exec-npm-run-build</id>
    					<phase>prepare-package</phase>
    					<goals>
    						<goal>exec</goal>
    					</goals>
    					<configuration>
    						<executable>npm</executable>
    						<arguments>
    							<argument>run</argument>
    							<argument>build</argument>
    						</arguments>
    						<workingDirectory>${basedir}</workingDirectory>
    					</configuration>
    				</execution>
    			</executions>
    		</plugin>
    	</plugins>
    </build>

  2. 新建Maven构建任务。
  3. 在新建的Maven构建任务里增加以下npm的安装及环境配置命令:

    # 创建文件目录

    mkdir ./node

    # 使用curl命令下载Node.js软件包

    curl -kv https://mirrors.xxxcloud.com/nodejs/v10.15.3/node-v10.15.3-linux-x64.tar.gz  -o ./node/node-v10.15.3-linux-x64.tar.gz

    # 使用tar命令解压

    1
    tar -zxvf ./node/node-v10.15.3-linux-x64.tar.gz -C ./node
    

    # 配置环境变量

    export NODEJS_HOME="${WORKSPACE}/node/node-v10.15.3-linux-x64"
    export PATH="${NODEJS_HOME}/bin:${PATH}"

    Node.js包(如本例中的node-v10.15.3)的下载地址请访问华为开源镜像站查找并复制链接地址。

  4. 保存后,执行构建验证。
分享:

Maven构建 所有常见问题

more