LarryDpk
发布于 2020-09-03 / 13547 阅读
0

用org.codehaus.mojo的插件exec-maven-plugin执行程序,实在是太方便了

1 前言

maven的强大之处在于有各种插件可用,而org.codehaus.mojoexec-maven-plugin就是其中一个常用的非常方便的插件。在项目中经常使用它,比如执行shell命令、构建docker镜像、用npm打包等。特别是结合phase使用,非常强大。

2 shell

shell的配置如下:

<plugin>
  <artifactId>exec-maven-plugin</artifactId>
  <groupId>org.codehaus.mojo</groupId>
  <executions>
    <execution><!-- Run our version calculation script -->
      <id>Version Calculation</id>
      <phase>generate-sources</phase>
      <goals>
        <goal>exec</goal>
      </goals>
      <configuration>
        <executable>${basedir}/scripts/calculate-version.sh</executable>
      </configuration>
    </execution>
  </executions>
</plugin>

3 Docker

Docker的使用如下:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>exec-maven-plugin</artifactId>
  <version>3.0.0</version>
  <executions>
    <execution>
      <phase>install</phase>
      <goals>
        <goal>exec</goal>
      </goals>
      <configuration>
        <executable>docker</executable>
        <arguments>
          <argument>build</argument>
          <argument>.</argument>
          <argument>-t</argument>
          <argument>pkslow/${project.artifactId}:${project.version}</argument>
          <argument>-f</argument>
          <argument>src/main/docker/Dockerfile</argument>
        </arguments>
      </configuration>
    </execution>
  </executions>
</plugin>

4 npm

npm如下:

<build>
  <plugins>
    <!-- Standard plugin to generate WAR -->
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-war-plugin</artifactId>
      <version>2.1.1</version>
      <configuration>
        <failOnMissingWebXml>false</failOnMissingWebXml>
        <webResources>
          <resource>
            <directory>${gulp.output.directory}</directory>
          </resource>
        </webResources>
      </configuration>
    </plugin>


    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>exec-maven-plugin</artifactId>
      <version>1.3.2</version>
      <executions>
        <!-- Required: The following will ensure `npm install` is called
          before anything else during the 'Default Lifecycle' -->
        <execution>
          <id>npm install (initialize)</id>
          <goals>
            <goal>exec</goal>
          </goals>
          <phase>initialize</phase>
          <configuration>
            <executable>npm</executable>
            <arguments>
              <argument>install</argument>
            </arguments>
          </configuration>
        </execution>
        <!-- Required: The following will ensure `npm install` is called
          before anything else during the 'Clean Lifecycle' -->
        <execution>
          <id>npm install (clean)</id>
          <goals>
            <goal>exec</goal>
          </goals>
          <phase>pre-clean</phase>
          <configuration>
            <executable>npm</executable>
            <arguments>
              <argument>install</argument>
            </arguments>
          </configuration>
        </execution>

        <!-- Optional: The following will output the npm configuration.
          I do this so my CI logs will show the npm information used
        for the build -->
        <execution>
          <id>npm config list (validate)</id>
          <goals>
            <goal>exec</goal>
          </goals>
          <phase>validate</phase>
          <configuration>
            <executable>npm</executable>
            <arguments>
              <argument>config</argument>
              <argument>list</argument>
            </arguments>
          </configuration>
        </execution>

        <!-- Required: This following calls `npm run build` where 'build' is
          the script name I used in my project, change this if yours is
        different -->
        <execution>
          <id>npm run build (compile)</id>
          <goals>
            <goal>exec</goal>
          </goals>
          <phase>compile</phase>
          <configuration>
            <executable>npm</executable>
            <arguments>
              <argument>run</argument>
              <argument>build</argument>
            </arguments>
          </configuration>
        </execution>

        <!-- Optional: The following runs the script that copies the
          appropriate files from the npm build directory into the location
        'maven-war-plugin' is expecting.  The copying could be done
        during the 'build' script, but I like to keep it separate.
        Idealy in the future, I won't need maven at which, I can just
        delete the 'prepare-for-maven-war' script. -->
        <execution>
          <id>npm run prepare-for-maven (prepare-package)</id>
          <goals>
            <goal>exec</goal>
          </goals>
          <phase>prepare-package</phase>
          <configuration>
            <executable>npm</executable>
            <arguments>
              <argument>run</argument>
              <argument>prepare-for-maven-war</argument>
            </arguments>
          </configuration>
        </execution>

        <!-- Optional: The following will publish to npm if you run
          `mvn deploy`. -->
        <execution>
          <id>npm run publish (deploy)</id>
          <goals>
            <goal>exec</goal>
          </goals>
          <phase>deploy</phase>
          <configuration>
            <executable>npm</executable>
            <arguments>
              <argument>publish</argument>
            </arguments>
          </configuration>
        </execution>

        <!-- Required: The following will run unit tests.  My test scripts
          in npm look for the property 'skipTests', so I map it to
        'maven.test.skip'
        Note: the douple '-' syntax used below only works with npm >= 2. -->
        <execution>
          <id>npm run test (test)</id>
          <goals>
            <goal>exec</goal>
          </goals>
          <phase>test</phase>
          <configuration>
            <executable>npm</executable>
            <arguments>
              <argument>run</argument>
              <argument>test</argument>
              <argument>--</argument>
              <argument>--skipTests=${maven.test.skip}</argument>
            </arguments>
          </configuration>
        </execution>

        <!-- Required: The following calls the npm script that cleans
          up the build. -->
        <execution>
          <id>npm run clean (clean)</id>
          <goals>
            <goal>exec</goal>
          </goals>
          <phase>clean</phase>
          <configuration>
            <executable>npm</executable>
            <arguments>
              <argument>run</argument>
              <argument>clean</argument>
            </arguments>
          </configuration>
        </execution>
      </executions>

      <configuration>
        <environmentVariables>
          <!-- The following parameters create an NPM sandbox for CI -->
          <NPM_CONFIG_PREFIX>${basedir}/npm</NPM_CONFIG_PREFIX>
          <NPM_CONFIG_CACHE>${NPM_CONFIG_PREFIX}/cache</NPM_CONFIG_CACHE>
          <NPM_CONFIG_TMP>${project.build.directory}/npmtmp</NPM_CONFIG_TMP>
        </environmentVariables>
      </configuration>
    </plugin>
  </plugins>
</build>

参考文档:

exec-maven-plugin/usage

npm