Maven2のtips仕事で使い始めたので、ちょこちょこまとめていきます。 ローカルリポジトリにインストールするMavenリポジトリに登録されていないjarファイルをローカルリポジトリにインストールする。
jar -cvf C:/jta-1.0.1b.jar javax
mvn install:install-file -DgroupId=javax.transaction -DartifactId=jta^ -Dversion=1.0.1B -Dpackaging=jar -Dfile=C:/jta-1.0.1b.jar MavenでWebプロジェクトを作成する。
eclipseでWTPをインストールしている前提です。(インストール手順はこちら) D:\eclipse32\workspace_birt>mvn archetype:create -DgroupId=^ <--実際は1行で入力 com.powerdee.sample -DartifactId=birt -DarchetypeArtifactId=maven-archetype-webapp D:\eclipse32\workspace_birt>cd birt eclipseのクラスパスを設定する。D:\eclipse32\workspace_birt\birt>mvn eclipse:eclipse eclipseを起動して、プロジェクトをインポートします。 eclipseにリポジトリの変数を追加する。Mavenのローカルリポジトリ(通常は、C:/Documents and Settings/ユーザ名/.m2/repository)
D:\eclipse32\workspace_birt\birt>mvn -Declipse.workspace=^ "D:/eclipse32/workspace_birt/birt" eclipse:add-maven-repo WTPプロジェクトの設定下記のコマンドでWTPのWebプロジェクトとして設定されます。 D:\eclipse32\workspace_birt\birt>mvn eclipse:eclipse -Dwtpversion=1.0 j2ee5用のビルド環境構築Maven2では、J2EE用のプロジェクトテンプレートとして maven-archetype-j2ee-simple というのがあるが2006年10月時点では、alpha版であるため正常に動作しない。 ここではJavaアプリケーション用の maven-archetype-quickstart 及び、Webアプリ用の maven-archetype-webapp を利用してビルド環境を構築してみる。 本サンプル環境のディレクトリ構成─j2ee5sample <--ビルド環境の親プロジェクト ├─j2ee5sample-ear <--EARプロジェクト ├─j2ee5sample-ejb <--EJBプロジェクト └─j2ee5sample-war <--WARプロジェクト Java EE 5 APIの登録GlassFishに付属のJ2EEライブラリを登録する。(GlassFishについては、こちらを参照) mvn install:install-file -DgroupId=javaee-spec -DartifactId=javaee-spec^ -Dfile=(GlassFishインストールディレクトリ)/lib/javaee.jar -Dpackaging=jar -Dversion=5.0 親プロジェクトの作成下記のコマンドを実行すると、j2ee5sampleというディレクトリとその下にpom.xmlが生成される。 mvn archetype:create -DgroupId=com.powerdee -DartifactId=j2ee5sample^ -DarchetypeArtifactId=maven-archetype-quickstart 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>com.powerdee</groupId>
<artifactId>j2ee5sample</artifactId>
<packaging>pom</packaging> <--jarをpomに変更する
<version>1.0-SNAPSHOT</version>
<name>Maven Quick Start Archetype</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
子プロジェクトの作成mvn archetype:create -DgroupId=com.powerdee.j2ee5sample -DartifactId=j2ee5sample-ear^ -DarchetypeArtifactId=maven-archetype-quickstart mvn archetype:create -DgroupId=com.powerdee.j2ee5sample -DartifactId=j2ee5sample-ejb^ -DarchetypeArtifactId=maven-archetype-quickstart mvn archetype:create -DgroupId=com.powerdee.j2ee5sample -DartifactId=j2ee5sample-war^ -DarchetypeArtifactId=maven-archetype-webapp j2ee5sample-ear/pom.xmlの編集。
<?xml version="1.0"?><project>
<parent>
<artifactId>j2ee5sample</artifactId>
<groupId>com.powerdee</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.powerdee.j2ee5sample</groupId>
<artifactId>j2ee5sample-ear</artifactId>
<packaging>ear</packaging> <--packaging(ear)を追記
<name>j2ee5Sample EAR Project</name> <--プロジェクト名を変更
<version>1.0-SNAPSHOT</version>
<url>http://maven.apache.org</url>
<build>
<plugins>
<plugin>
<artifactId>maven-ear-plugin</artifactId>
<configuration>
<defaultJavaBundleDir>lib</defaultJavaBundleDir> --earプラグインを追加
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency> --j2ee5sample-ejbの依存を設定
<groupId>com.powerdee.j2ee5sample</groupId>
<artifactId>j2ee5sample-ejb</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>runtime</scope>
<type>ejb</type>
</dependency>
<dependency> --j2ee5sample-warの依存を設定
<groupId>com.powerdee.j2ee5sample</groupId>
<artifactId>j2ee5sample-war</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>runtime</scope>
<type>war</type>
</dependency>
</dependencies>
</project>
j2ee5sample-ejb/pom.xmlの編集。
<?xml version="1.0"?><project>
<parent>
<artifactId>j2ee5sample</artifactId>
<groupId>com.powerdee</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.powerdee.j2ee5sample</groupId>
<artifactId>j2ee5sample-ejb</artifactId>
<packaging>jar</packaging> <--packaging(jar)を追記
<name>j2ee5Sample EJB Project</name> <--プロジェクト名を変更
<version>1.0-SNAPSHOT</version>
<url>http://maven.apache.org</url>
<dependencies>
<dependency> --javaee.jarの依存を設定
<groupId>javaee-spec</groupId>
<artifactId>javaee-spec</artifactId>
<version>5.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
j2ee5sample-war/pom.xmlの編集。
<?xml version="1.0"?><project>
<parent>
<artifactId>j2ee5sample</artifactId>
<groupId>com.powerdee</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.powerdee.j2ee5sample</groupId>
<artifactId>j2ee5sample-war</artifactId>
<packaging>war</packaging>
<name>j2ee5Sample WAR Project</name> <--プロジェクト名を変更
<version>1.0-SNAPSHOT</version>
<url>http://maven.apache.org</url>
<build>
<finalName>j2ee5sample-web</finalName>
</build>
<dependencies>
<dependency> --javaee.jarの依存を設定
<groupId>javaee-spec</groupId>
<artifactId>javaee-spec</artifactId>
<version>5.0</version>
<scope>provided</scope>
</dependency>
<dependency> --j2ee5sample-ejbの依存を設定
<groupId>com.powerdee.j2ee5sample</groupId>
<artifactId>j2ee5sample-ejb</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
親プロジェクトの設定変更親プロジェクトのpom.xmlに共通的な設定を記述しておけば、子プロジェクトへ継承される。以下を追加していく。
<?xml version="1.0" encoding="UTF-8"?>
<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>com.powerdee</groupId>
<artifactId>j2ee5sample</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<name>Maven Quick Start Archetype</name>
<url>http://maven.apache.org</url>
<modules>
<module>j2ee5sample-ear</module>
<module>j2ee5sample-ejb</module>
<module>j2ee5sample-war</module>
</modules>
<build> --コンパイル用プラグインの設定。J2SE5.0に対応
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
<defaultGoal>install</defaultGoal>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency> --子プロジェクトで共通で利用するライブラリを追加
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.0.4</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.1</version>
</dependency>
</dependencies>
</project>
ビルドの実行親プロジェクトのカレントディレクトリで以下のコマンドを実行すると、j2ee5sample-ear/target ディレクトリ下に "j2ee5sample-ear-1.0-SNAPSHOT.ear"ファイルが作成されている。 このファイルをアプリケーションサーバへデプロイすればOKです。楽々ですね。 mvn clean install おすすめ書籍
|