将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一点不用改,以后能直接复用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值