ぺーぺーSEのブログ

備忘録・メモ用サイト。

Maven2、Maven3のsetting.xmlの書き方まとめ

参考:http://maven.apache.org/settings.html

<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
                      http://maven.apache.org/xsd/settings-1.0.0.xsd">
  <localRepository/>
  <interactiveMode/>
  <usePluginRegistry/>
  <offline/>
  <pluginGroups/>
  <servers/>
  <mirrors/>
  <proxies/>
  <profiles/>
  <activeProfiles/>
</settings>

localRepository要素

ローカルリポジトリの設定を行う。

<localRepository>C:/Users/xxx/.m2/repository</localRepository>

デフォルトでいい。

interactiveMode要素

対話形式でパラメーターを指定するかどうかの設定を行う。(デフォルト:true)

<interactiveMode>true</interactiveMode>

デフォルトでいい。

usePluginRegistry要素

「 ${user.home}/.m2/plugin-registry.xml」を使ってプラグインのバージョンを管理するかどうかの設定を行う。(デフォルト:false)

<usePluginRegistry>false</usePluginRegistry>

デフォルトでいい。

offline要素

オフラインモードかどうかの設定を行う。(デフォルト:false)

<offline>false</offline>

デフォルトでいい。

pluginGroups要素

コマンドラインで実行した際に自動的にプラグインを呼び出してくれる。

<pluginGroups>
  <pluginGroup>org.mortbay.jetty</pluginGroup>
</pluginGroups>

デフォルトで「org.apache.maven.plugins」と「org.codehaus.mojo」は追加されてる。
デフォルトでいい。

servers要素

ダウンロードやデプロイを行うMavenリポジトリの設定は、pom.xmlrepositories要素とdistributionManagement要素で行う。
しかし、usernameやpasswordのような認証情報はpom.xmlで配布するわけにはいかないのでここで設定する。

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

mirrors要素

ミラーのMavenリポジトリを設定する。
認証情報はservers要素で設定する。

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

mirrorOf」要素を「*」にするとなんでもかんでもミラーになる。
自前のMavenリポジトリを設定する。

proxies要素

プロキシの設定を行う。

<proxies>
  <proxy>
    <active>true</active>
    <protocol>http</protocol>
    <host>[proxyのアドレス]</host>
    <port>[ポート番号(8080とか)]</port>
    <username>[アカウント]</username>
    <password>[パスワード]</password>
  <proxy>
<proxies>

profiles要素

この要素にはactivationrepositoriespluginRepositoriespropertiesの4要素が含まれる。

<!-- activation  -->
<profiles>
  <profile>
    <id>test</id>
    <activation>
      <activeByDefault>false</activeByDefault>
      <jdk>1.5</jdk>
      <os>
        <name>Windows XP</name>
        <family>Windows</family>
        <arch>x86</arch>
        <version>5.1.2600</version>
      </os>
      <property>
        <name>mavenVersion</name>
        <value>2.0.3</value>
      </property>
      <file>
        <exists>${basedir}/file2.properties</exists>
        <missing>${basedir}/file1.properties</missing>
      </file>
    </activation>
    ...
  </profile>
</profiles>
<!-- repositories  -->
<profiles>
  <profile>
    <repositories>
      <repository>
        <id>codehausSnapshots</id>
        <name>Codehaus Snapshots</name>
        <releases>
          <enabled>false</enabled>
          <updatePolicy>always</updatePolicy>
          <checksumPolicy>warn</checksumPolicy>
        </releases>
        <snapshots>
          <enabled>true</enabled>
          <updatePolicy>never</updatePolicy>
          <checksumPolicy>fail</checksumPolicy>
        </snapshots>
        <url>http://snapshots.maven.codehaus.org/maven2</url>
        <layout>default</layout>
      </repository>
    </repositories>
    <pluginRepositories>
      <!-- repositoriesと同様 -->
    </pluginRepositories>
  </profile>
</profiles>
<!-- properties  -->
<profiles>
  <profile>
    <properties>
      <user.install>${user.home}/our-project</user.install>
    </properties>
    ...
  </profile>
</profiles>

activeProfiles要素

profilesprofile要素で定義したidを指定して有効化する。

<activeProfiles>
  <!-- 有効化したい profiles.profile.id を指定する -->
  <activeProfile>env-test</activeProfile>
</activeProfiles>

設定例

めっちゃシンプルなやつ。

<?xml version="1.0" encoding="UTF-8"?>
<settings>

  <!-- ローカルリポジトリの位置を任意に指定したい場合 -->
  <!-- <localRepository>C:/foo/bar/.m2/repository</localRepository> -->

  <mirrors>
    <mirror>
      <!--This sends everything else to /public -->
      <id>nexus</id>
      <mirrorOf>*</mirrorOf>
      <url>http:// 自前のMavenリポジトリのIPまたはホスト名 /nexus/content/groups/public </url>
    </mirror>
  </mirrors>

  <activeProfiles>
    <activeProfile>nexus</activeProfile>
  </activeProfiles>

  <profiles>
    <profile>
      <id>nexus</id>
      <!--Enable snapshots for the built in central repo to direct -->
      <!--all requests to nexus via the mirror -->
      <repositories>
        <repository>
          <id>central</id>
          <url>http://central</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </repository>
      </repositories>
      <pluginRepositories>
        <pluginRepository>
          <id>central</id>
          <url>http://central</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </pluginRepository>
      </pluginRepositories>
    </profile>
  </profiles>

</settings>