LoginSignup
9
8

More than 5 years have passed since last update.

exec-maven-plugin を使って Maven から npm を実行する

Last updated at Posted at 2015-06-28

概要

npm タスクを Maven と連携してみる。
具体例をひとつあげるとJSライブラリを取り込む npm タスクを用意して、mvn compile と同時に動くようにしてみる。

手順

exec-maven-plugin を使って、npm install を実行する。
Maven には多様なライフサイクルが定義されている1。例示した npm タスクは JS ライブラリを npm 経由で取ってくるタスクなので、compile 前のソースコード変換をする generate-sources ライフサイクルがこれに該当する(厳密には違う感じもするけど)。

以上を pom.xml に書くとこのようになる。

pom.xml
    <build>
        <plugins>
            ...
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>exec-npm-install</id>
                        <phase>generate-sources</phase>
                        <configuration>
                            <executable>npm</executable>
                            <arguments>
                                <argument>install</argument>
                            </arguments>
                        </configuration>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

これで mvn compile 実行時に npm install も動作するようになった。めでたしめでたし。

おまけ - Jenkins 連携

CI に Jenkins を導入している場合は Jenkins から npm を見えるように以下の作業が必要になる。

  1. NodeJS Plugin をインストールする
  2. 該当プロジェクトの設定ページの Build Environment セクションの "Provide Node & npm bin/ folder to PATH" にチェックを入れる

まとめ

mvnnpm を連携した。基本的に利用者は npm の存在をほとんど意識しないで済むのが利点といえる。

参照

9
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
8