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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM