eclipseの新規GradleプロジェクトウィザードのSample projectに対する疑問編

 
Gradle!! Gradle!! Gradle!! 

前回はこちら → よろしい。ならばGradleだ。 - 書く。
 
前回の宿題を片付けることに...

Sample projectに対する疑問

日本語ドキュメントはありがたい。
Gradle 日本語ドキュメント

だけど目的のSample projectに対する記述は見つからない。
 
ならばとGoogle検索で

検索:"Sample project" gradle
検索:"Java Quickstart" gradle

としてみたんだけど、こっちも(上から数ページを見る限りだけど)これといったhitはなし。

そもそもSample projectには"Java Quickstart"以外に何があるの?
f:id:nzkh2nr:20131212115036p:plain

ということで、
 
検索:"Java API and Implementation"
 
で発見。
New gradle project wizard · spring-projects/eclipse-integration-gradle Wiki · GitHub

Eclipse Integration for Gradle のほうだったか。

だがしかし

Java Quickstart: A 'single project' sample project from the official Gradle distribution. It is described here.

Java API and Implementation: Another 'single project' sample from the official Gradle distribution. Shows you how to customize your project's layout.

Flat Java Multi Project: A sample we created to demonstrates the most common
'multi-project' layout that works well within Eclipse. There is a single root project that has most of the build logic, and a number of sub-projects containing the actual source code of the project. Many SpringSource projects have Gradle builds and use this layout (e.g. spring-integration, spring-framework, spring-security).

イメージがわかない。とごまかしているけど英語力の問題
ならばeclipseでプロジェクト作って比較したらいいじゃん。

Java API and Implementation

作ったときのコンソール出力がこちら

Download http://repo1.maven.org/maven2/commons-codec/commons-codec/1.5/commons-codec-1.5.pom
Download http://repo1.maven.org/maven2/org/apache/commons/commons-parent/20/commons-parent-20.pom
Download http://repo1.maven.org/maven2/org/apache/apache/9/apache-9.pom
Download http://repo1.maven.org/maven2/commons-lang/commons-lang/2.6/commons-lang-2.6.pom
Download http://repo1.maven.org/maven2/org/apache/commons/commons-parent/17/commons-parent-17.pom
Download http://repo1.maven.org/maven2/org/apache/apache/7/apache-7.pom
Download http://repo1.maven.org/maven2/junit/junit/4.9/junit-4.9.pom
Download http://repo1.maven.org/maven2/org/hamcrest/hamcrest-core/1.1/hamcrest-core-1.1.pom
Download http://repo1.maven.org/maven2/org/hamcrest/hamcrest-parent/1.1/hamcrest-parent-1.1.pom
Download http://repo1.maven.org/maven2/commons-codec/commons-codec/1.5/commons-codec-1.5-sources.jar
Download http://repo1.maven.org/maven2/commons-lang/commons-lang/2.6/commons-lang-2.6-sources.jar
Download http://repo1.maven.org/maven2/org/hamcrest/hamcrest-core/1.1/hamcrest-core-1.1-sources.jar
Download http://repo1.maven.org/maven2/junit/junit/4.9/junit-4.9-sources.jar
Download http://repo1.maven.org/maven2/commons-codec/commons-codec/1.5/commons-codec-1.5.jar
Download http://repo1.maven.org/maven2/commons-lang/commons-lang/2.6/commons-lang-2.6.jar
Download http://repo1.maven.org/maven2/junit/junit/4.9/junit-4.9.jar
Download http://repo1.maven.org/maven2/org/hamcrest/hamcrest-core/1.1/hamcrest-core-1.1.jar

BUILD SUCCESSFUL

デフォルトのbuild.gradleがこちら

apply plugin: "java"
apply plugin: "maven"

group = "myorg"
version = 1.0

repositories {
    mavenCentral()
}

sourceSets.all { set ->
    def jarTask = task("${set.name}Jar", type: Jar) {
        baseName = baseName + "-$set.name"
        from set.output
    }

    artifacts {
        archives jarTask
    }
}

sourceSets {
    api
    impl
}

dependencies {
    apiCompile 'commons-codec:commons-codec:1.5'

    implCompile sourceSets.api.output
    implCompile 'commons-lang:commons-lang:2.6'

    testCompile 'junit:junit:4.9'
    testCompile sourceSets.api.output
    testCompile sourceSets.impl.output
    runtime configurations.apiRuntime
    runtime configurations.implRuntime
}

jar {
    from sourceSets.api.output
    from sourceSets.impl.output
}

uploadArchives {
    repositories {
        mavenDeployer {
            repository(url: uri("${buildDir}/repo"))

            addFilter("main") { artifact, file -> artifact.name == project.name }
            ["api", "impl"].each { type ->
                addFilter(type) { artifact, file -> artifact.name.endsWith("-$type") }
                
                // We now have to map our configurations to the correct maven scope for each pom
                ["compile", "runtime"].each { scope ->
                    configuration = configurations[type + scope.capitalize()]
                    ["main", type].each { pomName ->
                        pom(pomName).scopeMappings.addMapping 1, configuration, scope
                    }
                }
            }

        }
    }
}

srcフォルダはこんな感じ
f:id:nzkh2nr:20131212122826p:plain

"Java Quickstart"のほうが使いやすそうな雰囲気かな。

Flat Java Multi Project

作ったときのコンソール出力がこちら

[sts] -----------------------------------------------------
[sts] Starting Gradle build for the following tasks: 
[sts]      :my-lib:cleanEclipse
[sts]      :product:cleanEclipse
[sts]      :my-lib:eclipse
[sts]      :product:eclipse
[sts] -----------------------------------------------------
:my-lib:cleanEclipseClasspath UP-TO-DATE
:my-lib:cleanEclipseJdt UP-TO-DATE
:my-lib:cleanEclipseProject UP-TO-DATE
:my-lib:cleanEclipse UP-TO-DATE
:product:cleanEclipseClasspath UP-TO-DATE
:product:cleanEclipseJdt UP-TO-DATE
:product:cleanEclipseProject UP-TO-DATE
:product:cleanEclipse UP-TO-DATE
:my-lib:eclipseClasspath
:my-lib:eclipseJdt
:my-lib:eclipseProject
:my-lib:eclipse
:product:eclipseClasspath
:product:eclipseJdt
:product:eclipseProject
:product:eclipse

BUILD SUCCESSFUL

なるほど。プロジェクトが3つできあがった。
・作成したプロジェクト
・my-lib
・product

build.gradleは

subprojects {
    apply plugin: 'java'
    apply plugin: 'eclipse'

    repositories {
       mavenCentral()
    }

    dependencies {
        testCompile 'junit:junit:4.8.2'
    }

    version = '1.0'

    jar {
        manifest.attributes provider: 'my cool company'
    }
}

my-libプロジェクトのbuild.gradleは

dependencies {
	compile group: 'commons-collections', name: 'commons-collections', version: '3.2'
}

productプロジェクトのbuild.gradleは

dependencies {
	compile project(':my-lib')
}

C#でいうソリューションみたいに扱いたいときに使う感じかな?
プロジェクト連携のイメージが少しわいてきた。

結論

とりあえず"Java Quickstart"で。

そして日本語ドキュメントを読む。