# Mavenプロジェクトを作成する

この節ではMavenプラグインを実装するためのMavenプロジェクトを作成する方法を紹介します。

## pom.xmlを設定する

はじめに、`pom.xml`に以下を設定しましょう。

* `<packaging>` 要素を `<project>` 要素の直下に追加し、値を *maven-plugin* に設定
* *maven-plugin-api* と *maven-plugin-annotations* に *provided* スコープで依存
* `<artifactId>` 要素を *(任意の名前)-maven-plugin* に設定
  * 慣習でありで必須ではありませんが、このように命名することでMavenプラグイン実行時に `-maven-plugin` を省略できます。 たとえば *jp.skypencil* グループに属する *sample-maven-plugin* なら、`mvn jp.skypencil:sample` で実行できます。
  * 古い資料では *maven-(任意の名前)-plugin* を使うよう推奨していますが、現在は非推奨ですので使用しないでください。
* *maven-plugin-plugin* に `<execution><id>default-descriptor</id></execution><phase>process-classes</phase></execution>` を追記[^1](http://maven.apache.org/plugin-tools/maven-plugin-plugin/examples/using-annotations.html)
* *maven-plugin-plugin* に `<execution><id>generate-helpmojo</id><goals><goal>helpmojo</goal></goals></execution>` を追記[^2](https://github.com/KengoTODA/what-is-maven/blob/main/implement-plugin/%E3%83%98%E3%83%AB%E3%83%97%E8%A1%A8%E7%A4%BA%E7%94%A8Mojo%E3%82%92%E8%87%AA%E5%8B%95%E7%94%9F%E6%88%90%E3%81%99%E3%82%8B%E3%81%9F%E3%82%81%E3%81%AB%E5%BF%85%E8%A6%81%E3%81%A7%E3%81%99%E3%80%82/README.md)

`pom.xml`の概要は次のようになります。

```xml
<project
<artifactId>sample-maven-plugin</artifactId>
<packaging>maven-plugin</packaging>
  <dependencies
&lt;dependency&gt;
  &lt;groupId&gt;org.apache.maven&lt;/groupId&gt;
  &lt;artifactId&gt;maven-plugin-api&lt;/artifactId&gt;
  &lt;version&gt;{{book.version.maven}}&lt;/version&gt;&lt;!-- version of Maven --&gt;
  &lt;scope&gt;provided&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
  &lt;groupId&gt;org.apache.maven.plugin-tools&lt;/groupId&gt;
  &lt;artifactId&gt;maven-plugin-annotations&lt;/artifactId&gt;
  &lt;version&gt;3.4&lt;/version&gt;&lt;!-- version of Maven Plugin Tools --&gt;
  &lt;scope&gt;provided&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
  &lt;groupId&gt;junit&lt;/groupId&gt;
  &lt;artifactId&gt;junit&lt;/artifactId&gt;
  &lt;version&gt;{{book.version.junit}}&lt;/version&gt;
  &lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;

</dependencies>
  <build
&lt;plugins&gt;
  &lt;plugin&gt;
    &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
    &lt;artifactId&gt;maven-plugin-plugin&lt;/artifactId&gt;
    &lt;version&gt;3.4&lt;/version&gt;
    &lt;executions&gt;
      &lt;execution&gt;
        &lt;id&gt;default-descriptor&lt;/id&gt;
        &lt;phase&gt;process-classes&lt;/phase&gt;
      &lt;/execution&gt;
      &lt;execution&gt;
        &lt;id&gt;generate-helpmojo&lt;/id&gt;
        &lt;goals&gt;
          &lt;goal&gt;helpmojo&lt;/goal&gt;
        &lt;/goals&gt;
      &lt;/execution&gt;
    &lt;/executions&gt;
  &lt;/plugin&gt;
&lt;/plugins&gt;

</build>
```

なお、archetypeプラグインを利用するとpom.xmlを自動的に生成してくれます\[^3]ので、 スクラッチで実装する場合はぜひ利用してください。次のコマンドでMavenプロジェクトの作成を行えます。

```
mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-plugin -DarchetypeVersion=1.2
```

## Eclipseプロジェクトを作成する

Eclipseで開発する場合、次のコマンドでEclipseプロジェクトの作成してください。 作成後、メニューバーの「ファイル→インポート」から既存のEclipseプロジェクトとして取り込むことができます。

```
mvn eclipse:eclipse
```

あるいはEclipseの`m2e`プラグインを使用することで、MavenプロジェクトをEclipseプロジェクトとして 直接開くことも可能です。詳しくは`m2e`の公式サイトをご確認ください。

TODO 上記サイトのURLを調べる。

\[^3]: TODO 1.2は2015年2月時点での最新版だが、長く更新されていないので、新しいアーキタイプを作成すること。
