質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.31%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Gradle

Gradleは、ビルド自動化ツールです。 ソフトウェアパッケージやドキュメント、 または実際に何か他の種類のプロジェクトの構築、テスト、公開、展開などを自動化が出来ます

Q&A

解決済

2回答

1101閲覧

Gradleマルチプロジェクトで「reportOn」メソッドを使わずにテストレポートを1つに集約して出力したい

S-N-G-T-O

総合スコア1

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Gradle

Gradleは、ビルド自動化ツールです。 ソフトウェアパッケージやドキュメント、 または実際に何か他の種類のプロジェクトの構築、テスト、公開、展開などを自動化が出来ます

0グッド

0クリップ

投稿2023/06/21 00:01

編集2023/06/21 04:15

実現したいこと

題名の通り、Gradleのマルチプロジェクトで「reportOn」メソッドを使わずに、サブプロジェクトのテスト結果を集約したテスト結果レポートをルートプロジェクト配下のフォルダに出力したいです。

前提

  • Java17
  • Gradle Wrapper7.5.1

Gradleのマルチプロジェクト構成でJavaのアプリ開発を行っています。
プロジェクト構成は以下の通りです。

root |--- buildSrc |--- batch |--- common |--- webapp |--- webservice

各プロジェクトのbuild.gradleは以下のようになっています。
(関係なさそうなところは省略しています)

root\buildSrc\src\main\groovy\java-common.gradle

groovy

1plugins { 2 id 'java' 3 id 'eclipse' 4 id 'project-report' 5 id 'org.springframework.boot' 6 id 'io.spring.dependency-management' 7} 8 9sourceCompatibility = 17 10targetCompatibility = 17 11 12[compileJava, compileTestJava]*.options*.encoding = "UTF-8" 13 14group = 'com.example' 15 16repositories { 17 mavenCentral() 18} 19 20dependencies { 21 // Spring 22 implementation 'org.springframework.boot:spring-boot-starter-web' 23 testImplementation 'org.springframework.boot:spring-boot-starter-test' 24 25 // JUnit 26 testImplementation 'org.junit.jupiter:junit-jupiter:5.9.1' 27 28 // Mockito 29 testImplementation 'org.mockito:mockito-core:4.8.1' 30 testImplementation 'org.mockito:mockito-inline:4.8.1' 31} 32 33tasks.named('test') { 34 useJUnitPlatform() 35 36 testLogging { 37 showStandardStreams false 38 events 'failed' 39 exceptionFormat 'full' 40 } 41 systemProperty "file.encoding", "UTF-8" 42 jvmArgs = ['--add-opens=java.base/java.util=ALL-UNNAMED'] 43} 44 45test { 46 // Test Result Reports are output from all projects at once. 47 reports.html.required = false 48} 49 50// Create a Test Result Report 51// gradlew testReport 52task testReport(type: TestReport) { 53 destinationDirectory = file("${rootDir}/build/reports/allTests") 54 reportOn files("${rootDir}/batch/build/test-results/test/binary", 55 "${rootDir}/common/build/test-results/test/binary", 56 "${rootDir}/webapp/build/test-results/test/binary", 57 "${rootDir}/webservice/build/test-results/test/binary") 58}

root\batch\build.gradle

groogy

1plugins { 2 id 'java-common' 3} 4 5dependencies { 6 implementation project(":common") 7}

root\common\build.gradle

groovy

1plugins { 2 id 'java-common' 3 id 'java-library' 4} 5 6dependencies { 7 // Spring 8 developmentOnly 'org.springframework.boot:spring-boot-devtools' 9 api 'org.springframework.boot:spring-boot-starter-validation' 10}

root\webapp\build.gradle

groovy

1plugins { 2 id 'java-common' 3} 4 5dependencies { 6 implementation project(":common") 7}

root\webservice\build.gradle

groovy

1plugins { 2 id 'java-common' 3} 4 5dependencies { 6 implementation project(":common") 7}

上記の通り、現在の実装では、「reportOn」メソッドを使用して、サブプロジェクトのテスト結果を1つのテスト結果レポートに集約して出力しています。

しかし、ビルド時に以下の警告メッセージが出るので、今後Gradle Wrapperのバージョンを上げることも検討しているため
警告の対応をしようとしていますがうまくいっていません。

発生している問題・警告メッセージ

The TestReport.reportOn(Object...) method has been deprecated. This is scheduled to be removed in Gradle 8.0. Please use the testResults method instead. See https://docs.gradle.org/7.5.1/dsl/org.gradle.api.tasks.testing.TestReport.html#org.gradle.api.tasks.testing.TestReport:testResults for more details.

試したこと

Gradle's TestReport classの実装を確認して、「testResults」プロパティを変更することができれば、やりたいことが実現できるのではないか?と思いましたが、「testResults」プロパティはReadOnlyなプロパティで変更できませんでした。

警告メッセージ内にある、URLにアクセスして、「binaryResultsDirectory」に値を指定すればよいのか?と思ったので、以下のように設定を一部修正してみましたが、テストレポート自体出力されなくなってしまいました・・・

root\buildSrc\src\main\groovy\java-common.gradle

groovy

1test { 2 // Test Result Reports are output from all projects at once. 3 reports.html.required = false 4 5 // add this property 6 tasks.test.binaryResultsDirectory = file("${rootDir}/build/test-results/test/binary") 7} 8 9// Create a Test Result Report 10// gradlew testReport 11task testReport(type: TestReport) { 12 destinationDirectory = file("${rootDir}/build/reports/allTests") 13 // reportOn files("${rootDir}/batch/build/test-results/test/binary", 14 // "${rootDir}/common/build/test-results/test/binary", 15 // "${rootDir}/webapp/build/test-results/test/binary", 16 // "${rootDir}/webservice/build/test-results/test/binary") 17}

設定方法が誤っていると思われますが、公式のドキュメントを参照してみてもよく分からなかったので、
「こういうの試してみたら?」みたいなのでも良いのでアドバイス頂けたら嬉しいです。
よろしくお願いいたします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

前に別プラグインを使用することで自己解決していましたが、修正前と、テストレポートを出力するコマンド、テストレポートの出力先が変わってしまうことが課題でした。
テストレポートを出力するコマンド、テストレポートの出力先が変わらない自己解決案を見つけたので投稿します。

以下のサイトに書いてある書き方を真似することで、対応可能でした。
https://docs.gradle.org/7.5.1/userguide/java_testing.html#test_reporting

以下build.gradleの設定です。

root\build.gradle

groovy

1plugins { 2 id 'java' 3 id 'eclipse' 4} 5 6configurations { 7 testReportData { 8 canBeResolved = true 9 canBeConsumed = false 10 attributes { 11 attribute(Category.CATEGORY_ATTRIBUTE, objects.named(Category, Category.DOCUMENTATION)) 12 attribute(DocsType.DOCS_TYPE_ATTRIBUTE, objects.named(DocsType, 'test-report-data')) 13 } 14 } 15} 16 17dependencies { 18 testReportData project(':common') 19 testReportData project(':batch') 20 testReportData project(':webapp') 21 testReportData project(':webservice') 22} 23 24tasks.register('testReport', TestReport) { 25 destinationDirectory = reporting.baseDirectory.dir('allTests') 26 testResults.from(configurations.testReportData) 27}

root\buildSrc\src\main\groovy\java-common.gradle

groovy

1plugins { 2 id 'java' 3 id 'eclipse' 4 id 'project-report' 5 id 'org.springframework.boot' 6 id 'io.spring.dependency-management' 7} 8 9sourceCompatibility = 17 10targetCompatibility = 17 11 12[compileJava, compileTestJava]*.options*.encoding = "UTF-8" 13 14group = 'com.example' 15 16repositories { 17 mavenCentral() 18} 19 20configurations { 21 compileOnly { 22 extendsFrom annotationProcessor 23 } 24 testCompileOnly { 25 extendsFrom testAnnotationProcessor 26 } 27} 28 29dependencies { 30 // Spring 31 implementation 'org.springframework.boot:spring-boot-starter-web' 32 implementation 'org.springframework.boot:spring-boot-starter-aop' 33 testImplementation 'org.springframework.boot:spring-boot-starter-test' 34 35 // Apache Commons Lang3 36 implementation "org.apache.commons:commons-lang3:3.12.0" 37 38 // lombok 39 compileOnly 'org.projectlombok:lombok' 40 annotationProcessor 'org.projectlombok:lombok' 41 testCompileOnly 'org.projectlombok:lombok' 42 testAnnotationProcessor 'org.projectlombok:lombok' 43 44 // JUnit 45 testImplementation 'org.junit.jupiter:junit-jupiter:5.9.1' 46 47 // Mockito 48 testImplementation 'org.mockito:mockito-core:4.8.1' 49 testImplementation 'org.mockito:mockito-inline:4.8.1' 50} 51 52tasks.named('test') { 53 useJUnitPlatform() 54 55 testLogging { 56 showStandardStreams false 57 events 'failed' 58 exceptionFormat 'full' 59 } 60 systemProperty "file.encoding", "UTF-8" 61 jvmArgs = ['--add-opens=java.base/java.util=ALL-UNNAMED'] 62} 63 64test { 65 // Test Result Reports are output from all projects at once. 66 reports.html.required = false 67} 68 69// 以下の設定を追加 70configurations { 71 binaryTestResultsElements { 72 canBeResolved = false 73 canBeConsumed = true 74 attributes { 75 attribute(Category.CATEGORY_ATTRIBUTE, objects.named(Category, Category.DOCUMENTATION)) 76 attribute(DocsType.DOCS_TYPE_ATTRIBUTE, objects.named(DocsType, 'test-report-data')) 77 } 78 outgoing.artifact(test.binaryResultsDirectory) 79 } 80} 81 82// このタスク定義は不要となるので削除する 83// Create a Test Result Report 84// gradlew testReport 85// task testReport(type: TestReport) { 86// destinationDirectory = file("${rootDir}/build/reports/allTests") 87// reportOn files("${rootDir}/batch/build/test-results/test/binary", 88// "${rootDir}/common/build/test-results/test/binary", 89// "${rootDir}/webapp/build/test-results/test/binary", 90// "${rootDir}/webservice/build/test-results/test/binary") 91}

投稿2023/08/02 07:05

編集2023/08/02 07:08
S-N-G-T-O

総合スコア1

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

自己解決

The Test Report Aggregation Pluginを使用することで解決できました。

以下プラグインの設定です。

root\build.gradle

groovy

1plugins { 2 id 'java' 3 id 'eclipse' 4 id 'project-report' 5 id 'test-report-aggregation' 6} 7 8dependencies { 9 testReportAggregation project(':common') 10 testReportAggregation project(':batch') 11 testReportAggregation project(':webapp') 12 testReportAggregation project(':webservice') 13} 14 15reporting { 16 reports { 17 testAggregateTestReport(AggregateTestReport) { 18 testType = TestSuiteType.UNIT_TEST 19 } 20 } 21}

root\buildSrc\src\main\groovy\java-common.gradle

groovy

1plugins { 2 id 'java' 3 id 'eclipse' 4 id 'project-report' 5 id 'org.springframework.boot' 6 id 'io.spring.dependency-management' 7} 8 9sourceCompatibility = 17 10targetCompatibility = 17 11 12[compileJava, compileTestJava]*.options*.encoding = "UTF-8" 13 14group = 'com.example' 15 16repositories { 17 mavenCentral() 18} 19 20configurations { 21 compileOnly { 22 extendsFrom annotationProcessor 23 } 24 testCompileOnly { 25 extendsFrom testAnnotationProcessor 26 } 27} 28 29dependencies { 30 // Spring 31 implementation 'org.springframework.boot:spring-boot-starter-web' 32 implementation 'org.springframework.boot:spring-boot-starter-aop' 33 testImplementation 'org.springframework.boot:spring-boot-starter-test' 34 35 // Apache Commons Lang3 36 implementation "org.apache.commons:commons-lang3:3.12.0" 37 38 // lombok 39 compileOnly 'org.projectlombok:lombok' 40 annotationProcessor 'org.projectlombok:lombok' 41 testCompileOnly 'org.projectlombok:lombok' 42 testAnnotationProcessor 'org.projectlombok:lombok' 43 44 // JUnit 45 testImplementation 'org.junit.jupiter:junit-jupiter:5.9.1' 46 47 // Mockito 48 testImplementation 'org.mockito:mockito-core:4.8.1' 49 testImplementation 'org.mockito:mockito-inline:4.8.1' 50} 51 52tasks.named('test') { 53 useJUnitPlatform() 54 55 testLogging { 56 showStandardStreams false 57 events 'failed' 58 exceptionFormat 'full' 59 } 60 systemProperty "file.encoding", "UTF-8" 61 jvmArgs = ['--add-opens=java.base/java.util=ALL-UNNAMED'] 62} 63 64test { 65 // Test Result Reports are output from all projects at once. 66 reports.html.required = false 67} 68 69// このタスク定義は不要となるので削除する 70// Create a Test Result Report 71// gradlew testReport 72// task testReport(type: TestReport) { 73// destinationDirectory = file("${rootDir}/build/reports/allTests") 74// reportOn files("${rootDir}/batch/build/test-results/test/binary", 75// "${rootDir}/common/build/test-results/test/binary", 76// "${rootDir}/webapp/build/test-results/test/binary", 77// "${rootDir}/webservice/build/test-results/test/binary") 78}

build.gradleに上記の設定完了後、以下のコマンドを実行することで、
サブプロジェクトのテスト結果を1つにまとめたレポートをルートプロジェクトのbuildディレクトリ配下に出力することができました。

gradlew testAggregateTestReport

投稿2023/06/25 23:59

S-N-G-T-O

総合スコア1

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.31%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問