Skip to content

Commit 324702a

Browse files
authored
Add Scala example project (#246)
Also moved Kotlin/Scala to subprojects rather than applying to all.
1 parent 158acaf commit 324702a

File tree

15 files changed

+93
-50
lines changed

15 files changed

+93
-50
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[![build](https://github.com/will-molloy/java-template/actions/workflows/build.yml/badge.svg?branch=main&event=push)](https://github.com/will-molloy/java-template/actions/workflows/build.yml)
44
[![codecov](https://codecov.io/gh/will-molloy/java-template/branch/main/graph/badge.svg)](https://codecov.io/gh/will-molloy/java-template)
55

6-
template repo for Java/Kotlin Gradle projects
6+
template repo for Java (or Kotlin/Scala) Gradle projects
77

88
## Features
99

@@ -13,7 +13,7 @@ template repo for Java/Kotlin Gradle projects
1313
- Automatic code formatting via [Spotless](https://github.com/diffplug/spotless)
1414
- Java: [`google-java-format`](https://github.com/google/google-java-format)
1515
- Kotlin: [`ktfmt`](https://github.com/facebook/ktfmt)
16-
- Kotlin Gradle: [`ktlint`](https://github.com/pinterest/ktlint)
16+
- Scala: [`scalafmt`](https://github.com/scalameta/scalafmt)
1717
- Code style analysis via [Checkstyle](https://github.com/checkstyle/checkstyle)
1818
- Static analysis via [SpotBugs](https://spotbugs.github.io/)
1919
- Unit and integration test support via [JUnit 5](https://junit.org/junit5/) and [TestSets plugin](https://github.com/unbroken-dome/gradle-testsets-plugin)

build.gradle.kts

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@ import com.github.spotbugs.snom.SpotBugsExtension
55
import com.github.spotbugs.snom.SpotBugsTask
66
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
77
import org.gradle.api.tasks.testing.logging.TestLogEvent
8-
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension
98

109
logger.quiet("Java version: ${JavaVersion.current()}")
1110
logger.quiet("Gradle version: ${gradle.gradleVersion}")
1211

1312
plugins {
1413
id("java-library")
15-
kotlin("jvm") version libs.versions.kotlin
1614
alias(libs.plugins.spotless)
1715
alias(libs.plugins.spotbugs)
1816
alias(libs.plugins.buildtimetracker)
@@ -30,11 +28,6 @@ allprojects {
3028
targetCompatibility = JavaVersion.VERSION_21
3129
}
3230

33-
apply(plugin = "kotlin")
34-
configure<KotlinJvmProjectExtension> {
35-
jvmToolchain(21)
36-
}
37-
3831
apply(plugin = "com.diffplug.spotless")
3932
configure<SpotlessExtension> {
4033
// https://github.com/diffplug/spotless/tree/main/plugin-gradle#java
@@ -55,9 +48,15 @@ allprojects {
5548
trimTrailingWhitespace()
5649
endWithNewline()
5750
}
51+
// https://github.com/diffplug/spotless/tree/main/plugin-gradle#scala
52+
scala {
53+
scalafmt().configFile("$rootDir/scalafmt.conf")
54+
trimTrailingWhitespace()
55+
endWithNewline()
56+
}
5857
}
5958

60-
// TODO Kotlin alternative?
59+
// TODO Kotlin/Scala alternative?
6160
apply(plugin = "checkstyle")
6261
configure<CheckstyleExtension> {
6362
toolVersion = "10.12.0"
@@ -112,23 +111,8 @@ allprojects {
112111
}
113112
}
114113

115-
val previewFeatures = emptyList<String>()
116-
tasks.withType<JavaCompile> {
117-
options.compilerArgs = previewFeatures
118-
}
119-
tasks.withType<Test> {
120-
jvmArgs = previewFeatures
121-
}
122-
tasks.withType<JavaExec> {
123-
jvmArgs = previewFeatures
124-
}
125-
126114
dependencies {
127-
implementation(rootProject.libs.log4j.core)
128-
implementation(rootProject.libs.log4j.api)
129-
implementation(rootProject.libs.log4j.slf4j2)
130115
implementation(rootProject.libs.spotbugs.annotations)
131-
implementation(rootProject.libs.guava)
132116

133117
testImplementation(rootProject.libs.junit)
134118
testImplementation(rootProject.libs.truth)
@@ -138,10 +122,6 @@ allprojects {
138122
configurations.all {
139123
exclude(group = "org.assertj")
140124
exclude(group = "junit")
141-
resolutionStrategy {
142-
// exclude android version
143-
force("com.google.guava:guava:${rootProject.libs.versions.guava.get()}")
144-
}
145125
}
146126
}
147127
}

example-java/build.gradle.kts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1-
plugins { alias(libs.plugins.testsets) }
1+
plugins {
2+
alias(libs.plugins.testsets)
3+
}
24

3-
testSets { create("integrationTest") }
5+
testSets {
6+
create("integrationTest")
7+
}
8+
9+
dependencies {
10+
implementation(rootProject.libs.log4j.core)
11+
implementation(rootProject.libs.log4j.api)
12+
implementation(rootProject.libs.log4j.slf4j2)
13+
}

example-java/src/integrationTest/java/com/willmolloy/HelloWorldIntegrationTest.java renamed to example-java/src/integrationTest/java/com/willmolloy/HelloJavaIntegrationTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
import org.junit.jupiter.api.Test;
66

77
/**
8-
* Integration tests for {@link HelloWorld}.
8+
* Integration tests for {@link HelloJava}.
99
*
1010
* @author <a href=https://willmolloy.com>Will Molloy</a>
1111
*/
12-
class HelloWorldIntegrationTest {
12+
class HelloJavaIntegrationTest {
1313

1414
@Test
1515
void test_hello() {
16-
assertThat(new HelloWorld().hello("world")).isEqualTo("Hello world!");
16+
assertThat(new HelloJava().hello("world")).isEqualTo("Hello world!");
1717
}
1818
}

example-java/src/main/java/com/willmolloy/HelloWorld.java renamed to example-java/src/main/java/com/willmolloy/HelloJava.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
* @author <a href=https://willmolloy.com>Will Molloy</a>
1010
*/
11-
class HelloWorld {
11+
class HelloJava {
1212
private static final Logger log = LogManager.getLogger();
1313

1414
String hello(String text) {

example-java/src/test/java/com/willmolloy/HelloWorldTest.java renamed to example-java/src/test/java/com/willmolloy/HelloJavaTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
import org.junit.jupiter.api.Test;
66

77
/**
8-
* Unit tests for {@link HelloWorld}.
8+
* Unit tests for {@link HelloJava}.
99
*
1010
* @author <a href=https://willmolloy.com>Will Molloy</a>
1111
*/
12-
class HelloWorldTest {
12+
class HelloJavaTest {
1313

1414
@Test
1515
void test_hello() {
16-
assertThat(new HelloWorld().hello("world")).isEqualTo("Hello world!");
16+
assertThat(new HelloJava().hello("world")).isEqualTo("Hello world!");
1717
}
1818
}

example-kotlin/build.gradle.kts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,12 @@
1+
plugins {
2+
kotlin("jvm") version libs.versions.kotlin
3+
}
14

5+
kotlin {
6+
jvmToolchain(21)
7+
}
8+
9+
// TODO disabling on Kotlin/Scala atm... too many false positives
10+
spotbugs {
11+
ignoreFailures.set(true)
12+
}

example-kotlin/src/main/kotlin/com/willmolloy/HelloWorld.kt renamed to example-kotlin/src/main/kotlin/com/willmolloy/HelloKotlin.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ package com.willmolloy
55
*
66
* @author <a href=https://willmolloy.com>Will Molloy</a>
77
*/
8-
class HelloWorld {
8+
object HelloKotlin {
99

10-
fun hello(text: String): String {
11-
return "Hello $text!"
12-
}
10+
fun hello(text: String): String = "Hello $text, from Kotlin!"
1311
}

example-kotlin/src/test/kotlin/com/willmolloy/HelloWorldTest.kt renamed to example-kotlin/src/test/kotlin/com/willmolloy/HelloKotlinTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ package com.willmolloy
33
import com.google.common.truth.Truth.assertThat
44
import org.junit.jupiter.api.Test
55

6-
/** Unit tests for [HelloWorld]. */
7-
class HelloWorldTest {
6+
/** Unit tests for [HelloKotlin]. */
7+
class HelloKotlinTest {
88

99
@Test
1010
fun `test hello`() {
11-
assertThat(HelloWorld().hello("world")).isEqualTo("Hello world!")
11+
assertThat(HelloKotlin.hello("world")).isEqualTo("Hello world, from Kotlin!")
1212
}
1313
}

example-scala/build.gradle.kts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
plugins {
2+
scala
3+
}
4+
5+
dependencies {
6+
implementation(libs.scala.library)
7+
}
8+
9+
// TODO disabling on Kotlin/Scala atm... too many false positives
10+
spotbugs {
11+
ignoreFailures.set(true)
12+
}

0 commit comments

Comments
 (0)