garbagetown

個人の日記です

maven で s2jdbc-gen (2)

年明けに一所懸命撒いた種がひとつも芽吹かないどころか滑り込んだ先がまさかの中断という憂き目に遭い休日に雪山に行くには大変結構な状況なのですが平日が死ぬほどヒマなので継続調査してみました。

Perhaps JAVA_HOME does not point to the JDK

s2jdbc-gen で gen-names や gen-service タスクを実行する際、当然、生成元となるエンティティを gen-entity タスクで生成してコンパイルしておかなければならないのですが、antrun プラグインで javac タスクを叩くと tools.jar が見つからないとイチャモンを付けられます。

[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] An Ant BuildException has occured: The following error occurred while executing this line:
C:\path\to\s2jdbc-gen-build.xml:23: Unable to find a javac compiler;
com.sun.tools.javac.Main is not on the classpath.
Perhaps JAVA_HOME does not point to the JDK.
It is currently set to "C:\path\to\jdk1.6.0_18\jre"

こちら によると maven-antrun-plugin は JAVA_HOME に JDK のサブディレクトリにある JRE を設定するらしく、相対パスを使って別途指定しやがれとのこと。

The maven-antrun-plugin runs ant with JAVA_HOME set to the jre subdirectory of the JDK, even if the JAVA_HOME for the overall run is a JDK.

http://docs.codehaus.org/display/MAVENUSER/Running+ant+tasks+that+use+the+JDK

pom.xml

勝手なことしてんじゃねーよという気もしますが、まあ仕方ないので pom.xml はこんな風にしてみました。

(snip)
            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <dependencies>
                    <dependency>
                        <groupId>org.apache.ant</groupId>
                        <artifactId>ant</artifactId>
                        <version>1.7.0</version>
                    </dependency>
                    <dependency>
                        <groupId>com.sun</groupId>
                        <artifactId>tools</artifactId>
                        <version>1.6.0</version>
                        <scope>system</scope>
                        <systemPath>${java.home}/../lib/tools.jar</systemPath>
                    </dependency>
                </dependencies>
                <configuration>
                    <tasks>
                        <ant antfile="${basedir}/s2jdbc-gen-build.xml" inheritRefs="true">
                            <target name="${ant.target}" />
                        </ant>
                    </tasks>
                </configuration>
                <goals>
                    <goal>run</goal>
                </goals>
            </plugin>
        </plugins>
    </build>
(snip)

s2jdbc-gen.xml

s2jdbc-gen.xml はデフォルト値や idGeneration 属性を活用しつつ javac タスクを追加してこんな感じ。

<?xml version="1.0"?>
<project name="s2jdbc-gen">
    <target name="init">
        <property name="version" value="latest" />
        <property name="idGeneration" value="identity" />
        <property name="classpathdir" value="target/classes" />
        <property name="rootpackagename" value="org.garbagetown.test" />
        <property name="javafiledestdir" value="src/main/java" />
        <property name="testjavafiledestdir" value="src/test/java" />
        <property name="javafileencoding" value="UTF-8" />
        <taskdef resource="s2jdbc-gen-task.properties" classpathref="maven.runtime.classpath" />
    </target>
    <target name="gen-entity" depends="init">
        <gen-entity
            idGeneration="${idGeneration}"
            rootpackagename="${rootpackagename}"
            overwrite="true"
            classpathref="maven.runtime.classpath" />
        <javac
            destdir="${classpathdir}"
            source="1.6"
            encoding="${javafileencoding}"
            classpathref="maven.runtime.classpath">
            <src path="${javafiledestdir}" />
            <src path="${testjavafiledestdir}" />
            <include name="**/*.java" />
            <exclude name="**/*Test.java"/>
        </javac>
    </target>
    <target name="gen-names" depends="gen-entity">
        <gen-names
            classPathDir="${classpathdir}"
            rootpackagename="${rootpackagename}"
            overwrite="true"
            classpathref="maven.runtime.classpath" />
    </target>
    <target name="gen-service" depends="gen-names">
        <gen-service
            classPathDir="${classpathdir}"
            rootpackagename="${rootpackagename}"
            classpathref="maven.runtime.classpath" />
    </target>
</project>

これで mvn antrun:run -Dant.target=gen-service を実行すればスキーマ情報から entity, names, service を自動生成できます。