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
- プロジェクト管理 ツール
- ビルド管理 と依存 関係 (複数 の 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 | はい |
provided | JDK、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
- 新 しい開発者 がプロジェクトに参加 しやすい
- コード、プロパティファイル、ユニットテスト、Web ファイルなどを簡単 に見 つけられる
- Maven プロジェクトは移植性 がある
Deploy
| タイプ | デプロイ方法 |
|---|---|
| jar | プライベート Nexus Server にデプロイ |
| war | Maven Cargo を使用 して Tomcat Server にデプロイ、または Jenkins(より強力 ) |