LoginSignup
32
31

More than 5 years have passed since last update.

mavenですべての設定ファイルを自動生成させる方法

Last updated at Posted at 2014-04-19

2014/5/25 全面的に書きなおしました

eclipseの設定はプロジェクトメンバー内で、ほとんど自動で揃えることができます。
プロジェクトの最初にwikiに設定方法を書いて、それぞれの開発者に同じ環境をwikiを見て構築してもらう必要はありません。(最初に一人が設定ファイルを作っておく必要はあります)
gradleでなくても、mavenでも簡単にできます。

できること

以下、それぞれの画面で設定することは、自動でできます。

  • checkstyleの設定ファイルのimportの自動化

以下で設定できるところ

eclipse-checkstyle.png

  • Java Code Styleの設定の自動化 

以下で設定できるところ

eclipse-codestyle.png

例えば
1.formatterの設定ファイルのimportの自動化
2.code templeteの設定ファイルのimportの自動化
3.import補完のfavorit設定などの自動化 
4.などなど

  • save actionの設定の自動化

以下で設定できるところ

eclipse-saveactions.png

  • エディタ一般の設定の自動化

以下で設定できるところ

eclipse-texteditor.png

  • xml関連(エディタ含む)の設定の自動化

以下で設定できるところ

eclipse-xml.png

  • web関連のエディタなどの設定(CSS,html,jsp)の自動化

以下で設定できるところ

eclpse-web.png

  • プロジェクト固有の設定の自動化

全般的に以下の場所にファイルを置けばプロジェクトで上書きできる
<プロジェクト>/.settings/

  • Run Configurations」や「External Tools Configurations」画面での設定の自動化

eclipse-run.png

  • それ以外の設定もほとんどのことはできます。

うまくできなかったもの

  • Java → Complier → Errors/Warningでの設定の自動化

eclipse-warning.png

今回の対象

今回は、checkstyleとformatterとExternal Tools Configurationsの設定を記述します。
ただし、他の設定も同じ仕組みできます。

方法

親プロジェクト

まずは、それぞれのプロジェクトで必要な共通の設定を親のpom.xmlに書きます。
これは特に変わったことをせずにmaven-eclipse-pluginを普通に使います。
一度実行すればいいので、自分の場合はプロファイルに書いています。
以下の部分は自分の設定に変えてください

  • check-config-name="sa-checkstyle"
  • <workspaceCodeStylesURL>
  • <workspaceActiveCodeStyleProfileName>
親pom.xml
    <profile>
      <id>conf</id>
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-eclipse-plugin</artifactId>
            <version>2.9</version>
            <configuration>
              <additionalBuildcommands>
                <buildcommand>com.atlassw.tools.eclipse.checkstyle.CheckstyleBuilder</buildcommand>
              </additionalBuildcommands>
              <additionalProjectnatures>
                <projectnature>com.atlassw.tools.eclipse.checkstyle.CheckstyleNature</projectnature>
              </additionalProjectnatures>
              <additionalConfig>
                <file>
                  <name>.checkstyle</name>
                  <content>
                  <![CDATA[<?xml version="1.0" encoding="UTF-8"?>
<fileset-config file-format-version="1.2.0" simple-config="true">
    <fileset name="all" enabled="true" check-config-name="sa-checkstyle" local="false">
        <file-match-pattern match-pattern="." include-pattern="true"/>
    </fileset>
    <filter name="NonSrcDirs" enabled="true"/>
</fileset-config>
]]>
                  </content>
                </file>
                <file>
                  <name>.settings/org.eclipse.core.resources.prefs</name>
                  <content>
                <![CDATA[eclipse.preferences.version=1${line.separator}encoding/<project>=${project.build.sourceEncoding}${line.separator}]]>
                  </content>
                </file>
              </additionalConfig>
              <workspace>${eclipse.workspace}</workspace>
              <workspaceCodeStylesURL>https://raw.githubusercontent.com/ko2ic/java-config/master/codeformatter-7.xml</workspaceCodeStylesURL>
              <workspaceActiveCodeStyleProfileName>Ko2ic Formatter</workspaceActiveCodeStyleProfileName>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>

設定ファイルを用意

例えば、<ルートプロジェクト>/config以下にファイルを置いときます。これはgitなどで管理しときます。

それぞれのファイルは自分の環境に合わせてください

checkstyle-config.xml
<?xml version="1.0" encoding="UTF-8"?>

<checkstyle-configurations file-format-version="5.0.0" default-check-configuration="Sun Checks">
  <check-configuration name="sa-checkstyle" location="https://raw.githubusercontent.com/ko2ic/java-config/master/sa-checkstyle-5.6-utf8.xml" type="remote" description="">
    <additional-data name="cache-file" value="false"/>
  </check-configuration>
</checkstyle-configurations>

org.eclipse.jdt.ui.prefs
・・・
formatter_profile=_Ko2ic Formatter

・・・

子プロジェクト

どのプロジェクトでも良いが、単純に一度だけ呼ばれるようにしたいので、一つの子プロジェクトのpom.xmlに記述します。
maven-antrun-pluginで上記のconfigディレクトリにある設定ファイルをworkspaceにcopyします。

このときに気をつけることは、maven-antrun-plugin${project.parent.basedir}の利用をサポートしていないっぽいので、わざわざgmaven-pluginmain.basedirで入れ替えています。

子pom.xml
    <profile>
      <id>conf</id>
      <build>
        <plugins>
          <plugin>
            <groupId>org.codehaus.gmaven</groupId>
            <artifactId>gmaven-plugin</artifactId>
            <version>1.5</version>
            <executions>
              <execution>
                <id>set-custom-property</id>
                <phase>validate</phase>
                <goals>
                  <goal>execute</goal>
                </goals>
                <configuration>
                  <source>
                    project.properties.setProperty('main.basedir',
                    project.parent.basedir.toString())
                  </source>
                </configuration>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.7</version>
            <dependencies>
              <dependency>
                <groupId>org.apache.ant</groupId>
                <artifactId>ant</artifactId>
                <version>1.9.3</version>
              </dependency>
            </dependencies>
            <executions>
              <execution>
                <phase>process-resources</phase>
                <configuration>
                  <target>
                    <copy file="${main.basedir}/config/checkstyle-config.xml" tofile="${eclipse.workspace}/.metadata/.plugins/net.sf.eclipsecs.core/checkstyle-config.xml" overwrite="true" />
                    <copy file="${main.basedir}/config/org.eclipse.jdt.ui.prefs" tofile="${eclipse.workspace}/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.jdt.ui.prefs"
                      overwrite="true" />
                </configuration>
                <goals>
                  <goal>run</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>

これ以外にも例えば、以下のような設定を書いておくと

子pom.xml
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-eclipse-plugin</artifactId>
        <configuration>
          <additionalConfig>
            <file>
              <name>.settings/.metadata/.plugins/org.eclipse.debug.core/.launches/open finder.launch</name>
              <content><![CDATA[<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<launchConfiguration type="org.eclipse.ui.externaltools.ProgramLaunchConfigurationType">
<stringAttribute key="org.eclipse.ui.externaltools.ATTR_LOCATION" value="/usr/bin/open"/>
<stringAttribute key="org.eclipse.ui.externaltools.ATTR_TOOL_ARGUMENTS" value="${container_loc}"/>
</launchConfiguration>              
]]>
              </content>
            </file>

以下のような画面になるような設定ファイルができます。
openFinder.png

ただし、子プロジェクトで、maven-eclipse-pluginの設定を書くと親pom.xmlの設定が上書きされてしまうので、親と同じ内容を小プロジェクトにも書く必要がでできます。

一度、実行すると以下のように使えます。
openFinder2.png

設定ファイルをmaven-antrun-plugin${eclipse.workspace}にコピーする必要がない場合もあります。
というのも、.settings/に置けば、eclipseが認識するからです。
以下が試してみた結果です。

.settings/open finder.launch ×
.settings/.plugins/org.eclipse.debug.core/.launches/open finder.launch ×
.settings/custom/.metadata/.plugins/org.eclipse.debug.core/.launches/open finder.launch ×
.settings/.metadata/.plugins/org.eclipse.debug.core/.launches/open finder.launch ○

プロジェクトごとの設定

そのプロジェクト固有の設定は、そのプロジェクトのpom.xmlに書く必要があります。
例えば、以下にファイルができるようにpom.xmlを記述すればよいです。
<プロジェクト>/.settings/org.eclipse.jdt.ui.prefs

これはpom.xmlのmaven-eclipse-pluginの<file>に記述しても良いし、ファイルを用意して、maven-antrun-pluginでもできるので、いままでのやり方でできると思います。

実行

いままでの設定は、一人だけが行っていけばよいことです。
このプロジェクトを使う開発者には以下のコマンドを叩いてもらうだけで設定されます。

$ mvn process-resources eclipse:eclipse -Pconf -Declipse.workspace=<Eclipseのworkspace>

m2eのmaven projectとしてimportする場合は、mvn eclipse:cleanをしてからimportをする必要があります。

まとめ

gradle使えば簡単にできそうだけど、現場によってはmavenしか使えないところもあると思います。
そういう場合でもこのように作成しておけば、それぞれの開発者の環境構築時間の削減と設定漏れを防ぐことができると思います。

サンプル

githubにサンプルを用意しました。
https://github.com/ko2ic/spike-maven-eclipse-config

32
31
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
32
31