@k2works/claude-code-booster 0.2.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,570 @@
1
+ # Java アプリケーション環境構築ガイド
2
+
3
+ ## 概要
4
+
5
+ 本ガイドは、Java アプリケーション開発環境をゼロから構築し、ソフトウェア開発の三種の神器(バージョン管理、テスティング、自動化)を実践するための手順書です。
6
+
7
+ ## 前提条件
8
+
9
+ - Java 17 以降がインストールされていること
10
+ - Git がインストールされていること
11
+ - IntelliJ IDEA または VS Code がインストールされていること(推奨)
12
+
13
+ ## ソフトウェア開発の三種の神器
14
+
15
+ ### 1. バージョン管理
16
+
17
+ #### Git の基本設定
18
+
19
+ ```bash
20
+ # ユーザー設定
21
+ git config --global user.name "Your Name"
22
+ git config --global user.email "your.email@example.com"
23
+
24
+ # リポジトリの初期化
25
+ git init
26
+
27
+ # .gitignore の作成
28
+ echo "build/" >> .gitignore
29
+ echo ".gradle/" >> .gitignore
30
+ echo "*.class" >> .gitignore
31
+ echo ".idea/" >> .gitignore
32
+ echo "*.iml" >> .gitignore
33
+ ```
34
+
35
+ #### コミットメッセージ規約(Angular ルール)
36
+
37
+ ```text
38
+ <タイプ>(<スコープ>): <タイトル>
39
+ <空行>
40
+ <ボディ>
41
+ <空行>
42
+ <フッタ>
43
+ ```
44
+
45
+ **タイプの種類:**
46
+ - `feat`: 新機能の追加
47
+ - `fix`: バグ修正
48
+ - `docs`: ドキュメント変更のみ
49
+ - `style`: コードに影響を与えない変更(フォーマット等)
50
+ - `refactor`: 機能追加でもバグ修正でもないコード変更
51
+ - `perf`: パフォーマンスを改善するコード変更
52
+ - `test`: テストの追加または修正
53
+ - `chore`: ビルドプロセスやツールの変更
54
+
55
+ **例:**
56
+ ```bash
57
+ git commit -m 'feat: FizzBuzz機能の実装'
58
+ git commit -m 'refactor: メソッドの抽出'
59
+ git commit -m 'chore: 静的コード解析セットアップ'
60
+ ```
61
+
62
+ ### 2. テスティング
63
+
64
+ #### JUnit 5 と AssertJ のセットアップ
65
+
66
+ Gradle プロジェクトの初期化:
67
+
68
+ ```bash
69
+ ./gradlew init --type java-application
70
+ ```
71
+
72
+ `build.gradle` の基本設定:
73
+
74
+ ```groovy
75
+ plugins {
76
+ id 'java'
77
+ id 'application'
78
+ }
79
+
80
+ repositories {
81
+ mavenCentral()
82
+ }
83
+
84
+ dependencies {
85
+ // JUnit 5
86
+ testImplementation 'org.junit.jupiter:junit-jupiter-api:5.10.1'
87
+ testImplementation 'org.junit.jupiter:junit-jupiter-params:5.10.1'
88
+ testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.10.1'
89
+
90
+ // AssertJ
91
+ testImplementation 'org.assertj:assertj-core:3.24.2'
92
+ }
93
+
94
+ test {
95
+ useJUnitPlatform()
96
+ }
97
+
98
+ application {
99
+ mainClass = 'App'
100
+ }
101
+ ```
102
+
103
+ #### テストの実行
104
+
105
+ ```bash
106
+ # テストの実行
107
+ ./gradlew test
108
+
109
+ # 継続的テスト実行(ファイル変更監視)
110
+ ./gradlew test --continuous
111
+ ```
112
+
113
+ ### 3. 自動化
114
+
115
+ #### 完全な Gradle セットアップ
116
+
117
+ `build.gradle` の完全版:
118
+
119
+ ```groovy
120
+ plugins {
121
+ id 'java'
122
+ id 'application'
123
+ id 'jacoco'
124
+ id 'checkstyle'
125
+ id 'pmd'
126
+ id 'com.github.spotbugs' version '6.0.7'
127
+ id 'org.gradle.test-retry' version '1.5.6'
128
+ }
129
+
130
+ repositories {
131
+ mavenCentral()
132
+ }
133
+
134
+ dependencies {
135
+ // テスト関連
136
+ testImplementation 'org.junit.jupiter:junit-jupiter-api:5.10.1'
137
+ testImplementation 'org.junit.jupiter:junit-jupiter-params:5.10.1'
138
+ testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.10.1'
139
+ testImplementation 'org.assertj:assertj-core:3.24.2'
140
+
141
+ // SpotBugs関連
142
+ spotbugsPlugins 'com.h3xstream.findsecbugs:findsecbugs-plugin:1.12.0'
143
+ }
144
+
145
+ application {
146
+ mainClass = 'App'
147
+ }
148
+
149
+ test {
150
+ useJUnitPlatform()
151
+
152
+ // テスト失敗時のリトライ設定
153
+ retry {
154
+ maxRetries = 3
155
+ maxFailures = 20
156
+ failOnPassedAfterRetry = false
157
+ }
158
+
159
+ // テストログ設定
160
+ testLogging {
161
+ events "passed", "skipped", "failed"
162
+ exceptionFormat "full"
163
+ }
164
+ }
165
+
166
+ // JaCoCo(コードカバレッジ)設定
167
+ jacoco {
168
+ toolVersion = "0.8.11"
169
+ }
170
+
171
+ jacocoTestReport {
172
+ dependsOn test
173
+ reports {
174
+ xml.required = false
175
+ csv.required = false
176
+ html.outputLocation = layout.buildDirectory.dir('jacocoHtml')
177
+ }
178
+ }
179
+
180
+ // PMD 設定
181
+ pmd {
182
+ consoleOutput = true
183
+ toolVersion = "6.55.0"
184
+ ruleSetFiles = files("config/pmd/ruleset.xml")
185
+ ruleSets = []
186
+ }
187
+
188
+ // カスタムタスク:TDD用の継続的テスト実行
189
+ task tdd(type: Test) {
190
+ useJUnitPlatform()
191
+ testLogging {
192
+ events "passed", "skipped", "failed"
193
+ exceptionFormat "full"
194
+ }
195
+ outputs.upToDateWhen { false }
196
+ }
197
+
198
+ // カスタムタスク:品質チェック全実行
199
+ task qualityCheck {
200
+ dependsOn 'checkstyleMain', 'checkstyleTest', 'pmdMain', 'pmdTest', 'spotbugsMain', 'spotbugsTest'
201
+ description 'Run all quality checks'
202
+ group 'verification'
203
+ }
204
+
205
+ // カスタムタスク:すべてのチェックとテストを実行
206
+ task fullCheck {
207
+ dependsOn 'test', 'qualityCheck', 'jacocoTestReport'
208
+ description 'Run all tests and quality checks'
209
+ group 'verification'
210
+ }
211
+
212
+ // ファイル変更監視タスク
213
+ task watchTest {
214
+ doLast {
215
+ println "Watching for file changes..."
216
+ println "Run: ./gradlew test --continuous"
217
+ println "Or use your IDE's auto-test feature"
218
+ }
219
+ }
220
+ ```
221
+
222
+ ## 静的コード解析ツール
223
+
224
+ ### Checkstyle の設定
225
+
226
+ `config/checkstyle/checkstyle.xml` を作成:
227
+
228
+ ```bash
229
+ mkdir -p config/checkstyle
230
+ ```
231
+
232
+ ```xml
233
+ <?xml version="1.0"?>
234
+ <!DOCTYPE module PUBLIC
235
+ "-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
236
+ "https://checkstyle.org/dtds/configuration_1_3.dtd">
237
+
238
+ <module name="Checker">
239
+ <property name="charset" value="UTF-8"/>
240
+
241
+ <!-- ファイルレベルのチェック -->
242
+ <module name="FileTabCharacter">
243
+ <property name="eachLine" value="true"/>
244
+ </module>
245
+
246
+ <!-- TreeWalkerによるAST解析 -->
247
+ <module name="TreeWalker">
248
+ <!-- インデント -->
249
+ <module name="Indentation">
250
+ <property name="basicOffset" value="4"/>
251
+ </module>
252
+
253
+ <!-- 命名規則 -->
254
+ <module name="TypeName"/>
255
+ <module name="MethodName"/>
256
+ <module name="VariableName"/>
257
+ <module name="ConstantName"/>
258
+
259
+ <!-- その他 -->
260
+ <module name="EmptyStatement"/>
261
+ <module name="EqualsHashCode"/>
262
+ <module name="MagicNumber">
263
+ <property name="ignoreHashCodeMethod" value="true"/>
264
+ <property name="ignoreAnnotation" value="true"/>
265
+ </module>
266
+
267
+ <!-- 循環複雑度のチェック -->
268
+ <module name="CyclomaticComplexity">
269
+ <property name="max" value="7"/>
270
+ <property name="switchBlockAsSingleDecisionPoint" value="false"/>
271
+ </module>
272
+ </module>
273
+ </module>
274
+ ```
275
+
276
+ ### PMD の設定
277
+
278
+ `config/pmd/ruleset.xml` を作成:
279
+
280
+ ```bash
281
+ mkdir -p config/pmd
282
+ ```
283
+
284
+ ```xml
285
+ <?xml version="1.0"?>
286
+ <ruleset name="Custom Rules"
287
+ xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
288
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
289
+ xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0
290
+ https://pmd.sourceforge.io/ruleset_2_0_0.xsd">
291
+
292
+ <description>カスタム PMD ルールセット</description>
293
+
294
+ <!-- 基本ルールセット -->
295
+ <rule ref="category/java/bestpractices.xml">
296
+ <exclude name="JUnitTestContainsTooManyAsserts"/>
297
+ </rule>
298
+
299
+ <rule ref="category/java/codestyle.xml">
300
+ <exclude name="AtLeastOneConstructor"/>
301
+ <exclude name="CommentDefaultAccessModifier"/>
302
+ </rule>
303
+
304
+ <rule ref="category/java/design.xml">
305
+ <!-- 循環複雑度を7に制限 -->
306
+ <exclude name="CyclomaticComplexity"/>
307
+ </rule>
308
+
309
+ <!-- 循環複雑度のカスタム設定 -->
310
+ <rule ref="category/java/design.xml/CyclomaticComplexity">
311
+ <properties>
312
+ <property name="classReportLevel" value="80"/>
313
+ <property name="methodReportLevel" value="7"/>
314
+ <property name="cycloOptions" value=""/>
315
+ </properties>
316
+ </rule>
317
+
318
+ <!-- 認知複雑度の設定 -->
319
+ <rule ref="category/java/design.xml/CognitiveComplexity">
320
+ <properties>
321
+ <property name="reportLevel" value="7"/>
322
+ </properties>
323
+ </rule>
324
+
325
+ <rule ref="category/java/errorprone.xml">
326
+ <exclude name="BeanMembersShouldSerialize"/>
327
+ </rule>
328
+
329
+ <rule ref="category/java/performance.xml"/>
330
+
331
+ <rule ref="category/java/security.xml"/>
332
+ </ruleset>
333
+ ```
334
+
335
+ build.gradle に PMD 設定を追加:
336
+
337
+ ```groovy
338
+ // PMD 設定
339
+ pmd {
340
+ consoleOutput = true
341
+ toolVersion = "6.55.0"
342
+ ruleSetFiles = files("config/pmd/ruleset.xml")
343
+ ruleSets = []
344
+ }
345
+ ```
346
+
347
+ PMD はカスタム設定で以下をチェック:
348
+ - マジックナンバーの使用
349
+ - 空の catch ブロック
350
+ - 循環複雑度が 7 を超えるメソッド
351
+ - 認知複雑度が 7 を超えるメソッド
352
+ - 不要なコード
353
+
354
+ ### SpotBugs の設定(デフォルト設定を使用)
355
+
356
+ SpotBugs は以下のバグパターンを検出:
357
+ - Null ポインタ参照
358
+ - リソースリーク
359
+ - セキュリティ脆弱性
360
+ - パフォーマンス問題
361
+
362
+ ## タスクランナーコマンド一覧
363
+
364
+ ### 基本コマンド
365
+
366
+ ```bash
367
+ # 依存関係のインストール
368
+ ./gradlew build
369
+
370
+ # テストの実行
371
+ ./gradlew test
372
+
373
+ # TDDモードでテスト実行
374
+ ./gradlew tdd
375
+
376
+ # 継続的テスト実行
377
+ ./gradlew test --continuous
378
+ ```
379
+
380
+ ### 品質チェックコマンド
381
+
382
+ ```bash
383
+ # Checkstyle実行
384
+ ./gradlew checkstyleMain
385
+ ./gradlew checkstyleTest
386
+
387
+ # PMD実行
388
+ ./gradlew pmdMain
389
+ ./gradlew pmdTest
390
+
391
+ # SpotBugs実行
392
+ ./gradlew spotbugsMain
393
+ ./gradlew spotbugsTest
394
+
395
+ # すべての品質チェック実行
396
+ ./gradlew qualityCheck
397
+ ```
398
+
399
+ ### カバレッジ測定
400
+
401
+ ```bash
402
+ # テスト実行とカバレッジレポート生成
403
+ ./gradlew test jacocoTestReport
404
+
405
+ # レポートは build/jacocoHtml/index.html で確認
406
+ ```
407
+
408
+ ### 統合コマンド
409
+
410
+ ```bash
411
+ # すべてのテストと品質チェックを実行
412
+ ./gradlew fullCheck
413
+
414
+ # タスク一覧の表示
415
+ ./gradlew tasks --group verification
416
+ ```
417
+
418
+ ## 開発フロー
419
+
420
+ ### 1. プロジェクトの初期化
421
+
422
+ ```bash
423
+ # プロジェクトディレクトリの作成
424
+ mkdir my-java-project
425
+ cd my-java-project
426
+
427
+ # Git リポジトリの初期化
428
+ git init
429
+
430
+ # Gradle プロジェクトの初期化
431
+ ./gradlew init --type java-application
432
+
433
+ # 設定ファイルの配置(上記の build.gradle、checkstyle.xml、PMD ruleset.xml)
434
+
435
+ # 依存関係のインストール
436
+ ./gradlew build
437
+
438
+ # 初期コミット
439
+ git add .
440
+ git commit -m 'chore: プロジェクトの初期化'
441
+ ```
442
+
443
+ ### 2. TDD サイクルの実践
444
+
445
+ ```bash
446
+ # 継続的テスト実行を開始
447
+ ./gradlew test --continuous
448
+
449
+ # 別ターミナルで開発を進める
450
+ # 1. 失敗するテストを書く(Red)
451
+ # 2. テストを通す最小限のコードを書く(Green)
452
+ # 3. リファクタリング(Refactor)
453
+
454
+ # 品質チェック
455
+ ./gradlew qualityCheck
456
+
457
+ # コミット
458
+ git add .
459
+ git commit -m 'feat: 新機能の実装'
460
+ ```
461
+
462
+ ### 3. カバレッジ確認
463
+
464
+ ```bash
465
+ # テストとカバレッジレポート生成
466
+ ./gradlew test jacocoTestReport
467
+
468
+ # ブラウザでレポートを確認
469
+ open build/jacocoHtml/index.html # macOS
470
+ start build/jacocoHtml/index.html # Windows
471
+ ```
472
+
473
+ ## IDE 統合
474
+
475
+ ### IntelliJ IDEA
476
+
477
+ 1. プロジェクトを開く
478
+ 2. `File` → `Settings` → `Build, Execution, Deployment` → `Build Tools` → `Gradle`
479
+ 3. `Build and run using` を `Gradle` に設定
480
+ 4. `Run tests using` を `Gradle` に設定
481
+
482
+ **自動テスト実行の設定:**
483
+ - `Run` → `Edit Configurations`
484
+ - `+` → `Gradle`
485
+ - `Tasks` に `test --continuous` を設定
486
+
487
+ ### VS Code
488
+
489
+ 必要な拡張機能:
490
+ - Extension Pack for Java
491
+ - Gradle for Java
492
+ - Test Runner for Java
493
+
494
+ `settings.json` に追加:
495
+ ```json
496
+ {
497
+ "java.test.runner": "junit",
498
+ "java.test.config": {
499
+ "workingDirectory": "${workspaceFolder}"
500
+ }
501
+ }
502
+ ```
503
+
504
+ ## トラブルシューティング
505
+
506
+ ### Gradle Wrapper が動作しない場合
507
+
508
+ ```bash
509
+ # Gradle Wrapper の再生成
510
+ gradle wrapper --gradle-version=8.5
511
+
512
+ # 実行権限の付与(Unix系)
513
+ chmod +x gradlew
514
+ ```
515
+
516
+ ### テストが見つからない場合
517
+
518
+ - テストクラス名が `*Test.java` で終わっているか確認
519
+ - テストメソッドに `@Test` アノテーションがあるか確認
520
+ - `src/test/java` ディレクトリ配下にテストクラスがあるか確認
521
+
522
+ ### 静的解析ツールのエラーを無視したい場合
523
+
524
+ ```java
525
+ // Checkstyle を無視
526
+ // CHECKSTYLE:OFF
527
+ 問題のあるコード
528
+ // CHECKSTYLE:ON
529
+
530
+ // PMD を無視
531
+ @SuppressWarnings("PMD.MethodNamingConventions")
532
+ public void my_method() { }
533
+
534
+ // SpotBugs を無視
535
+ @SuppressFBWarnings("NP_NULL_ON_SOME_PATH")
536
+ public void method() { }
537
+ ```
538
+
539
+ ## ベストプラクティス
540
+
541
+ 1. **コミットは小さく頻繁に**
542
+ - 機能単位でコミット
543
+ - テストが通る状態でコミット
544
+
545
+ 2. **テストファースト**
546
+ - 実装前にテストを書く
547
+ - テストが失敗することを確認してから実装
548
+
549
+ 3. **継続的な品質チェック**
550
+ - コミット前に `./gradlew qualityCheck` を実行
551
+ - カバレッジ 80% 以上を目標
552
+ - 循環複雑度を 7 以下に維持
553
+
554
+ 4. **自動化の活用**
555
+ - `./gradlew test --continuous` を常に起動
556
+ - IDE の自動テスト機能を活用
557
+
558
+ 5. **リファクタリングの習慣**
559
+ - テストが通ったらリファクタリング
560
+ - 重複コードの排除
561
+ - 意図が明確なコード
562
+
563
+ 6. **複雑度の管理**
564
+ - メソッドの循環複雑度は 7 以下を維持
565
+ - 複雑なロジックは小さなメソッドに分割
566
+ - 早期リターンやガード節を活用して複雑度を削減
567
+
568
+ ## まとめ
569
+
570
+ このガイドに従うことで、Java アプリケーション開発に必要な環境が整い、ソフトウェア開発の三種の神器を実践できます。継続的にテストを書き、品質をチェックし、自動化を活用することで、**動作するきれいなコード** を維持し続けることができます。