Hibernateのsqlログからテーブル名だけ持ってくるperlスクリプトを作るからHibernateのmavenプロジェクトを作るんだー
今やってる仕事は、Hibernate2.1なんでアノテーションベースではなく、マッピングファイルが使われているという環境。そして、そのマッピングファイルをいじってスキーマ修飾のSQLを発行できるようにしたいわけだが、使ってないテーブルに対するマッピングファイルが無駄に大量に存在していたり、使ってるマッピングファイルから使ってないマッピングファイルにmany-to-one関連が張られていて、使ってないから修正の必要ないと思っていると勝手に外部結合してて落ちたりと、メンテナンスの重要性を噛み締める今日この頃なんです。
そんで取りあえずいったいどのテーブルが使われてるのかというのはsqlのログを見てやるのですが、Eclipseのコンソールには、Hibernateのログだけが出るわけではなく、またHibernateのログは長ったらしく、おなじSQLも1つの画面遷移で大量に発行されたりするので、テーブル名だけ抽出してやるプログラムを作る。これからも必要になるだろうし。もっと早く作るべきだったんだよ。何で作るかというとPerlで。正規表現とテキスト処理が便利だし。勉強だよね。普遍だよね。ま、大体のコードはできました。次はJavaでEclipseプラグインで作る。
だが、その前にウチのMac環境には、Hibernateのプロジェクトなんて1つもないので作るんですが、せっかくなんで勉強復習がてらmavenでプロジェクトを作ってみようと思う。maven-archetype-pluginで、generateゴールを実行する。createゴールを指定する場合は対話的なものにならないので、べた書きでパラメータを指定してあげる必要がある。
mavenでwebアプリプロジェクトを作る
mavenはWindowsXPには始めから入っている訳など無いので自分でインストールしてパスを通してよね。だるい。MaxOS10.6ではクリーンインストールしただけで、ターミナルで、「mvn -v」してわかる通り、すでにパスも通ってて、バージョン2.2.1が使用されているってわかると思うよ。ここにも書いたよ。
それでは始めましょう。
mvn archetype:generate
そうするとリポジトリにあるarchetypeのリストが出てくるので、選ぶ。
1: internal -> appfuse-basic-jsf (AppFuse archetype for creating a web application with Hibernate, Spring and JSF) 2: internal -> appfuse-basic-spring (AppFuse archetype for creating a web application with Hibernate, Spring and Spring MVC) 3: internal -> appfuse-basic-struts (AppFuse archetype for creating a web application with Hibernate, Spring and Struts 2) 4: internal -> appfuse-basic-tapestry (AppFuse archetype for creating a web application with Hibernate, Spring and Tapestry 4) 5: internal -> appfuse-core (AppFuse archetype for creating a jar application with Hibernate and Spring and XFire) 6: internal -> appfuse-modular-jsf (AppFuse archetype for creating a modular application with Hibernate, Spring and JSF) 7: internal -> appfuse-modular-spring (AppFuse archetype for creating a modular application with Hibernate, Spring and Spring MVC) 8: internal -> appfuse-modular-struts (AppFuse archetype for creating a modular application with Hibernate, Spring and Struts 2) 9: internal -> appfuse-modular-tapestry (AppFuse archetype for creating a modular application with Hibernate, Spring and Tapestry 4) 10: internal -> maven-archetype-j2ee-simple (A simple J2EE Java application) 11: internal -> maven-archetype-marmalade-mojo (A Maven plugin development project using marmalade) 12: internal -> maven-archetype-mojo (A Maven Java plugin development project) 13: internal -> maven-archetype-portlet (A simple portlet application) 14: internal -> maven-archetype-profiles () 15: internal -> maven-archetype-quickstart () 16: internal -> maven-archetype-site-simple (A simple site generation project) 17: internal -> maven-archetype-site (A more complex site project) 18: internal -> maven-archetype-webapp (A simple Java web application) 19: internal -> jini-service-archetype (Archetype for Jini service project creation) 20: internal -> softeu-archetype-seam (JSF+Facelets+Seam Archetype) 21: internal -> softeu-archetype-seam-simple (JSF+Facelets+Seam (no persistence) Archetype) 22: internal -> softeu-archetype-jsf (JSF+Facelets Archetype) 23: internal -> jpa-maven-archetype (JPA application) 24: internal -> spring-osgi-bundle-archetype (Spring-OSGi archetype) 25: internal -> confluence-plugin-archetype (Atlassian Confluence plugin archetype) 26: internal -> jira-plugin-archetype (Atlassian JIRA plugin archetype) 27: internal -> maven-archetype-har (Hibernate Archive) 28: internal -> maven-archetype-sar (JBoss Service Archive) 29: internal -> wicket-archetype-quickstart (A simple Apache Wicket project) 30: internal -> scala-archetype-simple (A simple scala project) 31: internal -> lift-archetype-blank (A blank/empty liftweb project) 32: internal -> lift-archetype-basic (The basic (liftweb) project) 33: internal -> cocoon-22-archetype-block-plain ([http://cocoon.apache.org/2.2/maven-plugins/]) 34: internal -> cocoon-22-archetype-block ([http://cocoon.apache.org/2.2/maven-plugins/]) 35: internal -> cocoon-22-archetype-webapp ([http://cocoon.apache.org/2.2/maven-plugins/]) 36: internal -> myfaces-archetype-helloworld (A simple archetype using MyFaces) 37: internal -> myfaces-archetype-helloworld-facelets (A simple archetype using MyFaces and facelets) 38: internal -> myfaces-archetype-trinidad (A simple archetype using Myfaces and Trinidad) 39: internal -> myfaces-archetype-jsfcomponents (A simple archetype for create custom JSF components using MyFaces) 40: internal -> gmaven-archetype-basic (Groovy basic archetype) 41: internal -> gmaven-archetype-mojo (Groovy mojo archetype) Choose a number: (1/2/3/4/5/6/7/8/9/10/11/12/13/14/15/16/17/18/19/20/21/22/23/24/25/26/27/28/29/30/31/32/33/34/35/36/37/38/39/40/41) 15: :
上のほうにstruts2とHibernateとかspringとHibernateとかあるみたいだけれど、springとかStruts2とか今回邪魔なので、取りあえず18番のmaven-archetype-webapp (A simple Java web application)を使おうと思う。
Choose a number: (1/2/3/4/5/6/7/8/9/10/11/12/13/14/15/16/17/18/19/20/21/22/23/24/25/26/27/28/29/30/31/32/33/34/35/36/37/38/39/40/41) 15: : 18
これでReturnポチ!
Define value for groupId: :
これは、自分のアーティファクト(成果物)のgroupId。何でもいいけどこんな感じで、
Define value for groupId: : sample.kijitoraneko.hibernate
これでReturnポチ!
つぎにartifactId
Define value for artifactId: : hibernateSample
これでReturnポチ!
つぎにversion
Define value for version: 1.0-SNAPSHOT: : 0.0.1
これでReturnポチ!
つぎにpackageは何も指定せずに
Define value for package: sample.kijitoraneko.hibernate: :
これでReturnポチ!
Confirm properties configuration: groupId: sample.kijitoraneko.hibernate artifactId: hibernateSample version: 0.0.1 package: sample.kijitoraneko.hibernate Y: : y
Confirmしたら、yと入力しReturnポチ!
[INFO] ---------------------------------------------------------------------------- [INFO] Using following parameters for creating OldArchetype: maven-archetype-webapp:RELEASE [INFO] ---------------------------------------------------------------------------- [INFO] Parameter: groupId, Value: sample.kijitoraneko.hibernate [INFO] Parameter: packageName, Value: sample.kijitoraneko.hibernate [INFO] Parameter: package, Value: sample.kijitoraneko.hibernate [INFO] Parameter: artifactId, Value: hibernateSample [INFO] Parameter: basedir, Value: /Users/sabatoraneko [INFO] Parameter: version, Value: 0.0.1 [INFO] ********************* End of debug info from resources from generated POM *********************** [INFO] OldArchetype created in dir: /Users/sabatoraneko/hibernateSample [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESSFUL [INFO] ------------------------------------------------------------------------ [INFO] Total time: 46 minutes 33 seconds [INFO] Finished at: Fri Nov 05 23:47:42 JST 2010 [INFO] Final Memory: 13M/81M [INFO] ------------------------------------------------------------------------
ハイ成功。はいできたー。
Hibernateライブラリを使えるように依存関係を設定する
そうしましたらこのプロジェクトのpom.xml開いてみましょうか。こんな感じになってるので、
ここからHibernateの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>org.hibernate</groupId> <artifactId>hibernate</artifactId> <packaging>jar</packaging> <version>3.2.7.ga</version> <name>Hibernate</name> <description>Relational Persistence for Java</description> <url>http://www.hibernate.org</url> 〜略〜 </project>
このgroupIdとかを自分のpom.xmlの
mvn compile
いろいろログ出るよ。
[INFO] Scanning for projects... [INFO] ------------------------------------------------------------------------ [INFO] Building hibernateSample Maven Webapp [INFO] task-segment: [compile] [INFO] ------------------------------------------------------------------------ [INFO] [resources:resources {execution: default-resources}] [WARNING] Using platform encoding (SJIS actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 0 resource Downloading: http://repo1.maven.org/maven2/org/hibernate/hibernate/3.2.7.ga/hibernate-3.2.7.ga.pom Downloading: http://repo1.maven.org/maven2/net/sf/ehcache/ehcache/1.2.3/ehcache-1.2.3.pom Downloading: http://repo1.maven.org/maven2/asm/asm-attrs/1.5.3/asm-attrs-1.5.3.pom Downloading: http://repo1.maven.org/maven2/cglib/cglib/2.1_3/cglib-2.1_3.pom Downloading: http://repo1.maven.org/maven2/org/hibernate/hibernate/3.2.7.ga/hibernate-3.2.7.ga.jar Downloading: http://repo1.maven.org/maven2/net/sf/ehcache/ehcache/1.2.3/ehcache-1.2.3.jar Downloading: http://repo1.maven.org/maven2/cglib/cglib/2.1_3/cglib-2.1_3.jar Downloading: http://repo1.maven.org/maven2/javax/transaction/jta/1.0.1B/jta-1.0.1B.jar Downloading: http://repo1.maven.org/maven2/asm/asm-attrs/1.5.3/asm-attrs-1.5.3.jar [INFO] Unable to find resource 'javax.transaction:jta:jar:1.0.1B' in repository central (http://repo1.maven.org/maven2) [INFO] ------------------------------------------------------------------------ [ERROR] BUILD ERROR [INFO] ------------------------------------------------------------------------ [INFO] Failed to resolve artifact. Missing: ---------- 1) javax.transaction:jta:jar:1.0.1B Try downloading the file manually from: http://java.sun.com/products/jta Then, install it using the command: mvn install:install-file -DgroupId=javax.transaction -DartifactId=jta -Dversion=1.0.1B -Dpackaging=jar -Dfile=/path/to/file Alternatively, if you host your own repository you can deploy the file there: mvn deploy:deploy-file -DgroupId=javax.transaction -DartifactId=jta -Dversion=1.0.1B -Dpackaging=jar -Dfile=/path/to/file -Durl=[url] -DrepositoryId=[id] Path to dependency: 1) sample.kijitoraneko.hibernate:hibernateSample:war:0.0.1 2) org.hibernate:hibernate:jar:3.2.7.ga 3) javax.transaction:jta:jar:1.0.1B ---------- 1 required artifact is missing. for artifact: sample.kijitoraneko.hibernate:hibernateSample:war:0.0.1 from the specified remote repositories: central (http://repo1.maven.org/maven2) [INFO] ------------------------------------------------------------------------ [INFO] For more information, run Maven with the -e switch [INFO] ------------------------------------------------------------------------ [INFO] Total time: 16 seconds [INFO] Finished at: Sat Nov 06 00:13:28 JST 2010 [INFO] Final Memory: 10M/81M [INFO] ------------------------------------------------------------------------
ERRORって残念な言葉だなー。
javax.transaction:jta:jar:1.0.1BがMissingとな。これはmavenのセントラルリポジトリには無いので、そういうやつは自分でjarをダウンロードしてローカルや社内リポジトリにインストールしておいてあげれば、そこから依存性を解決してくれるのでそのようにします。
jtaのjarをローカルリポジトリにインストールする
こちらにjtaのjarございます。これを落としてきて、今回は自分のローカルリポジトリにインストールします。
jarを作成します。~/Downloadsにダウンロードしまして、zipを解凍しますとjavaxディレクトリができますので、~/Downloadsに移動いたしまして、以下のjarコマンドを実行します。
jar -cvf jta-1.0.1b.jar javax
jta-1.0.1b.jarファイルができすので、そのままの場所で、
mvn install:install-file -DgroupId=javax.transaction -DartifactId=jta -Dversion=1.0.1B -Dpackaging=jar -Dfile=jta-1.0.1b.jar
Return ポチッ!
ログがいろいろ出るよ!
[INFO] Scanning for projects... [INFO] Searching repository for plugin with prefix: 'install'. [INFO] ------------------------------------------------------------------------ [INFO] Building Maven Default Project [INFO] task-segment: [install:install-file] (aggregator-style) [INFO] ------------------------------------------------------------------------ [INFO] [install:install-file {execution: default-cli}] [INFO] Installing /Users/teru/Downloads/jta-1.0.1b.jar to /Users/teru/.m2/repository/javax/transaction/jta/1.0.1B/jta-1.0.1B.jar [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESSFUL [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1 second [INFO] Finished at: Sat Nov 20 22:33:06 JST 2010 [INFO] Final Memory: 5M/81M [INFO] ------------------------------------------------------------------------
成功したっぽいので、見てみましょう。
terubookAir:1.0.1B teru$ pwd /Users/teru/.m2/repository/javax/transaction/jta/1.0.1B terubookAir:1.0.1B teru$ ls -la total 40 drwxr-xr-x 5 teru staff 170 11 20 22:33 . drwxr-xr-x 4 teru staff 136 11 20 22:33 .. -rw-r--r-- 1 teru staff 8716 11 20 22:33 jta-1.0.1B.jar -rw-r--r-- 1 teru staff 515 11 16 21:14 jta-1.0.1B.pom -rw-r--r-- 1 teru staff 144 11 16 21:14 jta-1.0.1B.pom.sha1 terubookAir:1.0.1B teru$
できてるね。
再びmavenコンパイル
それでは改めてもう一度。
mvn compile
いろいろログが出るよ!
[INFO] Scanning for projects... [INFO] ------------------------------------------------------------------------ [INFO] Building sudawebchan Maven Webapp [INFO] task-segment: [compile] [INFO] ------------------------------------------------------------------------ [INFO] [resources:resources {execution: default-resources}] [WARNING] Using platform encoding (SJIS actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 0 resource Downloading: http://repo1.maven.org/maven2/org/apache/maven/maven-plugin-api/2.0/maven-plugin-api-2.0.pom Downloading: http://repo1.maven.org/maven2/org/codehaus/plexus/plexus-compiler-api/1.5.3/plexus-compiler-api-1.5.3.pom Downloading: http://repo1.maven.org/maven2/org/codehaus/plexus/plexus-compiler/1.5.3/plexus-compiler-1.5.3.pom Downloading: http://repo1.maven.org/maven2/org/codehaus/plexus/plexus-compiler-manager/1.5.3/plexus-compiler-manager-1.5.3.pom Downloading: http://repo1.maven.org/maven2/org/codehaus/plexus/plexus-compiler-javac/1.5.3/plexus-compiler-javac-1.5.3.pom Downloading: http://repo1.maven.org/maven2/org/codehaus/plexus/plexus-compilers/1.5.3/plexus-compilers-1.5.3.pom Downloading: http://repo1.maven.org/maven2/org/codehaus/plexus/plexus-utils/1.0.5/plexus-utils-1.0.5.pom Downloading: http://repo1.maven.org/maven2/org/codehaus/plexus/plexus-utils/1.0.4/plexus-utils-1.0.4.jar Downloading: http://repo1.maven.org/maven2/org/codehaus/plexus/plexus-compiler-api/1.5.3/plexus-compiler-api-1.5.3.jar Downloading: http://repo1.maven.org/maven2/org/codehaus/plexus/plexus-compiler-manager/1.5.3/plexus-compiler-manager-1.5.3.jar Downloading: http://repo1.maven.org/maven2/org/codehaus/plexus/plexus-compiler-javac/1.5.3/plexus-compiler-javac-1.5.3.jar [INFO] [compiler:compile {execution: default-compile}] [INFO] No sources to compile [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESSFUL [INFO] ------------------------------------------------------------------------ [INFO] Total time: 12 seconds [INFO] Finished at: Sat Nov 20 22:43:41 JST 2010 [INFO] Final Memory: 10M/81M [INFO] ------------------------------------------------------------------------
今度は大丈夫。
そういたしましたら
mvn eclipse:eclipse
して、OK。
これで、eclipseにimportすれば、とりあえずhibernate3.2.7依存のプロジェクトが作成されています。
あとはJBossかなんかのhibernateプラグインとか使ってマッピングファイルとか作ってみればいいのじゃないの?