0
# Java Projects
1
2
Java projects with Maven build system and dependency management. Provides comprehensive setup for Java applications and libraries with modern Java development tools.
3
4
## Capabilities
5
6
### JavaProject Class
7
8
Main Java project class with Maven build system and integrated Java tooling.
9
10
```typescript { .api }
11
/**
12
* Java project with Maven build system and dependency management
13
* Provides complete Java development environment
14
*/
15
class JavaProject extends GitHubProject {
16
constructor(options: JavaProjectOptions);
17
18
/** Maven POM file management */
19
readonly pom: Pom;
20
/** JUnit testing framework (if enabled) */
21
readonly junit?: Junit;
22
/** Maven packaging configuration */
23
readonly packaging: MavenPackaging;
24
/** Maven compilation setup */
25
readonly compile: MavenCompile;
26
/** Distribution directory */
27
readonly distdir: string;
28
/** Java package name */
29
readonly packageName: string;
30
}
31
32
interface JavaProjectOptions extends GitHubProjectOptions {
33
/** Maven group ID */
34
groupId?: string;
35
/** Maven artifact ID */
36
artifactId?: string;
37
/** Package version */
38
version?: string;
39
/** Main class for applications */
40
mainClass?: string;
41
/** Java package name */
42
packageName?: string;
43
/** Java version (default: "11") */
44
javaVersion?: string;
45
/** Maven packaging type */
46
packaging?: MavenPackaging;
47
/** Distribution directory (default: "dist") */
48
distdir?: string;
49
50
/** Enable JUnit testing */
51
junit?: boolean;
52
/** JUnit version */
53
junitVersion?: string;
54
55
/** Enable sample code generation */
56
sample?: boolean;
57
/** Sample Java class name */
58
sampleJavaPackage?: string;
59
}
60
61
enum MavenPackaging {
62
JAR = "jar",
63
WAR = "war",
64
POM = "pom"
65
}
66
```
67
68
**Basic Java Project Example:**
69
70
```typescript
71
import { JavaProject, MavenPackaging } from "projen";
72
73
const project = new JavaProject({
74
name: "my-java-app",
75
defaultReleaseBranch: "main",
76
77
// Maven configuration
78
groupId: "com.example",
79
artifactId: "my-java-app",
80
version: "1.0.0",
81
mainClass: "com.example.App",
82
83
// Java configuration
84
javaVersion: "17",
85
packaging: MavenPackaging.JAR,
86
87
// Project metadata
88
description: "My awesome Java application",
89
90
// Enable JUnit testing
91
junit: true,
92
junitVersion: "5.9.0",
93
94
// Generate sample code
95
sample: true,
96
sampleJavaPackage: "com.example",
97
});
98
```
99
100
### Pom Class
101
102
Maven POM file management for Java projects.
103
104
```typescript { .api }
105
/**
106
* Maven POM file management for Java projects
107
* Handles dependencies, plugins, and build configuration
108
*/
109
class Pom extends Component {
110
constructor(project: JavaProject, options?: PomOptions);
111
112
/** Maven group ID */
113
readonly groupId: string;
114
/** Maven artifact ID */
115
readonly artifactId: string;
116
/** Project version */
117
readonly version: string;
118
/** Packaging type */
119
readonly packaging: string;
120
121
/** Add runtime dependency */
122
addDependency(groupId: string, artifactId: string, version?: string, options?: MavenDependencyOptions): void;
123
/** Add test dependency */
124
addTestDependency(groupId: string, artifactId: string, version?: string): void;
125
/** Add plugin */
126
addPlugin(groupId: string, artifactId: string, version?: string, options?: MavenPluginOptions): void;
127
/** Add repository */
128
addRepository(id: string, url: string, options?: MavenRepositoryOptions): void;
129
/** Add property */
130
addProperty(key: string, value: string): void;
131
}
132
133
interface PomOptions {
134
/** Maven group ID */
135
groupId?: string;
136
/** Maven artifact ID */
137
artifactId?: string;
138
/** Project version */
139
version?: string;
140
/** Packaging type */
141
packaging?: string;
142
/** Project description */
143
description?: string;
144
/** Project URL */
145
url?: string;
146
}
147
148
interface MavenDependencyOptions {
149
/** Dependency scope */
150
scope?: "compile" | "test" | "runtime" | "provided";
151
/** Dependency type */
152
type?: string;
153
/** Optional dependency */
154
optional?: boolean;
155
/** Exclusions */
156
exclusions?: Array<{
157
groupId: string;
158
artifactId: string;
159
}>;
160
}
161
```
162
163
**Maven Configuration Example:**
164
165
```typescript
166
import { JavaProject } from "projen";
167
168
const project = new JavaProject({
169
name: "spring-boot-app",
170
groupId: "com.example",
171
artifactId: "spring-boot-app",
172
version: "1.0.0",
173
});
174
175
// Add Spring Boot dependencies
176
project.pom.addProperty("spring-boot.version", "3.1.0");
177
178
project.pom.addDependency(
179
"org.springframework.boot",
180
"spring-boot-starter-web",
181
"${spring-boot.version}"
182
);
183
184
project.pom.addDependency(
185
"org.springframework.boot",
186
"spring-boot-starter-data-jpa",
187
"${spring-boot.version}"
188
);
189
190
// Add database driver
191
project.pom.addDependency(
192
"org.postgresql",
193
"postgresql",
194
"42.6.0",
195
{ scope: "runtime" }
196
);
197
198
// Add test dependencies
199
project.pom.addTestDependency(
200
"org.springframework.boot",
201
"spring-boot-starter-test",
202
"${spring-boot.version}"
203
);
204
205
// Add Spring Boot Maven plugin
206
project.pom.addPlugin(
207
"org.springframework.boot",
208
"spring-boot-maven-plugin",
209
"${spring-boot.version}",
210
{
211
executions: [{
212
goals: ["repackage"],
213
}],
214
}
215
);
216
```
217
218
### JUnit Testing
219
220
JUnit testing framework setup for Java projects.
221
222
```typescript { .api }
223
/**
224
* JUnit testing framework for Java projects
225
* Supports JUnit 4 and JUnit 5 (Jupiter)
226
*/
227
class Junit extends Component {
228
constructor(project: JavaProject, options?: JunitOptions);
229
230
/** JUnit version */
231
readonly version: string;
232
/** Test task */
233
readonly testTask: Task;
234
}
235
236
interface JunitOptions {
237
/** JUnit version (4 or 5) */
238
version?: string;
239
/** Include JUnit Jupiter engine for JUnit 5 */
240
includeJupiter?: boolean;
241
}
242
```
243
244
### Maven Compilation
245
246
Maven compilation configuration for Java projects.
247
248
```typescript { .api }
249
/**
250
* Maven compilation configuration
251
* Manages Java compiler settings and source/target versions
252
*/
253
class MavenCompile extends Component {
254
constructor(project: JavaProject, options?: MavenCompileOptions);
255
256
/** Java source version */
257
readonly source: string;
258
/** Java target version */
259
readonly target: string;
260
}
261
262
interface MavenCompileOptions {
263
/** Java source version */
264
source?: string;
265
/** Java target version */
266
target?: string;
267
/** Compiler arguments */
268
compilerArgs?: string[];
269
/** Show deprecation warnings */
270
showDeprecation?: boolean;
271
/** Show unchecked warnings */
272
showUnchecked?: boolean;
273
}
274
```
275
276
### Maven Sample Files
277
278
Generate sample Java code and test files.
279
280
```typescript { .api }
281
/**
282
* Maven sample code generation
283
* Creates example Java classes and test files
284
*/
285
class MavenSample extends Component {
286
constructor(project: JavaProject, options?: MavenSampleOptions);
287
}
288
289
interface MavenSampleOptions {
290
/** Sample package name */
291
packageName?: string;
292
/** Sample class name */
293
className?: string;
294
}
295
```
296
297
**Complete Java Project Example:**
298
299
```typescript
300
import { JavaProject, MavenPackaging } from "projen";
301
302
const project = new JavaProject({
303
name: "enterprise-java-app",
304
defaultReleaseBranch: "main",
305
306
// Maven configuration
307
groupId: "com.enterprise",
308
artifactId: "enterprise-java-app",
309
version: "1.0.0",
310
mainClass: "com.enterprise.Application",
311
packaging: MavenPackaging.JAR,
312
313
// Java version
314
javaVersion: "17",
315
316
// Project metadata
317
description: "Enterprise Java application with Spring Boot",
318
319
// Enable testing
320
junit: true,
321
junitVersion: "5.9.0",
322
323
// Sample code
324
sample: true,
325
sampleJavaPackage: "com.enterprise",
326
});
327
328
// Configure Maven properties
329
project.pom.addProperty("maven.compiler.source", "17");
330
project.pom.addProperty("maven.compiler.target", "17");
331
project.pom.addProperty("spring-boot.version", "3.1.0");
332
project.pom.addProperty("junit.version", "5.9.0");
333
334
// Add Spring Boot dependencies
335
project.pom.addDependency(
336
"org.springframework.boot",
337
"spring-boot-starter-web",
338
"${spring-boot.version}"
339
);
340
341
project.pom.addDependency(
342
"org.springframework.boot",
343
"spring-boot-starter-actuator",
344
"${spring-boot.version}"
345
);
346
347
project.pom.addDependency(
348
"org.springframework.boot",
349
"spring-boot-starter-security",
350
"${spring-boot.version}"
351
);
352
353
// Add utility dependencies
354
project.pom.addDependency("org.apache.commons", "commons-lang3", "3.12.0");
355
project.pom.addDependency("com.fasterxml.jackson.core", "jackson-databind", "2.15.0");
356
357
// Add test dependencies
358
project.pom.addTestDependency(
359
"org.springframework.boot",
360
"spring-boot-starter-test",
361
"${spring-boot.version}"
362
);
363
364
project.pom.addTestDependency("org.testcontainers", "junit-jupiter", "1.18.0");
365
project.pom.addTestDependency("org.testcontainers", "postgresql", "1.18.0");
366
367
// Add Maven plugins
368
project.pom.addPlugin(
369
"org.apache.maven.plugins",
370
"maven-compiler-plugin",
371
"3.11.0",
372
{
373
configuration: {
374
source: "17",
375
target: "17",
376
compilerArgs: ["-parameters"],
377
},
378
}
379
);
380
381
project.pom.addPlugin(
382
"org.springframework.boot",
383
"spring-boot-maven-plugin",
384
"${spring-boot.version}",
385
{
386
executions: [{
387
goals: ["repackage"],
388
}],
389
configuration: {
390
mainClass: "com.enterprise.Application",
391
},
392
}
393
);
394
395
project.pom.addPlugin(
396
"org.apache.maven.plugins",
397
"maven-surefire-plugin",
398
"3.0.0",
399
{
400
configuration: {
401
includes: ["**/*Test.java", "**/*Tests.java"],
402
},
403
}
404
);
405
406
// Add custom tasks
407
project.addTask("run", {
408
description: "Run the application",
409
exec: "mvn spring-boot:run",
410
});
411
412
project.addTask("package", {
413
description: "Package the application",
414
exec: "mvn clean package",
415
});
416
417
project.addTask("test:integration", {
418
description: "Run integration tests",
419
exec: "mvn test -Dtest=**/*IntegrationTest",
420
});
421
```
422
423
## Types
424
425
### Java-Specific Types
426
427
```typescript { .api }
428
interface MavenPluginOptions {
429
/** Plugin configuration */
430
configuration?: Record<string, any>;
431
/** Plugin executions */
432
executions?: Array<{
433
id?: string;
434
phase?: string;
435
goals: string[];
436
configuration?: Record<string, any>;
437
}>;
438
/** Plugin dependencies */
439
dependencies?: Array<{
440
groupId: string;
441
artifactId: string;
442
version: string;
443
}>;
444
}
445
446
interface MavenRepositoryOptions {
447
/** Repository name */
448
name?: string;
449
/** Repository layout */
450
layout?: string;
451
/** Repository releases policy */
452
releases?: {
453
enabled?: boolean;
454
updatePolicy?: "always" | "daily" | "interval" | "never";
455
checksumPolicy?: "fail" | "ignore" | "warn";
456
};
457
/** Repository snapshots policy */
458
snapshots?: {
459
enabled?: boolean;
460
updatePolicy?: "always" | "daily" | "interval" | "never";
461
checksumPolicy?: "fail" | "ignore" | "warn";
462
};
463
}
464
465
interface PomProject {
466
groupId: string;
467
artifactId: string;
468
version: string;
469
packaging?: string;
470
name?: string;
471
description?: string;
472
url?: string;
473
properties?: Record<string, string>;
474
dependencies?: MavenDependency[];
475
plugins?: MavenPlugin[];
476
repositories?: MavenRepository[];
477
}
478
```