jbuild 0.1.8

High-performance Java build tool supporting Maven and Gradle
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
# Architecture - jbuild

This document describes the architecture of jbuild, a high-performance Rust implementation supporting both Maven and Gradle build systems.

## Domain-Driven Design (DDD)

jbuild adopts Domain-Driven Design principles to create a maintainable, scalable, and test-friendly architecture. See [docs/DDD_ARCHITECTURE.md](docs/DDD_ARCHITECTURE.md) for detailed DDD documentation.

### Key DDD Principles

- **Bounded Contexts**: Clear boundaries between domain areas (Maven, Gradle, Artifact, etc.)
- **Ubiquitous Language**: Consistent terminology across code and documentation
- **Entities & Value Objects**: Rich domain models with clear identity semantics
- **Aggregates**: Consistency boundaries for domain operations
- **Domain Services**: Business logic that doesn't belong to a single entity
- **Repositories**: Abstraction for data access and persistence
- **Domain Events**: Decoupled communication between contexts

## Overview

jbuild is a single-crate Rust implementation that supports both Maven and Gradle build systems, organized into logical modules under `src/`. The architecture follows the core principles of both build systems while leveraging Rust's type safety and performance.

## Module Structure

See [ORGANIZATION.md](ORGANIZATION.md) for detailed organization documentation.

```
src/
├── lib.rs              # Library root, exports all public APIs
├── main.rs             # CLI dispatcher (minimal entry point)
├── cli.rs              # CLI definition (Clap structs)
│
├── domain/             # Domain layer (DDD architecture)
│   ├── shared/         # Shared domain concepts (value objects, events)
│   ├── build_system/   # Build system detection and abstraction
│   ├── maven/          # Maven bounded context
│   ├── gradle/         # Gradle bounded context
│   ├── artifact/       # Artifact management bounded context
│   ├── compilation/    # Compilation bounded context
│   ├── testing/        # Testing bounded context
│   ├── packaging/      # Packaging bounded context
│   ├── plugin/         # Plugin bounded context
│   ├── config/         # Configuration bounded context
│   └── quality/        # Code quality bounded context
│
├── runner/             # Command implementation logic (Application layer)
│   ├── mod.rs          # Runner utilities
│   ├── cli.rs          # Modularized CLI command implementations
│   └── ...             # Other runner submodules (main_class, maven_central, etc.)
│
├── build/              # Build system abstraction layer (Infrastructure)
│   ├── detection.rs    # Build system detection (Maven vs Gradle)
│   └── executor.rs     # Generic build executor trait
│
├── maven/              # Maven-specific implementation
│   ├── model/          # Maven POM model
│   ├── core/           # Maven execution engine
│   ├── settings/       # Maven settings.xml
│   └── plugin/         # Maven plugins
│
├── gradle/             # Gradle-specific implementation
│   ├── model/          # Gradle build script model
│   ├── core/           # Gradle task execution
│   └── settings.rs     # settings.gradle parsing for multi-project builds
│
├── model/              # Maven POM model (backward compatibility)
│   ├── mod.rs
│   ├── model.rs        # Core Model structure (Maven POM)
│   ├── parent.rs        # Parent POM reference
│   ├── dependency.rs    # Dependency definitions
│   ├── build.rs         # Build configuration
│   ├── repository.rs   # Repository definitions
│   ├── profile.rs       # Build profiles
│   ├── distribution.rs  # Distribution management
│   ├── parser.rs        # POM XML parser with namespace handling
│   ├── model_builder.rs # Effective model construction
│   ├── effective_model.rs # Effective model with parent resolution
│   ├── profile_activator.rs # Profile activation logic
│   ├── interpolation.rs # Property interpolation
│   ├── validator.rs     # Model validation
│   └── gradle.rs        # Gradle build script parsing (in progress)
├── artifact/           # Artifact handling
│   ├── mod.rs
│   ├── artifact.rs     # Artifact representation
│   ├── coordinates.rs   # GAV coordinates
│   ├── handler.rs       # Artifact type handlers
│   └── repository.rs    # Local repository interface
├── core/               # Core execution engine
│   ├── mod.rs
│   ├── execution.rs     # Execution request/result
│   ├── lifecycle.rs     # Lifecycle phases (Maven)
│   ├── project.rs       # MavenProject representation
│   ├── session.rs       # MavenSession management
│   ├── project_builder.rs # Builds projects from POMs
│   ├── lifecycle_starter.rs # Starts lifecycle execution
│   ├── lifecycle_executor.rs # Executes lifecycle phases
│   ├── mojo_executor.rs # Executes plugin mojos
│   ├── reactor.rs       # Multi-module reactor with parallel execution
│   ├── default_maven.rs # Main execution engine
│   ├── graph_builder.rs # Dependency graph construction
│   ├── goal_parser.rs  # Goal parsing and phase mapping
│   ├── cache.rs         # Build cache (incremental compilation)
│   ├── persistent_cache.rs # Persistent build cache (disk storage)
│   ├── optimization.rs # Build optimization (caching, parallel execution)
│   └── unit_of_work.rs # Gradle-inspired UnitOfWork abstraction
├── resolver/           # Dependency resolution
│   ├── mod.rs
│   ├── repository.rs    # Remote repository
│   ├── resolver.rs       # Dependency resolver
│   ├── parallel.rs      # Parallel dependency resolution (rayon-based)
│   ├── metadata.rs      # Repository metadata
│   ├── transitive.rs    # Transitive dependency resolution
│   ├── downloader.rs   # Artifact downloader (HTTP)
│   └── advanced.rs      # Advanced dependency resolution (ranges, conflicts, exclusions)
├── settings/           # Settings management
│   ├── mod.rs
│   ├── settings.rs      # Settings structure
│   ├── profile.rs       # Settings profiles
│   ├── server.rs        # Server configuration
│   ├── mirror.rs        # Repository mirrors
│   └── parser.rs        # Settings.xml parser
├── plugin_api/         # Plugin API
│   ├── mod.rs
│   ├── mojo.rs         # Mojo interface
│   ├── plugin.rs       # Plugin trait
│   ├── descriptor.rs   # Plugin descriptor
│   ├── registry.rs     # Plugin registry and loading
│   └── compatibility.rs # Plugin compatibility and configuration inheritance
├── compiler/           # Compiler integration (Java, Kotlin, Scala)
│   ├── mod.rs
│   ├── java_compiler.rs # Java compiler invocation
│   ├── kotlin.rs        # Kotlin compiler integration
│   ├── scala.rs         # Scala compiler integration
│   ├── annotation_processor.rs # JSR 269 annotation processing
│   ├── classpath.rs     # Classpath management
│   └── source_discovery.rs # Source file discovery
├── packaging/          # JAR/WAR packaging
│   ├── mod.rs
│   ├── jar.rs          # JAR file creation
│   ├── war.rs          # WAR file packaging
│   ├── manifest.rs     # Manifest generation
│   └── resources.rs    # Resource handling
└── testing/            # Test execution
    ├── mod.rs
    ├── discovery.rs    # Test class discovery
    ├── runner.rs       # Test runner
    └── reporting.rs    # Test reporting
```

## Layered Architecture

jbuild follows a layered architecture aligned with DDD principles:

```
┌─────────────────────────────────────────────────┐
│         Presentation Layer (CLI)                │
│  - cli.rs: Command definitions                  │
│  - runner/: Command implementations             │
│  - ui/: User interface utilities                │
└─────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────┐
│         Application Layer                       │
│  - Orchestrates domain services                 │
│  - Use cases (build, test, run, etc.)          │
│  - Transaction boundaries                       │
│  - Located in: runner/, main.rs                 │
└─────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────┐
│         Domain Layer                            │
│  - Entities, Value Objects, Aggregates          │
│  - Domain Services                              │
│  - Domain Events                                │
│  - Business logic and invariants                │
│  - Located in: domain/                          │
└─────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────┐
│         Infrastructure Layer                    │
│  - Repository implementations                   │
│  - External service adapters                    │
│  - File system, HTTP, process execution         │
│  - Located in: artifact/, resolver/, etc.       │
└─────────────────────────────────────────────────┘
```

## Core Components

### Execution Flow

```
CLI (main.rs)
  ↓
BuildSystemDetection (Maven/Gradle)
  ↓
MavenExecutionRequest / GradleExecutionRequest
  ↓
DefaultMaven.execute() / GradleExecutor.execute()
  ↓
ProjectBuilder.build_reactor()
  ↓
Reactor (dependency graph)
  ↓
LifecycleStarter.execute()
  ↓
LifecycleExecutor.execute_to_phase()
  ↓
MojoExecutor.execute()
  ↓
MavenExecutionResult / GradleExecutionResult
```

### Key Design Decisions

1. **Domain-Driven Design**: Explicit bounded contexts, rich domain models, and clear separation of concerns
2. **Single Crate**: All code in one crate for simplicity and faster compilation
3. **Layered Architecture**: Presentation → Application → Domain → Infrastructure
4. **Modular CLI**: Decoupled CLI definition (`src/cli.rs`) from command implementation (`src/runner/cli.rs`), improving maintainability and testability
5. **Consolidated Model Building**: Centralized all POM inheritance, property merging, and interpolation logic into `ModelBuilder`, reducing redundancy
6. **Error Handling**: Uses `anyhow` for application errors, `thiserror` for library errors
7. **Async Support**: `tokio` for I/O operations (future use for parallel builds)
8. **Type Safety**: Strong typing for coordinates, phases, and configurations
9. **Compiler Integration**: Native Rust implementation for Java compiler invocation with classpath management
10. **Dual Build System Support**: Unified artifact and dependency resolution for both Maven and Gradle
11. **Repository Pattern**: All data access through trait-based repository interfaces
12. **Domain Events**: Decoupled communication between bounded contexts

### Data Flow

#### Project Building
1. Detect build system (pom.xml or build.gradle)
2. Parse build file → `Model` (Maven) or `GradleModel` (Gradle)
3. Build effective model (inherit from parent) → `Model`
4. Create `MavenProject` or `GradleProject` with resolved paths
5. Build dependency graph for reactor

#### Dependency Resolution
1. Check local repository (shared between Maven and Gradle)
2. If not found, download from remote repositories via HTTP
3. Resolve transitive dependencies recursively
4. Cache resolved artifacts
5. Store downloaded artifacts in local repository

#### Lifecycle Execution
1. Parse goals → map to lifecycle phases (Maven) or tasks (Gradle)
2. For each phase/task up to target:
   - Get plugin bindings for phase
   - Execute mojos/tasks in order
   - Handle failures appropriately

### Extension Points

- **Plugin API**: Trait-based for future plugin loading
- **Repository**: Trait-based for custom repository implementations
- **Artifact Handler**: Trait-based for custom packaging types
- **Build System**: Trait-based for adding new build system support

## Dependencies

### Core Dependencies
- `serde` / `serde_json` - Serialization
- `quick-xml` - XML parsing (Maven POM)
- `anyhow` / `thiserror` - Error handling
- `clap` - CLI parsing
- `tracing` - Structured logging
- `tokio` - Async runtime
- `reqwest` - HTTP client (for remote repositories)
- `rayon` - Parallel processing for dependency resolution and builds
- `num_cpus` - CPU core detection for parallel execution
- `sha2` - SHA-256 hashing for build cache
- `url` - URL handling
- `zip` - JAR file handling for plugins and packaging
- `walkdir` - File system traversal for source discovery
- `which` - Finding executables (javac, kotlinc, scalac, mvn, java, gradle) in PATH
- `glob` - Pattern matching for resource filtering
- `tree-sitter` / `tree-sitter-java` - Java parsing for Checkstyle
- `jni` - JNI for Java integration (optional feature)

## Bounded Contexts

jbuild is organized into the following bounded contexts:

1. **Build System Context** - Build system detection and abstraction
2. **Maven Context** - Maven-specific implementation
3. **Gradle Context** - Gradle-specific implementation
4. **Artifact Context** - Artifact management and resolution
5. **Compilation Context** - Java source compilation
6. **Testing Context** - Test discovery and execution
7. **Packaging Context** - Creating distributable artifacts
8. **Plugin Context** - Plugin loading and execution
9. **Configuration Context** - Project configuration (jbuild.toml)
10. **Code Quality Context** - Code quality checks

Each bounded context has its own:
- **Entities**: Objects with identity
- **Value Objects**: Immutable objects without identity (✅ Phase 2 complete)
- **Aggregates**: Consistency boundaries
- **Domain Services**: Business logic
- **Repositories**: Data access abstractions

### Implementation Status

**Phase 2: Value Objects (Completed)**
- `ArtifactCoordinates` with validation, GAV parsing, and repository path calculation
-`Version` with semantic comparison and ordering (handles snapshots and qualifiers)
-`Scope` enum for dependency scopes
-`LifecyclePhase` enum with ordering and phase execution logic
-`VersionRange` for dependency version constraints
- ✅ Tests passing (included in total 469)

**Phase 3: Aggregate Roots (Completed)**
- `MavenProject` aggregate root with:
  - Project coordinates, metadata, and configuration
  - Dependencies and plugins as entities within the aggregate
  - Multi-module support with validation
  - Business invariants: no duplicate dependencies/plugins, multi-module must be POM packaging
-`GradleProject` aggregate root with:
  - Project identity (name, group, version)
  - Configurations for dependency management
  - Tasks with dependency graph validation
  - Circular dependency detection
  - Multi-project build support
- ✅ Consistency boundaries enforced at aggregate level
- ✅ Tests passing (included in total 469)

**Phase 4: Domain Services (Completed)**
- `BuildSystemDetector` service for detecting Maven/Gradle/JBuild projects
-`DependencyResolver` service with:
  - Transitive dependency resolution
  - Circular dependency detection
  - Conflict resolution using nearest-wins strategy
  - Scope-based filtering
-`VersionResolver` service for version range resolution
-`LifecycleExecutor` service for Maven with:
  - Phase execution planning
  - Plugin goal binding
  - Custom plugin execution support
-`TaskExecutor` service for Gradle with:
  - Task dependency resolution using topological sort
  - Circular dependency detection
  - Parallel execution planning with execution levels
- ✅ Tests passing (included in total 469)

**Phase 5: Repository Implementations (Completed)**
- `LocalRepository` for local artifact storage (~/.m2/repository)
   - Install and retrieve artifacts
   - List available versions
   - POM file parsing (stub)
-`RemoteRepository` for Maven Central with local caching
   - Cache directory management
   - Artifact URL generation
   - Download stub (ready for HTTP implementation)
-`RepositoryChain` for fallback logic
   - Multiple repository support
   - Automatic fallback on failure
   - Install to first repository
- ✅ Tests passing (included in total 469)

**Phase 6: Application Services (Completed)**
- `BuildOrchestrationService` for build execution
   - Detect build system type
   - Execute Maven phases and goals
   - Execute Gradle tasks
   - Clean build artifacts
-`ProjectInitializationService` for project creation
   - Create Maven projects with pom.xml
   - Create Gradle projects with build.gradle
   - Create JBuild projects with jbuild.toml
   - Generate standard directory structure
   - Create sample Java files
-`DependencyManagementService` for dependency operations
   - Resolve transitive dependencies
   - Get latest versions
   - Add dependencies to projects
- ✅ Tests passing (included in total 469)

See [docs/DDD_ARCHITECTURE.md](docs/DDD_ARCHITECTURE.md) for detailed information on each bounded context.

## Testing & Testability

### Trait-Based Design
The codebase uses trait-based abstractions to enable dependency injection and testing:

- `ProjectBuildStrategy`: Trait for building Maven/Gradle projects
- `LifecycleExecutionStrategy`: Trait for executing lifecycle phases
- `DependencyResolutionStrategy`: Trait for resolving dependencies
- `ArtifactRepository`: Trait for artifact repository operations

### Testing Utilities
Located in `src/testing_utils.rs`:

- `MockArtifactRepository`: In-memory artifact repository for isolated testing
- `MockDependencyResolver`: Mock dependency resolver with configurable behavior
- `TestProjectBuilder`: Fluent builder for creating test projects

### Builder Patterns
Complex objects use fluent builders for easier construction:

- `ExecutionRequestBuilder`: Builds `MavenExecutionRequest` with fluent API

### Error Handling
Custom error types in `src/error.rs` provide:

- Specific error variants for different failure modes
- Better error messages and context
- Conversion from standard library errors

## Gradle-Inspired Patterns

The following patterns were migrated from Gradle's execution engine:

### UnitOfWork Trait
Inspired by Gradle's `UnitOfWork.java`, this abstraction provides:
- **WorkIdentity**: Unique identification for work units
- **InputFingerprint**: Caching and up-to-date checks via input hashing
- **WorkOutput**: Standardized execution results
- **ExecutionContext**: Work execution environment
- **InputVisitor/OutputVisitor**: Patterns for discovering inputs/outputs

### Multi-Project Builds
Settings.gradle support enables:
- **GradleSettings**: Model for settings.gradle parsing
- **SubprojectConfig**: Per-subproject configuration
- **include/includeFlat**: Standard Gradle include statements
- **Multi-project task execution**: Execute tasks across all projects

## Completed Features

1. **Gradle Support**: ✅ Implemented
   - ✅ Gradle build script parsing (Groovy/Kotlin DSL)
   - ✅ Gradle task execution
   - ✅ Gradle dependency resolution
   - ✅ Multi-project builds (settings.gradle)
   - ✅ Version catalogs (libs.versions.toml)
   - ✅ Application plugin with run task
   - ✅ Java toolchains support

2. **Performance Optimizations**: ✅ Implemented
   - ✅ Parallel dependency resolution (rayon-based, 2-5x faster)
   - ✅ Parallel reactor builds (3-5x faster multi-module builds)
   - ✅ Persistent build cache (10-50x faster incremental builds)
   - ✅ Incremental compilation (50-100x faster for single file changes)

3. **Multi-Language Support**: ✅ Implemented
   - ✅ Kotlin compiler integration with plugins (all-open, no-arg, Spring)
   - ✅ Scala compiler integration (2.12, 2.13, 3.x)
   - ✅ Annotation processing (JSR 269: Lombok, MapStruct, Dagger, AutoValue, Immutables)
   - ✅ Mixed-language project support

4. **Code Quality**: ✅ Implemented
   - ✅ Checkstyle integration with tree-sitter Java parser
   - ✅ 9 built-in checks (EmptyCatchBlock, EmptyStatement, MissingSwitchDefault, MultipleVariableDeclarations, SimplifyBooleanReturn, PackageName, TypeName, RedundantImport, LineLength)
   - ✅ XML configuration file support
   - ✅ Test execution (JUnit/TestNG)
   - ✅ Test reporting

5. **Cargo-like Features**: ✅ Implemented
   - ✅ Project scaffolding (jbuild new, jbuild init)
   - ✅ Unified configuration (jbuild.toml, jbuild-workspace.toml, jbuild.lock)
   - ✅ Dependency management (add, remove, update, search, tree, outdated)
   - ✅ Build and run (jbuild run, jbuild watch)
   - ✅ Code formatting (google-java-format integration)
   - ✅ Documentation generation (javadoc)

## Future Architecture Considerations

1. **Plugin System**: Full Java plugin execution
   - JNI integration available (optional `jni` feature) for direct Java class loading
   - External Maven/Gradle process fallback for plugin execution
   - Framework ready for complete Mojo execution implementation

2. **Advanced Features**:
   - Distributed cache support
   - Remote cache server
   - Build analytics and insights
   - Predictive caching
   - IDE integration (LSP)

3. **Additional Languages**:
   - Groovy compiler support
   - Clojure compiler support
   - Ceylon compiler support

4. **Deterministic Snapshots**: Use `IndexMap` for deterministic HashMap ordering in snapshot tests

## Performance Considerations

### Architecture-Level Optimizations
- Single crate reduces compilation time
- Zero-cost abstractions where possible
- Efficient dependency graph algorithms (topological sort, cycle detection)
- Lazy loading of POMs/Gradle files and artifacts
- Rust's performance advantages over Java-based build tools

### Runtime Optimizations
- **Parallel Dependency Resolution**: Rayon-based concurrent resolution (2-5x faster)
- **Parallel Reactor Builds**: Independent modules build concurrently (3-5x faster)
- **Persistent Build Cache**: Disk-based cache with SHA-256 hashing (10-50x faster)
- **Incremental Compilation**: Content-based fingerprinting (50-100x faster)
- **Smart Caching**: Compilation, dependency, and test result caching
- **Batch Processing**: Chunked dependency resolution for better throughput

### Performance Metrics
- **Startup**: ~10ms (vs 500ms Maven, 1000ms Gradle)
- **Memory**: ~50MB (vs 200MB+ Maven, 300MB+ Gradle)
- **Dependency Resolution**: 2-5x faster with parallel resolver
- **Multi-Module Builds**: 3-5x faster with parallel reactor
- **Incremental Builds**: 10-50x faster with persistent cache

## Compatibility

The implementation aims for compatibility with:
- Maven POM 4.0.0 format
- Standard Maven lifecycle
- Maven repository layout
- Maven plugin API (where possible in Rust)
- Gradle build scripts (Groovy/Kotlin DSL) - implemented
- Gradle dependency management - implemented
- Gradle multi-project builds (settings.gradle) - implemented