Maven

http://maven.apache.org/

http://books.sonatype.com/mvnref-book/reference/

$MAVEN_HOME /usr/share/maven

Java Maven

.Net Nuget

Ruby Gem

Node Npm

★Mavenのメリット:

1.jarファイルの管理(ダウンロード、依存関係など)が簡単になる

2.Javaファイル、リソースファイル、テストファイルなどをはっきり分ける

※他サイト参照

★pom.xmlの構造

<project ...>

<modelVersion>4.0.0</

<!-- The Basics -->

<groudId>

<artifactId>

<version>

<packaging>

<dependencies>

<parent>

<dependencyManagement>

<modules>

<properties>

<!-- Build Settings -->

<build>

<reporting>

<!-- Project Meta Data -- >

<name>

<description>

<url>

<inceptionYear>

<licenses>

<organization>

<developers>

<contributors>

<!-- Environment -->

<issueManagement>

<ciManagement>

<mailingLists>

<scm>

<prerequisites>

<repositories>

<pluginRepositories>

<distributionManagement>

<profiles>

</project>

★Encoding設定

<properties>

<project.build.sourceEncoding>utf-8</project.build.sourceEncoding>

</properties>

★Module化

親Module

<groudId>...

<artifactId>...

<version>...

<name>...

<packaging>pom</packaging>

<modules>

<module>MySubApp1</module>

<module>MySubApp2</module>

</modules>

<scm>...

<distributionManagement>...

<properties>...

<dependencyManagement>...

<dependencies>...

<build>...

<profiles>...

子Module

<parent>

<groudId>...

<artifactId>...

<version>...

</parent>

<artifactId>MySubApp1</artifactId>

<relativePath>../pom.xml</relativePath>

<name>...

★Profileの使い方

<properties>

<testGroupName>release</testGroupName>

<webXmlPath>webxml/release</webXmlPath>

</properties>

<profiles>

<profile>

<id>dev</id>

<properties>

<testGroupName>develop</testGroupName>

<webXmlPath>webxml/develop</webXmlPath>

</properties>

</profile>

</profiles>

<build>

<plugins>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-surefire-plugin</artifactId>

<version>2.12</version>

<configuration>

<groups>${testGroupName}</groups>

</configuration>

</plugin>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-war-plugin</artifactId>

<version>2.1.1</version>

<configuration>

<webResources>

<resource>

<directory>${webXmlPath}</directory>

<targetPath>WEB-INF</targetPath>

</resource>

</webResources>

</configuration>

</plugin>

</plugins>

</build>

mvn test release

mvn test -Pdev develop

mvn package webxml/releaseのxml → WEB-INF

mvn package -Pdev webxml/developのxml → WEB-INF

mvn clean test/verify

mvn clean test/verify -Pdev

mvn -Pdev -pl <project-name> test/verify -Dtest=<xxxIT>#<testxxx> -offline; zenity --info

mvn clean install -offline -pl <project-name> -am -DskipTests=true (-am: 関連プロジェクトも)

サンプル

<project ...>

...

<profiles>

<!-- 開発環境 -->

<profile>

<id>development</id>

<activation>

<activeByDefault>true</activeByDefault>

</activation>

<properties>

<resources.directory>

${basedir}/src/main/resources/development

</resources.directory>

<log.level>ALL</log.level>

<web.protocol>HTTP</web.protocol>

<web.host>localhost</web.host>

<web.port>8080</web.port>

<jdbc.url>jdbc:mysql://127.0.0.1:3306/test</jdbc.url>

<jdbc.username>xxx</jdbc.username>

<jdbc.password>yyy</jdbc.password>

<jboss-as.home>${env.JBOSS_HOME}</jboss-as.home>

<jboss-as.hostname>127.0.0.1</jboss-as.hostname>

<jboss-as.port.offset>100</jboss-as.port.offset>

</properties>

</profile>

<!-- 本番環境 -->

<profile>

<id>production</id>

<activation>

<activeByDefault>false</activeByDefault>

</activation>

<properties>

<resources.directory>

${basedir}/src/main/resources/production

</resources.directory>

<log.level>WARN</log.level>

<web.protocol>HTTP</web.protocol>

<web.host>...</web.host>

<web.port>8080</web.port>

<jdbc.url>jdbc:mysql://x.x.x.x:3306/db</jdbc.url>

<jdbc.username>xxx</jdbc.username>

<jdbc.password>yyy</jdbc.password>

<jboss-as.home>

${env.JBOSS_HOME:/opt/jboss}

</jboss-as.home>

<jboss-as.hostname>...</jboss-as.hostname>

<jboss-as.port.offset>0</jboss-as.port.offset>

</properties>

</profile>

</profiles>

<build>

<resources>

<resource>

<directory>${resources.directory}</directory>

<filtering>true</filtering>

</resource>

</resources>

</build>

...

</project>

log4j.rootLogger=${log.level}, ...

...

jdbc.driverClassName=org.gjt.mm.mysql.Driver

jdbc.url=${jdbc.url}

jdbc.username=${jdbc.username}

jdbc.password=${jdbc.password}

★warファイル中のclassを共用する

依頼元

<plugin>

<artifactId>maven-war-plugin</artifactId>

<version>2.1.1</version>

<configuration>

<attachClasses>true</attachClasses>

<classesClassifier>classes</classesClassifier>

</configuration>

</plugin>

下記のファイルが作成された

example.war

example-classes.jar

依頼先

<dependency>

<groupId>...</groupId>

<artifactId>...</artifactId>

<version>...</version>

<classifier>classes</classifier>

</dependency>

★Eclipseに向けPlugin

〇m2e

http://m2eclipse.sonatype.org/sites/m2e

〇m2e-wtp

http://m2eclipse.sonatype.org/sites/m2e-extras

★Repositoryについて(dependency検索など)

ローカルRepository C:\Documents and Settings\ユーザ名\.m2\repository

リモートRepository

Central Repository http://search.maven.org/

Sonatype Server https://repository.sonatype.org/

検索 http://mvnrepository.com/

★Mavenのインストールと操作

sudo apt-get install maven

maven -version

mv my/settings.xml ~/.m2/

mvn [フェーズ] [プラグイン:ゴール]

例:mvn clean test javadoc:javadoc

mvn clean install

mvn -Dmaven.test.skip=true clean install

mvn -Dmaven.test.skip=true clean package

mvn install -Dmaven.test.skip=true テストのコンパイルと実行をスキップ

mvn install -DskipTests=true テストの実行をスキップ

mvn archetype:generate -DgroupId=com.example -DartifactId=MyApp -DarchetypeArtifactId=maven-archetype-quickstart

mvn archetype:generate -DgroupId=com.example -DartifactId=MyApp -DarchetypeArtifactId=maven-archetype-webapp

mvn site:run

mvn site

mvn javadoc:javadoc

mvn javadoc:test-javadoc

mvn javadoc:aggregate マルチモジュール

Coverageレポート

Cobertura テストされたコードの割合

Clover メトリクス

mvn cobertura:cobertura

品質レポート

Checkstyle 静的なコードスタイル分析(ソースファイルを対象に検査)

PMD/CPD コード分析、Copy&Paste検出

FindBugs コード分析、バグパターンを検出(コンパイルされたクラスファイルを対象に検査)

JDepend パッケージごとに設計品質メトリクス

Taglist 静的なコード分析、@totoや//TODOを検出

mvn checkstyle:checkstyle

単体レポート

Surefire

レポートの一元管理

mvn dashboard:dashboard

Webサーバ

mvn jetty:run

<build>

<plugins>

<plugin>

<groupId>org.mortbay.jetty</groupId>

<artifactId>maven-jetty-plugin</artifactId>

<version>6.1.17</version>

</plugin>

</plugins>

</build>

Dependency調査

mvn dependency:tree -Dscope=test

★Nexusの導入(Ubuntuの場合)

http://www.sonatype.org/

前提:Java、Tomcat

sudo service tomcat6 stop

sudo mv nexus.war /var/lib/tomcat6/webapps/

sudo mkdir /usr/share/tomcat6/sonatype-work フォルダ構造は下記

sudo chown -R tomcat6:tomcat6 /usr/share/tomcat6/sonatype-work

sudo service tomcat6 start

sonatype-work/

├── README.txt

└── nexus

/usr/share/maven/conf/settings.xml

Nexusで追加したRepositoryを<mirror>に設定

http://localhost:8080/nexus/

★Eclipseにpom.xmlエラーの解決方法

右クリック→ Maven → update Project

★ビルドライフサイクル

・default

validate,compile, test, package, integration-test, verify, install, deploy

・clean

pre-clean, clean, post-clean

・site

pre-site, site, post-site, site-deploy

★プラグイン

Compile設定

pom.xml

カテゴリ化テスト

<build>

<plugins>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-surefire-plugin</artifactId>

<version>2.11</version>

<configuration>

<excludeGroups>

<excludeGroup>xxx.yyy</excludeGroup>

</excludeGroups>

</configuration>

<dependencies>

<dependency>

<groupId>org.apache.maven.surefire</groupId>

<artifactId>surefire-junit47</artifactId>

<version>2.11</version>

</dependency>

</dependencies>

</plugin>

</plugins>

</build>

カバレッジレポート

<build>

<plugins>

<plugin>

<groupId>org.jacoco</groupId>

<artifactId>jacoco-maven-plugin</artifactId>

<version>0.5.7.201204190339</version>

</plugin>

</plugins>

</build>

mvn clean jacoco:prepare-agent test

mvn jacoco:report

★Properties

・Environment Variable Properties

${env.PATH}

・Project Properties (pom.xml)

${project.version} <project><version>1.0</version></project>

・Settings Properties (settings.xml)

${settings.offline} <settings><offline>false</offline></settings>

・Java System Properties

${java.home} java.lang.System.getProperties()

・User-defined Properties (pom.xml)

${someVar} <properties><someVar>value</someVar></properies>

${basedir} pom.xmlが置かれているフルパス

${project.groupId}

${project.artifactId}

${project.version}

${project.name}

${project.description}

${project.build.directory} target

${project.build.outputDirectory} target/classes

${project.build.testOutputDirectory} target/test-classes

${project.compileSourceRoots} src/main/java

${project.compileClasspathElements} target/classes、jarファイルパス群

${project.testCompileSourceRoots} src/test/java

${project.testClasspathElements} target/classes、target/test-classes、jarファイルパス群

${project.reporting.outputDirectory} target/site

★EclipseでConsoleに文字化けの対策

<project>

...

<properties>

<argLine>-Dfile.encoding=UTF-8</argLine>

</properties>

...

</project>

★Resource設定

<build>

...

<resources>

<resource>

<directory>src/main/java</directory>

<includes>

<include>**/*.hbm.xml</include>

</includes>

</resource>

<resource>

<directory>src/main/resources</directory>

<includes>

<include>**/*.xml</include>

<include>**/*.properties</include>

</includes>

</resource>

</resources>

...

</build>

★Release

mvn clean

mvn release:clean

mvn release:prepare

mvn release:prepare -DdevelopmentVersion=<NEXT_DEV_VERSION>-SNAPSHOT -Dtag=<CURRENT_VERSION> -DreleaseVersion=<CURRENT_VERSION>

mvn release:prepare -DdevelopmentVersion=1.0.0.RC1-SNAPSHOT -Dtag=1.0.0.Beta2 -DreleaseVersion=1.0.0.Beta2

★Adding a custom jar as a maven dependency

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-install-plugin</artifactId>

<version>2.4</version>

<executions>

<execution>

<phase>initialize</phase>

<goals>

<goal>install-file</goal>

</goals>

<configuration>

<groupId>myGroupId</groupId>

<artifactId>myArtifactId</artifactId>

<version>myVersion</version>

<packaging>jar</packaging>

<file>${basedir}/lib/my.jar</file>

</configuration>

</execution>

</executions>

</plugin>