运行 npm run build

运行 npm run build

<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.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