Skip to content

Commit 14a9c74

Browse files
authored
Merge pull request #1 from kotlin-hands-on/simplified-pom
update: simplified pom configuration and instructions
2 parents 11076cd + 5256c37 commit 14a9c74

File tree

8 files changed

+69
-222
lines changed

8 files changed

+69
-222
lines changed

.idea/kotlinc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Tutorial: [Test code using JUnit in JVM](https://kotlinlang.org/docs/jvm-test-us
44

55
Configuration guides: [Maven](maven.md) | [Gradle](gradle.md)
66

7-
A sample project demonstrating how to write tests in Kotlin for Java applications. This project shows the migration path from Java tests to Kotlin tests while testing Java source code.
7+
A sample project demonstrating how to write tests in Kotlin for Java applications. This project shows how to add a Kotlin test for testing Java source code.
88

99
## Getting Started
1010

@@ -37,33 +37,34 @@ kotlin-junit-sample/
3737
├── initial/ # Starting point with Java tests
3838
│ ├── src/
3939
│ │ ├── main/java/ # Java source code
40-
│ │ └── test/java/ # JUnit tests in Java
40+
│ │ └── test/java/ # JUnit test in Java
4141
│ ├── pom.xml # Maven configuration
4242
│ └── build.gradle.kts # Gradle configuration
4343
4444
└── complete/ # Final version with Kotlin tests
4545
├── src/
4646
│ ├── main/java/ # Same Java source code
47-
│ └── test/java/ # JUnit tests in Kotlin
47+
│ └── test/java/ # Same JUnit test in Java, new test in Kotlin
4848
├── pom.xml # Maven with Kotlin support
4949
└── build.gradle.kts # Gradle with Kotlin plugin
5050
```
5151

5252
### Application Code
5353

5454
Both projects contain a simple Todo application with:
55-
- **TodoItem** - A Java class representing a todo item with title, description, completion status, and timestamps
56-
- **TodoRepository** - A Java repository class for managing todo items in memory
55+
56+
- **TodoItem** − a Java class representing a todo item with title, description, completion status, and timestamps
57+
- **TodoRepository** − a Java repository class for managing todo items in memory
5758

5859
### Test Code
5960

60-
- **initial/**: Contains comprehensive JUnit 5 tests written in Java
61-
- **complete/**: Same tests converted to Kotlin, demonstrating idiomatic Kotlin testing patterns
61+
- **initial/**: Contains a JUnit 5 test written in Java for the `TodoItem` class
62+
- **complete/**: Contains an additional Kotlin test for the `TodoRepository` class, demonstrating idiomatic Kotlin testing patterns
6263

6364
## Technologies
6465

6566
- **Java**: 17
66-
- **Kotlin**: 2.2.20 (test code only in complete version)
67+
- **Kotlin**: 2.2.21 (test code only in the `complete` version)
6768
- **JUnit**: 5.11.0
6869
- **Build Tools**: Maven and Gradle (both supported)
6970
- **UI Library**: Jexer 1.6.0 (terminal UI library)

complete/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ java.sourceCompatibility = JavaVersion.VERSION_17
55

66
plugins {
77
application
8-
kotlin("jvm") version "2.2.20"
8+
kotlin("jvm") version "2.2.21"
99
}
1010

1111
kotlin {

complete/pom.xml

Lines changed: 12 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1515
<maven.compiler.release>17</maven.compiler.release>
1616
<jexer.version>1.6.0</jexer.version>
17-
<kotlin.version>2.2.20</kotlin.version>
17+
<kotlin.version>2.2.21</kotlin.version>
1818
</properties>
1919

2020
<dependencyManagement>
@@ -99,103 +99,35 @@
9999
<artifactId>maven-project-info-reports-plugin</artifactId>
100100
<version>3.6.1</version>
101101
</plugin>
102-
103-
<!-- KOTLIN -->
104-
<plugin>
105-
<groupId>org.jetbrains.kotlin</groupId>
106-
<artifactId>kotlin-maven-plugin</artifactId>
107-
<version>${kotlin.version}</version>
108-
<extensions>true
109-
</extensions> <!-- You can set this option to automatically take information about lifecycles -->
110-
<executions>
111-
<execution>
112-
<id>compile</id>
113-
<goals>
114-
<goal>compile</goal> <!-- You can skip the <goals> element if you enable extensions for the plugin -->
115-
</goals>
116-
<configuration>
117-
<sourceDirs>
118-
<sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
119-
<sourceDir>${project.basedir}/src/main/java</sourceDir>
120-
</sourceDirs>
121-
</configuration>
122-
</execution>
123-
<execution>
124-
<id>test-compile</id>
125-
<goals>
126-
<goal>test-compile</goal> <!-- You can skip the <goals> element if you enable extensions for the plugin -->
127-
</goals>
128-
<configuration>
129-
<sourceDirs>
130-
<sourceDir>${project.basedir}/src/test/kotlin</sourceDir>
131-
<sourceDir>${project.basedir}/src/test/java</sourceDir>
132-
</sourceDirs>
133-
</configuration>
134-
</execution>
135-
</executions>
136-
</plugin>
137-
<plugin>
138-
<groupId>org.apache.maven.plugins</groupId>
139-
<artifactId>maven-compiler-plugin</artifactId>
140-
<version>3.14.0</version>
141-
<executions>
142-
<!-- Replacing default-compile as it is treated specially by Maven -->
143-
<execution>
144-
<id>default-compile</id>
145-
<phase>none</phase>
146-
</execution>
147-
<!-- Replacing default-testCompile as it is treated specially by Maven -->
148-
<execution>
149-
<id>default-testCompile</id>
150-
<phase>none</phase>
151-
</execution>
152-
<execution>
153-
<id>java-compile</id>
154-
<phase>compile</phase>
155-
<goals>
156-
<goal>compile</goal>
157-
</goals>
158-
</execution>
159-
<execution>
160-
<id>java-test-compile</id>
161-
<phase>test-compile</phase>
162-
<goals>
163-
<goal>testCompile</goal>
164-
</goals>
165-
</execution>
166-
</executions>
167-
</plugin>
102+
<!-- No maven-compiler-plugin needed with Kotlin extensions -->
168103
</plugins>
169104
</pluginManagement>
170105
<plugins>
171-
<!-- Activate Kotlin compiler for main and test sources -->
106+
<!-- Activate Kotlin Maven plugin for main and test sources -->
172107
<plugin>
173108
<groupId>org.jetbrains.kotlin</groupId>
174109
<artifactId>kotlin-maven-plugin</artifactId>
175110
<version>${kotlin.version}</version>
176111
<extensions>true</extensions>
177112
<executions>
178113
<execution>
179-
<id>compile</id>
180-
<goals>
181-
<goal>compile</goal>
182-
</goals>
114+
<id>default-compile</id>
115+
<phase>compile</phase>
183116
<configuration>
184117
<sourceDirs>
185-
<sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
186-
<sourceDir>${project.basedir}/src/main/java</sourceDir>
118+
<sourceDir>src/main/kotlin</sourceDir>
119+
<!-- Ensure Kotlin code can reference Java code -->
120+
<sourceDir>src/main/java</sourceDir>
187121
</sourceDirs>
188122
</configuration>
189123
</execution>
190124
<execution>
191-
<id>test-compile</id>
192-
<goals>
193-
<goal>test-compile</goal>
194-
</goals>
125+
<id>default-test-compile</id>
126+
<phase>test-compile</phase>
195127
<configuration>
196128
<sourceDirs>
197-
<sourceDir>${project.basedir}/src/test/kotlin</sourceDir>
198-
<sourceDir>${project.basedir}/src/test/java</sourceDir>
129+
<sourceDir>src/test/kotlin</sourceDir>
130+
<sourceDir>src/test/java</sourceDir>
199131
</sourceDirs>
200132
</configuration>
201133
</execution>

gradle.md

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
# Mixing Kotlin and Java in a Gradle Project
1+
# Mixing Kotlin and Java in a Gradle project
22

33
This guide shows how to configure a Gradle project to use both Kotlin and Java together.
44

5-
## Step 1: Add Kotlin JVM Plugin
5+
## Step 1: Add Kotlin JVM plugin
66

7-
Add the Kotlin JVM plugin to your `build.gradle.kts`:
7+
Add the Kotlin JVM plugin to your `build.gradle.kts` file:
88

99
```kotlin
1010
plugins {
1111
// ... existing plugins ...
12-
kotlin("jvm") version "2.2.20"
12+
kotlin("jvm") version "2.2.21"
1313
}
1414
```
1515

16-
## Step 2: Configure Kotlin JVM Toolchain
16+
## Step 2: Configure Kotlin JVM toolchain
1717

1818
Set the JVM toolchain version to match your Java version:
1919

@@ -25,7 +25,7 @@ kotlin {
2525

2626
This ensures Kotlin uses the same JDK version as your Java code.
2727

28-
## Step 3: Add Kotlin Test Dependency
28+
## Step 3: Add Kotlin test dependency
2929

3030
Add the Kotlin test library to your dependencies:
3131

@@ -40,15 +40,15 @@ dependencies {
4040

4141
The `kotlin("test")` dependency provides Kotlin's test utilities and integrates with JUnit.
4242

43-
## Step 4: Verify Configuration
43+
## Step 4: Verify configuration
4444

4545
Run your tests to verify the configuration:
4646

4747
```bash
4848
./gradlew clean test
4949
```
5050

51-
## Directory Structure
51+
## Directory structure
5252

5353
With this configuration, you can mix Java and Kotlin files in the same source directories:
5454

@@ -64,12 +64,13 @@ src/
6464

6565
The Kotlin plugin automatically recognizes both `src/main/java` and `src/test/java` directories, so you can place `.kt` files alongside `.java` files in the same directories.
6666

67-
## Summary of Changes
67+
## Summary
6868

69-
1. **Plugin**: Added `kotlin("jvm")` plugin with version 2.2.20
70-
2. **Toolchain**: Configured `jvmToolchain(17)` to match Java version
69+
1. **Plugin**: Added `kotlin("jvm")` plugin with version 2.2.21
70+
2. **Toolchain**: Configured `jvmToolchain(17)` to match the Java version
7171
3. **Dependency**: Added `kotlin("test")` test dependency
7272

7373
The configuration ensures that:
74+
7475
- Kotlin compiler runs before Java compiler
7576
- Both Java and Kotlin code can reference each other

initial/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
</plugin>
6363
<plugin>
6464
<artifactId>maven-compiler-plugin</artifactId>
65-
<version>3.13.0</version>
65+
<version>3.14.0</version>
6666
</plugin>
6767
<plugin>
6868
<artifactId>maven-surefire-plugin</artifactId>

0 commit comments

Comments
 (0)