將node命令集成到maven中--方法1--使用本地的node

環境:win10 64位 node-v6.2.0(內置npm3.8.9)

將node的一些命令集成到maven中,這樣打包時就能自動將前端的東西實時也打包了。

既然是使用本地機器上的node,那麼核心maven插件是exec-maven-plugin,詳細瞭解請看官網去學習。

爲了保證完整性,我貼出全部pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.fulong.pid</groupId>
  <artifactId>xxxx</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>xxxx Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>

    <maven.compliler.version>3.3</maven.compliler.version>
    <maven.war.version>2.6</maven.war.version>
    <maven.assembly.version>2.5.5</maven.assembly.version>
    <maven.resources.version>2.6</maven.resources.version>
    <maven.release.version>2.5.3</maven.release.version>

  </properties>

  <dependencies>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <!--考慮到window 和linux環境 npm命令格式的問題,使用maven的profile實現動態指定命令-->
  <profiles>
    <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>

  
  <build>
    <finalName>xxxx</finalName>
    <resources>
      <resource>
        <directory>src/main/java</directory>
        <!-- 是否替換資源中的屬性 -->
        <filtering>false</filtering>
      </resource>
      <resource>
        <directory>src/main/resources</directory>
        <includes>
          <include>config/*.xml</include>
          <include>excel/*</include>
          <include>*.xml</include>
          <include>config/*.properties</include>
        </includes>
        <filtering>false</filtering>
      </resource>
    </resources>


    <plugins>

      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>${maven.compliler.version}</version>
        <configuration>
          <source>${maven.compiler.source}</source>
          <target>${maven.compiler.target}</target>
        </configuration>
      </plugin>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>${maven.war.version}</version>
        <configuration>
          <!-- 不需要打進war包的文件 -->
          <warSourceExcludes>
            node_modules/**,node/**
          </warSourceExcludes>
        </configuration>
      </plugin>

      <!-- 如果 你本地安裝的node和npm是同一個版本,運行這個plugin
        exec-maven-plugin,這個插件能執行本地環境中存在的任何命令
      -->

      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.6.0</version>

        <executions>

          <!-- 清除緩存 -->
          <execution>
            <id>npm-cache-clean</id>
            <phase>clear environment</phase>
            <goals>
              <goal>exec</goal>
            </goals>
            <configuration>
              <executable>${npm}</executable>
              <arguments>cache clean</arguments>
              <workingDirectory>${basedir}/src/main/webapp</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}/src/main/webapp</workingDirectory>
            </configuration>
          </execution>

          <execution>
            <id>webpack-build</id>
            <phase>compile</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>
  </build>


 
</project>

當然,你本地的node,必須在環境變量裏配置好,這是必須的(驗證:能直接cmd運行node -v  和 npm -v)。

這個pom.xml一點不用改,以後能直接複用

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章