2016-07-28 10 views
0

私はuser guideで作業しようとしています。これは私がやりたい信じられないほど単純なもののようですが、私はそれを動作させる方法を理解できません。gradle:グルーヴィーな仕事を書くには

タスクを定義するための外部groovyファイルを作成し、そのタスクをbuild.gradleで呼び出したいとします。私は絶対に持って、この時点で

$ gradle tasks 

FAILURE: Build failed with an exception. 

* Where: 
Build file '/projects/gradle/build.gradle' line: 21 

* What went wrong: 
A problem occurred evaluating root project 'gradle'. 
> Could not get unknown property 'my' for root project 'gradle' of type org.gradle.api.Project. 

* Try: 
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. 

BUILD FAILED 

Total time: 3.193 secs 

package my.package 

import org.gradle.api.DefaultTask 
import org.gradle.api.tasks.TaskAction 

class MyTask extends DefaultTask { 
    String greeting = 'hello from MyTask' 

    @TaskAction 
    def greet(){ 
     println greeting 
    } 
} 

私もタスクをリストすることはできません:私はMyTask.groovyを持ってsrc/main/groovy/my/packageの下で、その後

// Apply the groovy plugin to add support for Groovy 
apply plugin: 'groovy' 

// In this section you declare where to find the dependencies of your project 
repositories { 
    // Use 'jcenter' for resolving your dependencies. 
    // You can declare any Maven/Ivy/file repository here. 
    jcenter() 
} 

// In this section you declare the dependencies for your production and test code 
dependencies { 
    // We use the latest groovy 2.x version for building this library 
    compile 'org.codehaus.groovy:groovy-all:2.4.7' 
    compile gradleApi() 
    compile localGroovy()  
    // We use the awesome Spock testing and specification framework 
    testCompile 'org.spockframework:spock-core:1.0-groovy-2.4' 
    testCompile 'junit:junit:4.12' 
} 

task myTask (type: my.package.MyTask) 

:ここに私のbuild.gradleファイルです何が起こっているのか、私がしたいことをどうやってやるのか分かりません。これは最も単純な可能性のある例のようですが、ユーザーガイドに従うと私はどこにもいません。私が何をしても、私はその例外を乗り越えることはできません。

groovyファイルを作成し、それを使ってビルドファイルに新しいタスクを定義するにはどうすればよいですか?

答えて

1

あなたは下にグルーヴィーなファイルを配置する必要があります:私はbuildSrcの場所を変更しようとしたことはありませんが、未解決であるように思われる https://docs.gradle.org/current/userguide/custom_tasks.html#N14573

EDIT

rootProjectDir/buildSrc/src/main/groovy 

リファレンス号:
https://issues.gradle.org/browse/GRADLE-2816

(私が間違っている場合は、それを修正)

+0

私は今、新しいエラーになったので、ありがとう。 Sidenote:それを変える方法はありますか?私はこれを学びたいので、大きなプロジェクトに貢献することができますが、大きなプロジェクトにはbuildSrcフォルダがありません – ewok

+1

@ewokアップデートを確認してください – CMPS

1

今、あなたはあなたのsrcがBUILDSRCに移動持っていることを、あなたもMyTask.groovyの小さなタイプミスを修正する必要があります:

代わりの

それあなたの @TaskAction注釈を修正するべき
import org.gradle.api.tasks.DefaultTask 

使用

import org.gradle.api.tasks.TaskAction 

+0

ありがとうございます。それに気づき、修正しました。 – ewok

関連する問題