Hatena::ブログ(Diary)

ととのいませんでした。

2011-01-07

m2eclipseでmavenプロジェクトをつくってみる

m2eclipseのインストール

何故m2eclipseかは深く考えていない。前にいいよ、っていわれたので。

EclipseのHelp>Install New Softwareにhttp://m2eclipse.sonatype.org/sites/m2e を追加してインストールするだけ。

早速プロジェクトをつくってみる

File>New>Maven

からMaven Projectを選んでNext。

チェックアウトとかモジュールもあとでやってみよう。

archetypeってのがいわゆるスニペットというかテンプレートというかな雰囲気だが

とりあえずはまんまでつくってみることにしたいので

Create a simple projectにチェックしとく。

NextいくとなんかGroup IdとかArtifact Idをいれろといわれる。

グループ Id(groupId)はプロジェクトを一意に識別する名前で、プロジェクトのルートパッケージ名を指定するのが一般的です。

アーティファクト Id(artifactId)はプロジェクトの成果物の名前です。JARやWARにつける名前を指定してください。

http://d.hatena.ne.jp/Kishi/20090228/1235813480

ってあったんだけどhttp://maven.ozacc.com/みるとなんか適当くせぇ。

とりあえずgroup idをmaven-test、artifact idはmaven-sample01とかにしてpackageはjarにしてみる。

なんか空白あるけどまぁいいかってFinishするとなんかmavenがごにょごにょ動いてプロジェクトができる。

f:id:gulibow:20110108013315p:image

おお。できた。

依存性を追加してみる

プロジェクトのコンテキストメニューからMaven>Add Dependency

なんかcommons-langとかいれてみると候補が出て・・・こなかった。

RepositoryのIndexをUpdateしなきゃなんだがなんか失敗する。

フォーラムみてたら日によって発生とか書いてあったwので

とりあえずは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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>maven-test</groupId>
  <artifactId>maven-sample01</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencies>
  	<dependency>
  		<groupId>commons-lang</groupId>
  		<artifactId>commons-lang</artifactId>
  		<version>2.5</version>
  	</dependency>
  </dependencies>
</project>

するとまたmavenがなんかごにょごにょして、commons-langが使えるようになっている!!

package maventest;

import org.apache.commons.lang.StringUtils;

public class Sample01 {
	private static final String PREFIX = "hoge_";
	
	public static void main(String[] args) {
		System.out.println(new Sample01().addPrefix("test"));
	}	
	
	public String addPrefix(String str) {
		if(StringUtils.isBlank(str)) return PREFIX;
		
		return PREFIX + str;
	}
}

ちゃんとコンパイル通る。

テストもつけてみよう。今度は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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>maven-test</groupId>
  <artifactId>maven-sample01</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencies>
  	<dependency>
  		<groupId>commons-lang</groupId>
  		<artifactId>commons-lang</artifactId>
  		<version>2.5</version>
  		<scope>compile</scope>
  	</dependency>
  	<dependency>
  		<groupId>junit</groupId>
  		<artifactId>junit</artifactId>
  		<version>4.3</version>
  		<scope>test</scope>
  	</dependency>
  </dependencies>
</project>

junitdependencyを追加したんだけど、今度はscopeを指定してみた。testだとテストのときしか依存性ができない。


package maventest;

import static org.junit.Assert.*;

import org.junit.Test;

public class Sample01Test {

	@Test
	public void testAddPrefix() {
		String prefix = "hoge_";
		String input = "maventest";
		assertEquals(prefix + input, new Sample01().addPrefix(input));
	}

}

普通にJUnit動かすんじゃなくて、mavenのtestを実行してみる。

Run AsからMaven実行して、goalをtestにしてみる。

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running maventest.Sample01Test
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.148 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.514s
[INFO] Finished at: Sat Jan 08 03:51:32 JST 2011
[INFO] Final Memory: 6M/81M
[INFO] ------------------------------------------------------------------------

おお。動いてるっぽいぞ。

今度はわざと失敗してみる。

	@Test
	public void testAddPrefix() {
		String prefix = "baka_";
		String input = "maventest";
		assertEquals(prefix + input, new Sample01().addPrefix(input));
	}
-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running maventest.Sample01Test
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.16 sec <<< FAILURE!

Results :

Failed tests: 
  testAddPrefix(maventest.Sample01Test)

Tests run: 1, Failures: 1, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.402s
[INFO] Finished at: Sat Jan 08 03:54:19 JST 2011
[INFO] Final Memory: 6M/81M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.5:test (default-test) on project maven-sample01: There are test failures.
[ERROR] 
[ERROR] Please refer to /Users/gulibow/Documents/workspace/maven-sample01/target/surefire-reports for the individual test results.
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

おおおお。ちゃんと失敗した。

スパム対策のためのダミーです。もし見えても何も入力しないでください
ゲスト


画像認証