2016-09-05 7 views
0

2つのクラスAとBのBをインポートするgradle(1.8)プロジェクトがあります。どちらのファイルも/foo/bar/パッケージ(myProjectFolder/test/foo/barフォルダ)にあります。タスクを実行しているときにGradleがGroovyクラスをインポートしない

A.groovyクラス

package foo.bar 

import org.junit.Test; 

public class ATest{ 
    @Test 
    public void run() { 
     System.out.println("CLASS A"); 
    } 
} 

B.groovyクラス

package foo.bar 

import org.junit.Test; 

public class BTest{ 
    @Test 
    public void run() { 
     ATest a = new ATest(); 
     a.run(); 
     System.out.println("CLASS B"); 
    } 
} 

build.gradle

apply plugin: "groovy" 
apply plugin: "java" 

repositories { 
     mavenCentral() 
} 

dependencies { 
     groovy group: "org.codehaus.groovy", name:"groovy-all", version: "1.8.6" 
     testCompile "junit:junit:4.10" 
} 

test { 
     testLogging.showStandardStreams = true 
} 

sourceSets { 
     test { groovy { 
       srcDir 'test/foo/bar' 
     } } 
} 

buildscript { 
     repositories { mavenCentral() } 
} 

configurations{ 
    addToClassLoader 
} 

dependencies { 
    addToClassLoader "junit:junit:4.10" 
} 

URLClassLoader loader = GroovyObject.class.classLoader 
configurations.addToClassLoader.each {File file -> 

loader.addURL(file.toURL()) 
} 

task runA << { 
    new GroovyShell().run(file('test/foo/bar/ATest.groovy')); 
} 

task runB << { 
    new GroovyShell().run(file('test/foo/bar/BTest.groovy')); 
} 

出力gradle -q runB -i<ためgradle -q runA -i

[[email protected]]# gradle -q runA -i 
Starting Build 
Settings evaluated using empty settings script. 
Projects loaded. Root project using build file '/opt/myProject/build.gradle'. 
Included projects: [root project 'myProject'] 
Evaluating root project 'myProject' using build file '/opt/myProject/build.gradle'. 
Starting file lock listener thread. 
The groovy configuration has been deprecated and is scheduled to be removed in Gradle 2.0. Typically, usages of 'groovy' can simply be replaced with 'compile'. In some cases, it may be necessary to additionally configure the 'groovyClasspath' property of GroovyCompile and Groovydoc tasks. 
All projects evaluated. 
Selected primary task 'runA' 
Tasks to be executed: [task ':runA'] 
:runA (Thread[main,5,main]) started. 
:runA 
Executing task ':runA' (up-to-date check took 0.001 secs) due to: 
    Task has not declared any outputs. 
CLASS A 
JUnit 4 Runner, Tests: 1, Failures: 0, Time: 63 
:runA (Thread[main,5,main]) completed. Took 0.739 secs. 
BUILD SUCCESSFUL 
Total time: 7.826 secs 

出力コンソール用gradle clean -Dtest.single=B test

[[email protected]]# gradle clean -Dtest.single=B test 
The groovy configuration has been deprecated and is scheduled to be removed in Gradle 2.0. Typically, usages of 'groovy' can simply be replaced with 'compile'. In some cases, it may be necessary to additionally configure the 'groovyClasspath' property of GroovyCompile and Groovydoc tasks. 
:clean 
:compileJava UP-TO-DATE 
:compileGroovy UP-TO-DATE 
:processResources 
:classes 
:compileTestJava UP-TO-DATE 
:compileTestGroovy 
:processTestResources UP-TO-DATE 
:testClasses 
:test 
foo.bar.BTest > run STANDARD_OUT 
    CLASS A 
    CLASS B 
BUILD SUCCESSFUL 
Total time: 12.218 secs 

出力コンソール用gradle clean -Dtest.single=A test

[[email protected]]# gradle clean -Dtest.single=A test 
The groovy configuration has been deprecated and is scheduled to be removed in Gradle 2.0. Typically, usages of 'groovy' can simply be replaced with 'compile'. In some cases, it may be necessary to additionally configure the 'groovyClasspath' property of GroovyCompile and Groovydoc tasks. 
:clean 
:compileJava UP-TO-DATE 
:compileGroovy UP-TO-DATE 
:processResources 
:classes 
:compileTestJava UP-TO-DATE 
:compileTestGroovy 
:processTestResources UP-TO-DATE 
:testClasses 
:test 
foo.bar.ATest > run STANDARD_OUT 
    CLASS A 
BUILD SUCCESSFUL 
Total time: 13.205 secs 

出力コンソールのコンソール - この実行はを失敗し、私にはわかりませんなぜ。

[[email protected]]# gradle -q runB -i 
Starting Build 
Settings evaluated using empty settings script. 
Projects loaded. Root project using build file '/opt/myProject/build.gradle'. 
Included projects: [root project 'myProject'] 
Evaluating root project 'myProject' using build file '/opt/myProject/build.gradle'. 
Starting file lock listener thread. 
The groovy configuration has been deprecated and is scheduled to be removed in Gradle 2.0. Typically, usages of 'groovy' can simply be replaced with 'compile'. In some cases, it may be necessary to additionally configure the 'groovyClasspath' property of GroovyCompile and Groovydoc tasks. 
All projects evaluated. 
Selected primary task 'runB' 
Tasks to be executed: [task ':runB'] 
:runB (Thread[main,5,main]) started. 
:runB 
Executing task ':runB' (up-to-date check took 0.001 secs) due to: 
    Task has not declared any outputs. 
:runB FAILED 
:runB (Thread[main,5,main]) completed. Took 0.237 secs. 

FAILURE: Build failed with an exception. 
* Where: 
Build file '/opt/myProject/build.gradle' line: 46 
* What went wrong: 
Execution failed for task ':runB'. 
> startup failed: 
    /opt/myProject/test/foo/bar/BTest.groovy: 8: unable to resolve class ATest 
    @ line 8, column 9. 
       ATest a = new ATest(); 
      ^
    /opt/myProject/test/foo/bar/BTest.groovy: 8: unable to resolve class ATest 
    @ line 8, column 13. 
       ATest a = new ATest(); 
       ^
    2 errors 
* Try: 
Run with --stacktrace option to get the stack trace. Run with --debug option to get more log output. 
BUILD FAILED 
Total time: 7.872 secs 

タスクrunBを正しく実行するには何が欠けていますか?

+1

あなたは何をしようとしていますか?なぜあなたはテスト*を手で*しようとしていますか、そしてノーマルとは別の道で?なぜ 'src/test/groovy'にそれらを貼り付けて、すべてのソースセットのものとあなたの余分なタスクとクラスローダーのものを取り除き、' gradle test'を実行してみてください...あなたは、殴られた道ですが、私はそれらを見ることができませんし、それが正しいことを確認せずに行う方法を説明することは愚かなようです –

答えて

0

注:質問の前提は真に非正統的なユースケースに基づいており、修正する必要があります。つまり、私は観察された行動を説明することができます。

コマンドラインでtestタスクを指定すると、~/build/classes/testがクラスパスに追加されます。 runBタスクを実行している場合、タスクは実行されていません。だからBTest.groovyATestが見つかりません。

は(説明のみ、「本当の」コードのための提案として ないとして を)それを修正するには、考慮してください。

// don't do this in a real project! 
task runB(dependsOn: 'compileTestGroovy') << { 
    def testDirURL = new File("${projectDir}/build/classes/test").toURL() 
    loader.addURL(testDirURL) 
    new GroovyShell(loader).run(file('test/foo/bar/BTest.groovy')) 
} 

さて、runBはGroovyのテストコードがコンパイルされていることを必要とし、それを手動で(!?)GroovyShellで使用されるクラスローダーに追加します。

関連する問題