Top / Maven2 / プラグインを自作する

プラグイン用Eclipseプロジェクトを作る

mvn archetype:create -DgroupId=nu.mine.kino.plugins \
                     -DartifactId=maven-hoge-plugin \
                     -DarchetypeArtifactId=maven-archetype-mojo
cd maven-hoge-plugin/
mvn eclipse:eclilpse

pom.xmlは以下の物が生成されました。

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>nu.mine.kino.plugins</groupId>
  <artifactId>maven-hoge-plugin</artifactId>
  <packaging>maven-plugin</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>maven-hoge-plugin Maven Mojo</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>org.apache.maven</groupId>
      <artifactId>maven-plugin-api</artifactId>
      <version>2.0</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

プラグインのソースを書く。

package nu.mine.kino.plugins;

/**
 * Goal which touches a timestamp file.
 * 
 * @goal touch  <-Annotation
 * 
 * @phase process-sources
 */
public class MyMojo extends AbstractMojo {
  /**
   * @parameter expression="${project.build.directory}"
   * @required
   */
  private File outputDirectory;

  public void execute() throws MojoExecutionException {
    File f = outputDirectory;
    if (!f.exists()) {
      f.mkdirs();
    }
    File touch = new File(f, "touch.txt");
    FileWriter w = null;
    try {
      w = new FileWriter(touch);
      w.write("touch.txt");
    } catch (IOException e) {
      throw new MojoExecutionException("Error creating file " + touch, e);
    } finally {
      if (w != null) {
        try {
          w.close();
        } catch (IOException e) {
          // ignore
        }
      }
    }
  }
}

Mavenコマンドでローカルリポジトリにプラグインをインストールする

maven clean install

これで先のソースがローカルリポジトリにインストールされました。

Mavenプロジェクトからインストールしたプラグインを呼び出す。

さて、ローカルリポジトリにプラグインがインストールされたので、別のmavenプロジェクトからプラグインを呼び出してみようと思います。

pom.xmlには以下のように追加します。

 <build>
     ......
     <plugin>
       <groupId>nu.mine.kino.plugins</groupId>
       <artifactId>maven-hoge-plugin</artifactId>
      <version>1.0-SNAPSHOT</version>
     </plugin>
   </plugins>
 </build>

以下のコマンドで先のモジュールを呼び出します。

mvn nu.mine.kino.plugins:maven-hoge-plugin:1.0-SNAPSHOT:touch

引数の意味。

mvn [groupId]:[artifactId]:[version]:[ゴール名]

なんだろ、ローカルにインストールされているだけなので、グループIDとかを指定しなくてはいけないのかな。

アノテーション

アノテーションやpomから生成されるファイル

上のアノテーションの記述やpom.xmlの情報から、プラグインファイルの設定情報plugin.xmlが出力されます。これは実際はjar内の

maven-hoge-plugin-1.0-SNAPSHOT.jar#META-INF/maven/plugin.xml

に格納されています。内容は以下の通り。

<plugin>
  <description></description>
  <groupId>nu.mine.kino.plugins</groupId>
  <artifactId>maven-hoge-plugin</artifactId>
  <version>1.0-SNAPSHOT</version>
  <goalPrefix>hoge</goalPrefix>
  <isolatedRealm>false</isolatedRealm>
  <inheritedByDefault>true</inheritedByDefault>
  <mojos>
    <mojo>
      <goal>touch</goal>
      <description>Goal which touches a timestamp file.</description>
      <requiresDirectInvocation>false</requiresDirectInvocation>
      <requiresProject>true</requiresProject>
      <requiresReports>false</requiresReports>
      <aggregator>false</aggregator>
      <requiresOnline>false</requiresOnline>
      <inheritedByDefault>true</inheritedByDefault>
      <phase>process-sources</phase>
      <implementation>nu.mine.kino.plugins.MyMojo</implementation>
      <language>java</language>
      <instantiationStrategy>per-lookup</instantiationStrategy>
      <executionStrategy>once-per-session</executionStrategy>
      <parameters>
        <parameter>
          <name>outputDirectory</name>
          <type>java.io.File</type>
          <required>true</required>
          <editable>true</editable>
          <description>Location of the file.</description>
        </parameter>
      </parameters>
      <configuration>
        <outputDirectory implementation="java.io.File">${project.build.directory}</outputDirectory>
      </configuration>
    </mojo>
  </mojos>
  <dependencies/>
</plugin>

所感

、、、、、、これ、デバッグどうやるんだ??

組み込みプロパティの一覧

に一覧があります。感謝!

関連リンク


この記事は

選択肢 投票
おもしろかった 7  
そうでもない 1  
  • リポジトリ内の maven-metadata-local.xml に<prefix>hoge</prefix>って書いてあるな。。どこから取ってきたんだろう。。。。 -- きの? 2007-06-28 (木) 11:54:40
  • http://maven.apache.org/guides/plugin/guide-java-plugin-development.html に he "maven-$name-plugin" and "$name-maven-plugin" artifactId patterns are treated in a special way. て書いてあった。スペシャルに扱われて抜き出されるんだな。。 -- きの? 2007-06-28 (木) 11:56:39

Top / Maven2 / プラグインを自作する

現在のアクセス:11876


トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2012-10-04 (木) 20:55:00 (4219d)