Angular, Spring, PostgreSQL(8)Maven?、Gradle?

前回までで、Springの開発・実行環境の整理までしたわけですが、「さぁ開発しよう」とする前に、以前から微妙にすっきりできていない、ビルド関係の話題を整理しておくことにします。

このへんの話題は避けてしまいたかったのだけれど、「ここらで少し整理しといたほうが良いよ」と心が地味~に訴えるので、ちょっとだけね、まとめることにします。

スポンサーリンク

Make

正直、僕はこの世代。といってもバリバリ書いていたわけではなく、誰かに書いてもらって使っていたタイプ(ズルい)。定義ファイル(Makefile)はたとえばこんな感じ。

JAVAC=javac
sources = $(wildcard *.java)
classes = $(sources:.java=.class)

all: myProgram

myProgram: $(classes)

%.class: %.java
    $(JAVAC) $<

jar:    
    @echo "Manifest-Version: 1.0" > manifest.txt
    @echo "Class-Path: ." >> manifest.txt
    @echo "Main-Class: Main" >> manifest.txt
    @echo "" >> manifest.txt

    jar -cmf manifest.txt JARNAME.jar $(classes)

clean:
    rm -f *.class
    rm manifest.txt

実際のプロジェクトでは、もっとファイルが増えるのでごちゃごちゃするけれど(MakeがMakeを呼ぶ、みたいな)個人的にはこれで良かった。難しくなってきたら近くに職人さんがいるので、お願いすれば良かったので。

けれど時代は進む。

スポンサーリンク

Ant

Antのファイル(build.xml)を入手するために、ごく簡単な、JavaプログラムをEclipseで作ってみます。

プロジェクトを右クリックして「エクスポート」。

「Ant ビルドファイル」を選択。

対象のプロジェクト(今回のsample1)を選択

プロジェクト直下に、build.xml が作成されます。

詳細はこんな感じ。

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- WARNING: Eclipse auto-generated file.
              Any modifications will be overwritten.
              To include a user specific buildfile here, simply create one in the same
              directory with the processing instruction <?eclipse.ant.import?>
              as the first entry and export the buildfile again. --><project basedir="." default="build" name="sample1">
    <property environment="env"/>
    <property name="ECLIPSE_HOME" value="../../pleiades/eclipse/"/>
    <property name="debuglevel" value="source,lines,vars"/>
    <property name="target" value="11"/>
    <property name="source" value="11"/>
    <path id="sample1.classpath">
        <pathelement location="bin"/>
    </path>
    <path id="run.Sample1.classpath">
        <path refid="sample1.classpath"/>
    </path>
    <target name="init">
        <mkdir dir="bin"/>
        <copy includeemptydirs="false" todir="bin">
            <fileset dir="src">
                <exclude name="**/*.launch"/>
                <exclude name="**/*.java"/>
            </fileset>
        </copy>
    </target>
    <target name="clean">
        <delete dir="bin"/>
    </target>
    <target depends="clean" name="cleanall"/>
    <target depends="build-subprojects,build-project" name="build"/>
    <target name="build-subprojects"/>
    <target depends="init" name="build-project">
        <echo message="${ant.project.name}: ${ant.file}"/>
        <javac debug="true" debuglevel="${debuglevel}" destdir="bin" includeantruntime="false" source="${source}" target="${target}">
            <src path="src"/>
            <classpath refid="sample1.classpath"/>
        </javac>
    </target>
    <target description="Build all projects which reference this project. Useful to propagate changes." name="build-refprojects"/>
    <target description="copy Eclipse compiler jars to ant lib directory" name="init-eclipse-compiler">
        <copy todir="${ant.library.dir}">
            <fileset dir="${ECLIPSE_HOME}/plugins" includes="org.eclipse.jdt.core_*.jar"/>
        </copy>
        <unzip dest="${ant.library.dir}">
            <patternset includes="jdtCompilerAdapter.jar"/>
            <fileset dir="${ECLIPSE_HOME}/plugins" includes="org.eclipse.jdt.core_*.jar"/>
        </unzip>
    </target>
    <target description="compile project with Eclipse compiler" name="build-eclipse-compiler">
        <property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter"/>
        <antcall target="build"/>
    </target>
    <target name="Sample1">
        <java classname="net.chankazu.Sample1" failonerror="true" fork="yes">
            <classpath refid="run.Sample1.classpath"/>
        </java>
    </target>
</project>

property、target、javac 等々、やりたいことの本質的な部分は、Makeでも出来そうだから、それほど違いはないように見える。

XMLで記載するので、Makeよりはルールが明確みたいだから、そういう「しっかり」感は記述に出るのかな。

ただ、この調子でXMLでずらずらと記述されていくのは、複雑プロジェクトだと読みづらくなりそう。XMLって無駄に長くなるし、条件分岐って出来たっけ?。

また、今時の開発は、Eclipseに代表されるようなIDE(統合開発環境)を利用することがメインだから、外に持っていく(テスト・本番サーバーを構築する)ようなことをする人以外、あんまりAntって、意識する必要ないのかなと思います(違ったらごめん)。

Maven

そして、Antの弱点を見直して出てきたのが「Maven」。しかし中身を見たことがない。じゃあ作ってみるしかない。

今のプロジェクトを右クリックして「構成」を選択すると、「Mavenプロジェクトへ変換」というのがあるので、それを実行してみます。

なんかできたけど(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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>net.chankazu.test</groupId>
  <artifactId>sample1</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.0</version>
        <configuration>
          <release>11</release>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

う~ん、とりあえず実行してみる、というか「Maven Install」というのがあるので、それを先にやってみます。

なんかいろいろ動いた。

どうもこの、「Maven Install」は、コマンドでいうところの「mvn install」と同じことのようで、必要なプラグイン・ライブラリーを、リモート環境「repo.maven.apache.org」からローカル環境にコピーしてくる。

つまり、必要なプラグイン・ライブラリーは随時、pom,xmlに書いておけば、あとはMavenが、リモート環境(これはデフォルトは”repo.maven.apache.org”だが、pomに記載して変更できる)からローカルにコピーしてくる、、ということかな。

※ローカルリポジトリの場所:C:¥Users¥ユーザー名¥. m2¥repository

プラグイン・ライブラリーをどしどし使わないと、良さがいまいち感じられないけれど、Antとの違いはむしろ、この部分なのでしょう。

そこそこのことをやりだせば、書き方は多少易しくなった風ではあるけれど、pom.xmlであっても結構長くなるような気がします。

Gradle

Make>Ant>Mavenときて、やっと Gradleです。

Eclipseで、Gradleプロジェクトの作成が可能。

pluginsとか、dependenciesかの指定が、XMLではなくなっています。
(build.gradle、Groovyという言語で記述)

特に処理をなにも書いてないのですかすか、参考にならない。

/*
 * This file was generated by the Gradle 'init' task.
 *
 * This generated file contains a sample Java Library project to get you started.
 * For more details take a look at the Java Libraries chapter in the Gradle
 * User Manual available at https://docs.gradle.org/6.0/userguide/java_library_plugin.html
 */

plugins {
    // Apply the java-library plugin to add support for Java Library
    id 'java-library'
}

repositories {
    // Use jcenter for resolving dependencies.
    // You can declare any Maven/Ivy/file repository here.
    jcenter()
}

dependencies {
    // This dependency is exported to consumers, that is to say found on their compile classpath.
    api 'org.apache.commons:commons-math3:3.6.1'

    // This dependency is used internally, and not exposed to consumers on their own compile classpath.
    implementation 'com.google.guava:guava:28.0-jre'

    // Use JUnit test framework
    testImplementation 'junit:junit:4.12'
}

下のパネルから、各種処理が実行できるみたい。

これだけでは良さがよくわからない。XMLではなくなったけれど、Groovyという(Javaライクな動的言語)、新たな厄介ごとが増えただけでは?。

Spring関係のプログラムでは、実際にGradleベースで作ってみる予定なので、細かいところは、とりあえず、その時確認したいと思います。

まとめ

雰囲気はなんとな~く理解しました、ぜんぜん完璧ではありませんが。

けど、いいんです、「知らないこと・わからないことは、やりながら調べる・憶える」ということをモットーとして、次回こそ、Springベースのアプリを作ってみます。

コメント

タイトルとURLをコピーしました