maven调用本地nodejs命令
在公司的一个项目中,前端使用的框架是vue.js,其中有需要使用npm run build进行前端打包。执行打包时,会默认将打包的前端静态资源文件(css/js/img等)输出到dist目录中。而spring boot只能访问src/main/resources/public下的静态资源文件,因此每次工程打包都得将dist目录下的资源文件手动拷贝到src/main/resources/public目录下,然后再执行mvn clean package命令进行打包,这样影响了开发效率。
公司项目使用maven技术进行项目工程组织。
问题思考:
在执行mvn clean package命令时,利用maven插件执行npm run build命令,一次性完成整个过程。
解决方式:
1、利用maven插件:exec-maven-plugin
详细的POM配置信息如下:
<profiles> <!--考虑到window 和linux环境 npm命令格式的问题,使用maven的profile实现动态指定命令-->
<profile>
<id>window</id>
<properties>
<npm>npm.cmd</npm>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>linux</id>
<properties>
<npm>npm</npm>
</properties>
</profile>
</profiles>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<warName>ROOT</warName>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.5.1.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<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}/src/main/webapp</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}/src/main/webapp</workingDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
执行方式:
windows环境 : mvn clean package -P window
Linux环境 :mvn clean package -P linux