LoginSignup
26
25

More than 5 years have passed since last update.

『Jenkins実践入門』のAntプロジェクトのサンプルをMavenプロジェクトにする

Last updated at Posted at 2014-06-19

目次


はじめに

『Jenkins実践入門』では、Antプロジェクトのサーブレットがサンプルで解説されている。

Jenkinsでのビルドに、AntではなくMavenを使用してみたいが、残念ながら pom.xmlファイルや設定例が掲載されていなかった。

ということで、『Jenkins実践入門』で扱われている、Antプロジェクトのサーブレットサンプル「sampleproject」を基にして
これをMavenプロジェクトに変換し、pom.xmlを編集し、JOBの設定を行ってみたので、その設定方法を紹介します。

最終構成は以下になります。
Jenkins+Maven+JUnit+CheckStyle+FindBugs+Cobertura+JavaNCSS

※pom.xmlは結構嵌まりました。

findbugsに関しては、一本必要なファイルがなかったので、追加しました。
findbugs-exclude.xml

目指している開発スタイル

目指している開発スタイルは以下の様なものになります。

  • eclipseでソースをコーディングして
  • eclipseでテストソースをコーディングして
  • subversionにソースをコミットして
  • redmineに連携させて
  • Jenkinsでビルドして
  • coberturaでjavaコーディイング規約を自動でチェックして
  • findbugsでバグの温床を事前発見して
  • テストを実施して
  • テストを集計し
  • コードのカバレッジを調査して集計し
  • ソースコードの分量を計測し
  • 自動でwarファイルを作成し
  • 自動でwarファイルをデプロイする

事前準備

環境構成

OS main middleware JDK Maven Subversion Ant
Jenkins Server CentOS 6.2 64bit (VMwarePlayer仮想マシン) Jenkins -1.533- 1.567 1.6.0_37 3.2.1 1.6.11 1.9.2
Development PC Windows7 64bit eclipse Kepler(Pleiades All in One 4.3.2.v20140321) 1.6.0_37 3.2.1 1.9.2

開発機へeclipse/JRE/Tomcatの準備

Full Editionを選べばJREもTomcatもついてくる。

※一点だけ注意すべきことがある。
tools.jarをビルド時に使用するが、jenkins側と合わせるため、場所を移動しておく。(というかコピーでいい。)
jenkinsで自動インストールされるJDKのライブラリとeclipseのpleiades内包のJREのライブラリのパスが合わないため。

例)

C:\eclipse\pleiades-e4.3\java\lib\tools.jar

開発機へサンプルプロジェクトをダウンロード

解凍して、フォルダ名を sampleproject2 とする。(AntのProjectとは違うという意味で…)

Jenkins Server に Subversionの準備


# yum install -y subversion

詳細は、以下を参照
http://centossrv.com/subversion.shtml

例)リポジトリとして「svnrepo1」を作成する。

開発機からサンプルプロジェクトをSVNリポジトリに向けてインポート

jenkins_maven_eclipse3.jpg

SVN URL:例)
http://jenkinsserver/svnrepo1/trunk/sampleproject2

開発機でeclipseにSVNリポジトリから該当のプロジェクトをチェックアウトする

SVNリポジトリ・エクスポローラ パースペクティブで取り込む。

取り込んだ後、プロジェクトの構成をMavenに変換する

eclipse_maven.jpg

Jenkins Server に Redmineの準備(あればjenkinsとも連携できる)

詳細は、以下を参照
http://blog.redmine.jp/articles/2_5/installation_centos/

Jenkins Server に Tomcatの準備


# yum install -y tomcat6 tomcat6-webapps tomcat6-admin-webapps

詳細は、以下を参照
http://kajuhome.com/tomcat6.shtml

Jenkins Server に Jenkinsの準備

jenkinsのダウンロード

http://jenkins-ci.org/
http://mirrors.jenkins-ci.org/war/latest/jenkins.war

Jenkinsの起動

単体起動の場合

Tomcatを利用しないでも簡易的に起動できます。
デフォルトで8080ポートを使うので、利用されていないか事前に確認すること。


# java -jar /root/tools/jenkins.war

URL:
http://localhost:8080/

Tomcatを利用して起動の場合

webappsにjenkins.warを配置

URL:
http://localhost:8080/jenkins/

Jenkinsの初期設定

JDK、Ant、Mavenの自動インストールを設定する。

JDKに関しては、念のためJDK7とJDK6の2つをインストールしました。
ちなみに、Jenkins 1.533 ではJDKの自動インストールに失敗したため、現時点最新の Jenkins 1.567 にしました。

ちなみに、Oracleのアカウントが必要になるので、事前に取得しておくこと。

jenkins_4.jpg

JOBの設定

JOBのプロジェクト名 sampleproject2_JOB コメント
Subversion リポジトリURL http://localhost/svnrepo1/trunk/sampleproject2
Subversion ID hoge
Subversion Password password
Subversion ローカルモジュールディレクトリ BUILD
Maven 使用するMaven Maven3.2.1
Maven ゴール clean cobertura:cobertura checkstyle:checkstyle package site ※実際にはひとつずつ丁寧に実行したほうがいいと思います。

jenkins_maven_sampleproject2_JOB5.jpg

ビルド後の処理追加

レポートのXMLのパスが肝です。

file path
checkstyle **/target/checkstyle-result.xml
findbugs **/target/findbugs.xml
cobertura **/target/site/cobertura/coverage.xml
junit */target/surefire-reports/.xml
javancss **/target/javancss-raw-report.xml

jenkins_maven_jobsetting1.jpg

jenkins_maven_jobsetting2.jpg

開発機にてPOMファイルの編集

試行錯誤の結果、最終的に以下の編集でうまくいきました。
(※格闘していたため、冗長な設定が一部残存していると思われますが。。。)

pom.xml
  <properties>
     <findbugs.version>2.5.2</findbugs.version>
     <checkstyle.version>2.9.1</checkstyle.version>
     <surefire.reportplugin.version>2.12.4</surefire.reportplugin.version>
     <cobertura.version>2.5.2</cobertura.version>
  </properties>
  <build>
    <sourceDirectory>src/java</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>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.3</version>
        <configuration>
          <warSourceDirectory>WebContent</warSourceDirectory>
          <failOnMissingWebXml>false</failOnMissingWebXml>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-site-plugin</artifactId>
        <version>3.3</version>
      </plugin>
      <plugin>
         <groupId>org.codehaus.mojo</groupId>
          <artifactId>cobertura-maven-plugin</artifactId>
         <version>${cobertura.version}</version>
         <configuration>
             <formats>
                 <format>xml</format>
                 <format>html</format>
             </formats>
             <instrumentation>
                 <excludes>
                     <exclude>**/*Test.class</exclude>
                 </excludes>
             </instrumentation>
         </configuration>
      </plugin>
      <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>javancss-maven-plugin</artifactId>
          <version>2.0</version>
      </plugin>
    </plugins>
    <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>cobertura-maven-plugin</artifactId>
                    <version>2.5.1</version>
                    <configuration>
                        <executions>
                            <execution>
                                <phase>package</phase>
                                <goals>
                                    <goal>cobertura</goal>
                                </goals>
                            </execution>
                        </executions>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-checkstyle-plugin</artifactId>
                    <version>2.9.1</version>
                </plugin>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>findbugs-maven-plugin</artifactId>
                    <version>2.5.2</version>
                </plugin>
            </plugins>
    </pluginManagement>
  </build>
  <reporting>
     <plugins>
        <plugin>
           <groupId>org.codehaus.mojo</groupId>
           <artifactId>findbugs-maven-plugin</artifactId>
           <version>${findbugs.version}</version>
           <configuration>
                    <effort>Max</effort>
                    <xmlOutput>true</xmlOutput>
           </configuration>
        </plugin>
        <plugin>
           <groupId>org.apache.maven.plugins</groupId>
           <artifactId>maven-checkstyle-plugin</artifactId>
           <version>${checkstyle.version}</version>
        </plugin>
        <plugin>
           <groupId>org.apache.maven.plugins</groupId>
           <artifactId>maven-surefire-report-plugin</artifactId>
           <version>${surefire.reportplugin.version}</version>
        </plugin>
        <plugin>
           <groupId>org.codehaus.mojo</groupId>
           <artifactId>cobertura-maven-plugin</artifactId>
           <version>${cobertura.version}</version>
           <configuration>
               <formats>
                   <format>xml</format>
               </formats>
           </configuration>
        </plugin>
        <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>javancss-maven-plugin</artifactId>
          <version>2.0</version>
        </plugin>
      </plugins>
  </reporting>
  <profiles>
    <profile>
        <id>default-tools.jar</id>
        <activation>
            <property>
                <name>java.vendor</name>
                <value>Sun Microsystems Inc.</value>
            </property>
        </activation>
        <dependencies>
            <dependency>
                <groupId>com.sun</groupId>
                <artifactId>tools</artifactId>
                <version>1.6.0</version>
                <scope>system</scope>
                <systemPath>${java.home}/../lib/tools.jar</systemPath>
            </dependency>
        </dependencies>
    </profile>
  </profiles>
  <parent>
    <groupId>org.easymock</groupId>
    <artifactId>easymock-parent</artifactId>
    <version>3.2</version>
  </parent>
  <dependencies>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.easymock</groupId>
        <artifactId>easymock</artifactId>
        <version>3.2</version>
        <scope>test</scope>
    </dependency>
  </dependencies>

開発環境に「findbugs-exclude.xml」を用意する

ダウンロード
http://code.ohloh.net/file?fid=hX9ynZAPfrJdP_eUd13nWm--fWk&cid=QYTpoM6QzVs&s=&fp=1166&mp&projSelected=true#L0

ファイル名を以下に変更する。
findbugs-exclude.xml

追加する場所は以下になります。

jenkins_maven_eclipse1.jpg

SVNコミットする(eclipseより)

jenkins_maven_eclipse2.jpg

Jenkins側でビルドの実行

jenkins_maven_sampleproject2_JOB1.jpg

ビルドの経過

SVNにコミットしたものを正しく取り込んでいる事を確認できる。

jenkins_maven_sampleproject2_JOB2.jpg

後半部分

BUILD SUCCESS
Finished: SUCCESS


[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 51.149 s
[INFO] Finished at: 2014-06-19T18:35:41+09:00
[INFO] Final Memory: 52M/124M
[INFO] ------------------------------------------------------------------------
[CHECKSTYLE] Collecting checkstyle analysis files...
[CHECKSTYLE] Finding all files that match the pattern **/target/checkstyle-result.xml
[CHECKSTYLE] Parsing 1 files in /root/.jenkins/jobs/sampleproject2_JOB/workspace
[CHECKSTYLE] Successfully parsed file /root/.jenkins/jobs/sampleproject2_JOB/workspace/BUILD/target/checkstyle-result.xml of module  with 0 warnings.
[CHECKSTYLE] Computing warning deltas based on reference build #68
[FINDBUGS] Collecting findbugs analysis files...
[FINDBUGS] Finding all files that match the pattern **/target/findbugs.xml
[FINDBUGS] Parsing 1 files in /root/.jenkins/jobs/sampleproject2_JOB/workspace
[FINDBUGS] Successfully parsed file /root/.jenkins/jobs/sampleproject2_JOB/workspace/BUILD/target/findbugs.xml of module  with 0 warnings.
[FINDBUGS] Computing warning deltas based on reference build #68
[Cobertura] Publishing Cobertura coverage report...
Publishing Cobertura coverage results...
Cobertura coverage report found.
Recording test results
Finished: SUCCESS

ビルドの結果表示(ビルドの画面)

CheckStyleやFindBugsの対応など行なったあとのキャプチャー画面ですが、こんな感じになります。

jenkins_maven_sampleproject2_JOB3.jpg

ビルドの結果表示(JOBのプロジェクト画面)

jenkins_maven_sampleproject2_JOB4.jpg

体裁を整え中です。。。

26
25
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
26
25