Skip to content

Instantly share code, notes, and snippets.

@ksomemo
Last active August 29, 2015 14:07
Show Gist options
  • Save ksomemo/4599dc3bcf7c290f24ef to your computer and use it in GitHub Desktop.
Save ksomemo/4599dc3bcf7c290f24ef to your computer and use it in GitHub Desktop.
eclipseからMavenとpom.xmlをいろいろ試した

maven

pom.xmlの作成

Eclipse上で作成した

手順

  1. プロジェクト右クリックメニュー
  2. 構成
  3. Maven

新規POMの作成

  • グループID
  • アーティファクトID
  • バージョン
  • パッケージング
    • jar
    • pom?
    • war
  • 名前
  • 説明

とりあえず適当につけた

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>junittest</groupId>
  <artifactId>junittest</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>namesample</name>
  <description>description</description>
  <build>
    <sourceDirectory>src/main</sourceDirectory>
    <testSourceDirectory>src/test</testSourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

pom.xml用エディタ画面

下記のタブが存在する

  • 概要
  • 依存関係
  • 依存関係階層
    • よくわからない… 実効POM
    • 最初にローディングされるが、あとは生XML pom.xml
    • 生XML

概要

作成時につけた情報が表示されている

  • 成果物
    • プロジェクト
    • URL
    • インセプション?
  • その他
    • 組織
    • SCM
    • 発行管理
    • 継続統合
    • 成果物とほぼ同じ
    • パッケージングが存在しない
    • 相対パスが存在する
  • プロパティー
    • 作成ボタンから追加できる
    • 名前と値を決定する
  • モジュール
    • pomパッケージのプロジェクトのみモジュール宣言可能
    • 今回はjarなので関係なさそう

依存関係

依存関係

  • グループID
  • アーティファクトID
  • バージョン
  • スコープ
    • compile
    • provided
    • runtime
    • test
    • system
例:twitter4j
  • testにjunitなど
  • providedにlog4jなど

作成後は、

  • プロパティの変更可能
    • 作成時のパラメータの変更
    • オプションとするかの選択

依存関係の管理

依存関係と同じなので、とりあえず作ってみたが、エラーになった

ArtifactDescriptionException Faild to read a... en2/junit/junit/4.11/junit-4.11.pom
ArtifactDescriptorException: Failed to read artifact descriptor for junit:junit:jar:4.11: ArtifactResolutionException: Failure to transfer junit:junit:pom:4.11 from http://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced. オリジナル・エラー: Could not transfer artifact junit:junit:pom:4.11 from/to central (http://repo.maven.apache.org/maven2): null to http://repo.maven.apache.org/maven2/junit/junit/4.11/junit-4.11.pom
解決
  • http://mvnrepository.com/ から探してグループIDとアーティファクトIDを入力する

  • Maven以外にも設定例が載っている

    • Gradle(Groovy)
    • SBT(Scala)
    • Leiningen(Clojure)
  • junitは4.11から4.10にしたら消えた

    • mvnrepositoryで一番使われているバージョンであったため直した
  • log4jも同様に修正した

    • 1.2.17から1.2.16

実行

pom.xmlを開いてC-F11で実行

実行メニュー

下記を実行できる

  • build
  • build...(新規作成)
  • clean
  • generate-sources
  • install
  • test

とりあえずbuild

構成編集のダイアログが出現する

  • 基底ディレクトリ(変数を使える)
  • ゴール
  • プロファイル
    • オンライン・オフライン
    • デバッグ出力
    • テストスキップ
    • 再帰的実行
    • 実行スレッド数
  • パラメータ追加

build

ゴール設定せずに実行

[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/NoGoalSpecifiedException

ゴールにjarを指定して実行

[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/LifecyclePhaseNotFoundException

あとでやる

install

Maven依存関係に依存関係に記載したものが作成された

clean

targetディレクトリが削除された

test

  • テストが実行された
  • テスト結果レポートが作成される
  • targetにgenerate-sources関連があった

いろいろ試した後のpom.xml

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <groupId>junittest</groupId>
  <artifactId>junittest</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>namesample</name>
  <description>description</description>
  <url>http://xxxxxxx.sample.co.jp</url>
  <inceptionYear>インセプション</inceptionYear>
  <organization>
    <name>組織名</name>
    <url>http://xxxxxxx.sample.co.jp</url>
  </organization>
  <scm>
    <connection>SCM接続</connection>
    <developerConnection>SCM開発者</developerConnection>
    <tag>タグ</tag>
    <url>http://xxxxxxx.sample.co.jp</url>
  </scm>
  <issueManagement>
    <system>発行管理システム</system>
    <url>http://xxxxxxx.sample.co.jp</url>
  </issueManagement>
  <ciManagement>
    <system>継続統合システム</system>
    <url>http://xxxxxxx.sample.co.jp</url>
  </ciManagement>
  <properties>
    <property2>value2</property2>
    <property1>value1</property1>
  </properties>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.10</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.16</version>
      <scope>provided</scope>
      <optional>true</optional>
    </dependency>
  </dependencies>
  <repositories>
    <repository>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <id>central</id>
      <name>Central Repository</name>
      <url>http://repo.maven.apache.org/maven2</url>
    </repository>
  </repositories>
  <pluginRepositories>
    <pluginRepository>
      <releases>
        <updatePolicy>never</updatePolicy>
      </releases>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <id>central</id>
      <name>Central Repository</name>
      <url>http://repo.maven.apache.org/maven2</url>
    </pluginRepository>
  </pluginRepositories>
  <build>
    <sourceDirectory>D:\Users\xxxx\dev\workspace\junittest\src\main</sourceDirectory>
    <scriptSourceDirectory>D:\Users\xxxx\dev\workspace\junittest\src\main\scripts</scriptSourceDirectory>
    <testSourceDirectory>D:\Users\xxxx\dev\workspace\junittest\src\test</testSourceDirectory>
    <outputDirectory>D:\Users\xxxx\dev\workspace\junittest\target\classes</outputDirectory>
    <testOutputDirectory>D:\Users\xxxx\dev\workspace\junittest\target\test-classes</testOutputDirectory>
    <resources>
      <resource>
        <directory>D:\Users\xxxx\dev\workspace\junittest\src\main\resources</directory>
      </resource>
    </resources>
    <testResources>
      <testResource>
        <directory>D:\Users\xxxx\dev\workspace\junittest\src\test\resources</directory>
      </testResource>
    </testResources>
    <directory>D:\Users\xxxx\dev\workspace\junittest\target</directory>
    <finalName>junittest-0.0.1-SNAPSHOT</finalName>
    <pluginManagement>
      <plugins>
        <plugin>
          <artifactId>maven-antrun-plugin</artifactId>
          <version>1.3</version>
        </plugin>
        <plugin>
          <artifactId>maven-assembly-plugin</artifactId>
          <version>2.2-beta-5</version>
        </plugin>
        <plugin>
          <artifactId>maven-dependency-plugin</artifactId>
          <version>2.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-release-plugin</artifactId>
          <version>2.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <executions>
          <execution>
            <id>default-testCompile</id>
            <phase>test-compile</phase>
            <goals>
              <goal>testCompile</goal>
            </goals>
            <configuration>
              <source>1.6</source>
              <target>1.6</target>
            </configuration>
          </execution>
          <execution>
            <id>default-compile</id>
            <phase>compile</phase>
            <goals>
              <goal>compile</goal>
            </goals>
            <configuration>
              <source>1.6</source>
              <target>1.6</target>
            </configuration>
          </execution>
        </executions>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-clean-plugin</artifactId>
        <version>2.4.1</version>
        <executions>
          <execution>
            <id>default-clean</id>
            <phase>clean</phase>
            <goals>
              <goal>clean</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-install-plugin</artifactId>
        <version>2.3.1</version>
        <executions>
          <execution>
            <id>default-install</id>
            <phase>install</phase>
            <goals>
              <goal>install</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.5</version>
        <executions>
          <execution>
            <id>default-resources</id>
            <phase>process-resources</phase>
            <goals>
              <goal>resources</goal>
            </goals>
          </execution>
          <execution>
            <id>default-testResources</id>
            <phase>process-test-resources</phase>
            <goals>
              <goal>testResources</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.10</version>
        <executions>
          <execution>
            <id>default-test</id>
            <phase>test</phase>
            <goals>
              <goal>test</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.3.2</version>
        <executions>
          <execution>
            <id>default-jar</id>
            <phase>package</phase>
            <goals>
              <goal>jar</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-deploy-plugin</artifactId>
        <version>2.7</version>
        <executions>
          <execution>
            <id>default-deploy</id>
            <phase>deploy</phase>
            <goals>
              <goal>deploy</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-site-plugin</artifactId>
        <version>3.0</version>
        <executions>
          <execution>
            <id>default-site</id>
            <phase>site</phase>
            <goals>
              <goal>site</goal>
            </goals>
            <configuration>
              <outputDirectory>D:\Users\xxxx\dev\workspace\junittest\target\site</outputDirectory>
              <reportPlugins>
                <reportPlugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-project-info-reports-plugin</artifactId>
                </reportPlugin>
              </reportPlugins>
            </configuration>
          </execution>
          <execution>
            <id>default-deploy</id>
            <phase>site-deploy</phase>
            <goals>
              <goal>deploy</goal>
            </goals>
            <configuration>
              <outputDirectory>D:\Users\xxxx\dev\workspace\junittest\target\site</outputDirectory>
              <reportPlugins>
                <reportPlugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-project-info-reports-plugin</artifactId>
                </reportPlugin>
              </reportPlugins>
            </configuration>
          </execution>
        </executions>
        <configuration>
          <outputDirectory>D:\Users\xxxx\dev\workspace\junittest\target\site</outputDirectory>
          <reportPlugins>
            <reportPlugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-project-info-reports-plugin</artifactId>
            </reportPlugin>
          </reportPlugins>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <reporting>
    <outputDirectory>D:\Users\xxxx\dev\workspace\junittest\target\site</outputDirectory>
  </reporting>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment