Maven

What is Maven?

  flowchart TB
	MR[Maven Repos]
	LR[Local Repos]
	RR[Remote Repos]
	CR[Central Repos]
	PR[Private Repos]
	ORR[Others Remote Repos]

	MR --> LR
	MR ------>|if local repos doesn't have lib| RR

	RR ----> CR
	RR ----> PR
	RR ----> ORR
  1. プロジェクト管理(かんり) ツール
  2. ビルド管理(かんり)依存(いぞん) 関係(かんけい)複数(ふくすう) の jar を管理(かんり)

Maven Repository

  flowchart LR
	subgraph Center
		C[(Maven)]
		end

	subgraph Left
	direction TB
		subgraph App
		Spring
		Hibernate
		Commons_Logging
		JSON
		...
		end
	end

	subgraph Right
	direction TB
		subgraph Maven_Central_Repository_Remote_Internet
		Spring_JAR_files
		Hibernate_JAR_files
		ApacheCommons_JAR_files
		JSON_JAR_files
		end
	end

	Left <====> Center <====> Right

settings.xml

Maven のグローバル設定(せってい)使用(しよう)

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
                          https://maven.apache.org/xsd/settings-1.0.0.xsd">
  <localRepository/>
  <interactiveMode/>
  <usePluginRegistry/>
  <offline/>
  <pluginGroups/>
  <servers/>
  <mirrors/>
  <proxies/>
  <profiles/>
  <activeProfiles/>
</settings>

LocalRepository

ローカルリポジトリのパスを設定(せってい) 。デフォルト:~/.m2/repository

<localRepository>${user.home}/.m2/repository</localRepository>

InteractiveMode

Maven がユーザーとの対話(たいわ)必要(ひつよう) とするかどうか:

<interactiveMode>true</interactiveMode>

Offline

Maven がオフラインモードで動作(どうさ) する必要(ひつよう) があるかどうか。ネットワークやセキュリティの理由(りゆう) でリモートリポジトリに接続(せつぞく) できない場合(ばあい)便利(べんり)

<offline>false</offline>

Servers

username、password などの情報(じょうほう)pom.xml(ふく) めるべきではなく、settings.xml設定(せってい) できます:

<servers>
  <server>
    <id>server001</id>
    <username>my_login</username>
    <password>my_password</password>
    <privateKey>${usr.home}/.ssh/id_dsa</privateKey>
    <passphrase>some_passphrase</passphrase>
    <filePermissions>664</filePermissions>
    <directoryPermissions>775</directoryPermissions>
  </server>
</servers>

Mirrors

リポジトリリストのダウンロードミラーリストを設定(せってい)

<mirrors>
  <mirror>
    <id>planetmirror.com</id>
    <name>PlanetMirror Australia</name>
    <url>http://downloads.planetmirror.com/pub/maven2</url>
    <mirrorOf>central</mirrorOf>
  </mirror>
</mirrors>

Proxies

プロキシを設定(せってい)

<proxies>
  <proxy>
    <id>myproxy</id>
    <active>true</active>
    <protocol>http</protocol>
    <host>proxy.somewhere.com</host>
    <port>8080</port>
    <username>proxyuser</username>
    <password>somepassword</password>
    <nonProxyHosts>*.google.com|ibiblio.org</nonProxyHosts>
  </proxy>
</proxies>

Dependency

リモートリポジトリからローカルに jar をダウンロード。jar ファイル(かん)依存(いぞん) 関係(かんけい)伝播(でんぱ)

Scope

Scope説明(せつめい)デプロイ() にパッケージ
compileデフォルト() 、この jar はすべての段階(だんかい)必要(ひつよう)はい
testテスト() のみ必要(ひつよう)(れい) : junitいいえ
runtime実行(じっこう) () のみ必要(ひつよう)(れい) : jdbcはい
providedJDK、Tomcat などによって提供(ていきょう) されることを期待(きたい)(れい) : servlet.jarいいえ
systemシステムによって提供(ていきょう) 、systemPath と()() わせて使用(しよう)-

Plugins

maven-compiler-plugin

直接(ちょくせつ) パッケージ化。依存(いぞん) 関係(かんけい) はパッケージ化せず、ソースコードのみを JAR にパッケージ化:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.1</version>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
        <encoding>UTF-8</encoding>
        <skipTests>true</skipTests>
        <verbose>true</verbose>
        <showWarnings>true</showWarnings>
        <fork>true</fork>
        <meminitial>128m</meminitial>
        <maxmem>512m</maxmem>
    </configuration>
</plugin>

maven-dependency-plugin

プロジェクトの依存(いぞん) するすべての jar パッケージを複製(ふくせい)

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-dependency-plugin</artifactId>
	<executions>
		<execution>
			<id>copy</id>
			<phase>package</phase>
			<goals>
				<goal>copy-dependencies</goal>
			</goals>
			<configuration>
				<outputDirectory>${project.build.directory}/lib</outputDirectory>
				<overWriteReleases>false</overWriteReleases>
				<overWriteSnapshots>false</overWriteSnapshots>
				<overWriteIfNewer>true</overWriteIfNewer>
			</configuration>
		</execution>
	</executions>
</plugin>

maven-assembly-plugin

<plugin>
	<artifactId>maven-assembly-plugin</artifactId>
	<executions>
		<execution>
			<phase>package</phase>
			<goals>
				<goal>single</goal>
			</goals>
		</execution>
	</executions>
	<configuration>
		<descriptorRefs>
			<descriptorRef>jar-with-dependencies</descriptorRef>
		</descriptorRefs>
	</configuration>
</plugin>

Descriptor オプション:

オプション説明(せつめい)
binデフォルトのパッケージ化に類似(るいじ) 、bin ディレクトリのファイルをパッケージに(ふく) める
jar-with-dependenciesすべての依存(いぞん) 関係(かんけい)解凍(かいとう) して生成物(せいせいぶつ) にパッケージ化
srcソースディレクトリのファイルのみをパッケージ化
projectプロジェクト全体(ぜんたい) のリソースをパッケージ化

maven-surefire-plugin

テストをスキップ、mvn package -Dmaven.test.skip同等(どうとう)

<plugin>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.6</version>
    <configuration>
        <skip>true</skip>
    </configuration>
</plugin>

Private Repository Server

  • JFrog Artifactory
  • Sonatype Nexus
  • Apache Archiva

Benefits

  1. (あたら) しい開発者(かいはつしゃ) がプロジェクトに参加(さんか) しやすい
  2. コード、プロパティファイル、ユニットテスト、Web ファイルなどを簡単(かんたん)() つけられる
  3. Maven プロジェクトは移植性(いしょくせい) がある

Deploy

タイプデプロイ方法(ほうほう)
jarプライベート Nexus Server にデプロイ
warMaven Cargo を使用(しよう) して Tomcat Server にデプロイ、または Jenkins(より強力(きょうりょく)