javaubuntu

Javaの開発環境としてのUbuntu

ひさしぶりにJavaのプログラムを見ることになりました。
ソースがあるだけで詳しい説明もないので簡単に試す環境が必要です。
こんなときには、Ubuntuの環境が便利です。

環境の準備

JDKとmaven2をaptでインストールします。
JDKは、OpenJDKとSunのJDKが選択できます。
OpenJDKの場合

$ sudo apt-get install openjdk-6-jdk maven2

SunJDKの場合

$ sudo apt-get install sun-java6-jdk maven2

mavenプロジェクトのひな形を作る

ターゲット名は、絶対genappの方が良いと思うのですが…
architype:generateで対話形式でひな形を作れるみたいです。
簡単なサンプルであれば、15のmaven-archetype-quickstartというのを選択するのが良さそうです。

$ mvn archetype:generate
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'archetype'.
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Default Project
[INFO]    task-segment: [archetype:generate] (aggregator-style)
[INFO] ------------------------------------------------------------------------
[INFO] Preparing archetype:generate
[INFO] No goals needed for project - skipping
[INFO] Setting property: classpath.resource.loader.class => 'org.codehaus.plexus.velocity.ContextClassLoaderResourceLoader'.
[INFO] Setting property: velocimacro.messages.on => 'false'.
[INFO] Setting property: resource.loader => 'classpath'.
[INFO] Setting property: resource.manager.logwhenfound => 'false'.
[INFO] [archetype:generate {execution: default-cli}]
[INFO] Generating project in Interactive mode
[INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0)
Choose 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 (
Cocoon Maven Plugins Site - List
) 34: internal -> cocoon-22-archetype-block (
Cocoon Maven Plugins Site - List
) 35: internal -> cocoon-22-archetype-webapp (
Cocoon Maven Plugins Site - List
) 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: : 15 Define value for groupId: : local Define value for artifactId: : sample Define value for version: 1.0-SNAPSHOT: : Define value for package: local: : Confirm properties configuration: groupId: local artifactId: sample version: 1.0-SNAPSHOT package: local Y: : [INFO] ---------------------------------------------------------------------------- [INFO] Using following parameters for creating OldArchetype: maven-archetype-quickstart:RELEASE [INFO] ---------------------------------------------------------------------------- [INFO] Parameter: groupId, Value: local [INFO] Parameter: packageName, Value: local [INFO] Parameter: package, Value: local [INFO] Parameter: artifactId, Value: sample [INFO] Parameter: version, Value: 1.0-SNAPSHOT [INFO] ********************* End of debug info from resources from generated POM *********************** [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESSFUL [INFO] ------------------------------------------------------------------------ [INFO] Total time: 16 seconds [INFO] Finished at: Sat Jan 16 14:22:30 JST 2010 [INFO] Final Memory: 12M/30M [INFO] ------------------------------------------------------------------------ $

動作テスト

テンプレートには、Hello, World相当のApp.javaとAppTest.javaが含まれています。
testターゲットを実行するとコンパイルとテストを実行できます。

$ mvn test
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building sample
[INFO]    task-segment: [test]
[INFO] ------------------------------------------------------------------------
[INFO] [resources:resources {execution: default-resources}]
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/work/java/sample/src/main/resources
[INFO] [compiler:compile {execution: default-compile}]
[INFO] Nothing to compile - all classes are up to date
[INFO] [resources:testResources {execution: default-testResources}]
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/work/java/sample/src/test/resources
[INFO] [compiler:testCompile {execution: default-testCompile}]
[INFO] Nothing to compile - all classes are up to date
[INFO] [surefire:test {execution: default-test}]
[INFO] Surefire report directory: /home/work/java/sample/target/surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running local.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.094 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5 seconds
[INFO] Finished at: Sat Jan 16 14:38:52 JST 2010
[INFO] Final Memory: 11M/26M
[INFO] ------------------------------------------------------------------------
$

ソースコードの追加とコンパイル

./src/main/javaに既存のソースコードをコピーしてコンパイルします。
追加が必要なライブラリは、pom.xmlにdependencyを追加していきます。
試しにcommons-dbcpのサンプルをダウンロードしてコンパイルするとcommons-dbcpがないのでコンパイルエラーになります。

 wget -O src/main/java/BasicDataSourceExample.java "http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/doc/BasicDataSourceExample.java?revision=897457&view=co"
--2010-01-16 14:50:37--  http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/doc/BasicDataSourceExample.java?revision=897457&view=co
svn.apache.org をDNSに問いあわせています... 140.211.11.4
svn.apache.org|140.211.11.4|:80 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています... 200 OK
長さ: 特定できません [text/plain]
`src/main/java/BasicDataSourceExample.java' に保存中
[  <=>                                                                                                                                     ] 4,731       14.9K/s   時間 0.3s
2010-01-16 14:50:39 (14.9 KB/s) - `src/main/java/BasicDataSourceExample.java' へ保存終了 [4731]
$ mvn compile
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building sample
[INFO]    task-segment: [compile]
[INFO] ------------------------------------------------------------------------
[INFO] [resources:resources {execution: default-resources}]
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/work/java/sample/src/main/resources
[INFO] [compiler:compile {execution: default-compile}]
[INFO] Compiling 2 source files to /home/work/java/sample/target/classes
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Compilation failure
/home/work/java/sample/src/main/java/BasicDataSourceExample.java:[30,30] パッケージ org.apache.commons.dbcp は存在しません。
/home/work/java/sample/src/main/java/BasicDataSourceExample.java:[114,8] シンボルを見つけられません。
シンボル: クラス BasicDataSource
場所    : BasicDataSourceExample の クラス
/home/work/java/sample/src/main/java/BasicDataSourceExample.java:[114,33] シンボルを見つけられません。
シンボル: クラス BasicDataSource
場所    : BasicDataSourceExample の クラス
/home/work/java/sample/src/main/java/BasicDataSourceExample.java:[123,8] シンボルを見つけられません。
シンボル: クラス BasicDataSource
場所    : BasicDataSourceExample の クラス
/home/work/java/sample/src/main/java/BasicDataSourceExample.java:[123,31] シンボルを見つけられません。
シンボル: クラス BasicDataSource
場所    : BasicDataSourceExample の クラス
/home/work/java/sample/src/main/java/BasicDataSourceExample.java:[129,8] シンボルを見つけられません。
シンボル: クラス BasicDataSource
場所    : BasicDataSourceExample の クラス
/home/work/java/sample/src/main/java/BasicDataSourceExample.java:[129,31] シンボルを見つけられません。
シンボル: クラス BasicDataSource
場所    : BasicDataSourceExample の クラス
[INFO] ------------------------------------------------------------------------
[INFO] For more information, run Maven with the -e switch
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4 seconds
[INFO] Finished at: Sat Jan 16 15:08:16 JST 2010
[INFO] Final Memory: 13M/31M
[INFO] ------------------------------------------------------------------------
$

ライブラリの追加とコンパイル

pom.xmlに依存関係のあるライブラリを追加してコンパイルするとコンパイルエラーは、なくなります。

$ bzr diff
=== modified file 'pom.xml'
--- pom.xml	2010-01-16 05:45:43 +0000
+++ pom.xml	2010-01-16 06:10:48 +0000
@@ -9,6 +9,12 @@
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
+      <groupId>commons-dbcp</groupId>
+      <artifactId>commons-dbcp</artifactId>
+      <version>1.2.2</version>
+      <scope>compile</scope>
+    </dependency>
+    <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
$ mvn compile
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building sample
[INFO]    task-segment: [compile]
[INFO] ------------------------------------------------------------------------
[INFO] [resources:resources {execution: default-resources}]
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/work/java/sample/src/main/resources
[INFO] [compiler:compile {execution: default-compile}]
[INFO] Compiling 2 source files to /home/work/java/sample/target/classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4 seconds
[INFO] Finished at: Sat Jan 16 15:10:58 JST 2010
[INFO] Final Memory: 13M/31M
[INFO] ------------------------------------------------------------------------
$

下記の表記でcommons-dbcpの1.2系の最新バージョンに追従することができます。
保守には、もってこいです。

$ bzr diff
=== modified file 'pom.xml'
--- pom.xml	2010-01-16 06:15:14 +0000
+++ pom.xml	2010-01-16 06:15:25 +0000
@@ -11,7 +11,7 @@
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
-      <version>1.2.2</version>
+      <version>[1.2,)</version>
<scope>compile</scope>
</dependency>
<dependency>
$

maven2のプロジェクトの設定を調べる方法

このターゲットで全てのmaven2プロジェクトの設定を一覧できます。
ビルドがうまく行かないときには、これを使って確認します。

$ mvn help:effective-pom

ひさしぶりに使ってみましたが、かなり便利です。
GUIツールも良いですが、導入とツールを覚えるのにかかります。

タイトルとURLをコピーしました