
# 使用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命令解压
   ```
   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}"
   ```
   ![](https://support.huaweicloud.com/codeci_faq/zh-cn_image_0000001910750438.png "点击放大")
   Node.js包（如本例中的node-v10.15.3）的下载地址请访问[华为开源镜像站](https://mirrors.huaweicloud.com/)查找并复制链接地址。
   
   
4. 保存后，执行构建验证。
 
