Skip to content

Commit 7fc2ddf

Browse files
committed
Fix Java module descriptor configuration
1 parent 7fbe829 commit 7fc2ddf

File tree

5 files changed

+91
-3
lines changed

5 files changed

+91
-3
lines changed

build.gradle.kts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ plugins {
5757
id("com.asarkar.gradle.build-time-tracker") version "3.0.1"
5858
id("org.jetbrains.dokka") version "1.6.21"
5959
id("ru.vyarus.use-python") version "2.3.0"
60+
id("org.moditect.gradleplugin") version "1.0.0-rc3"
6061
id("com.github.johnrengelman.shadow") version "7.1.2"
6162
id("io.github.gradle-nexus.publish-plugin") version "1.1.0"
6263
`maven-publish`
@@ -98,6 +99,39 @@ tasks.test {
9899
maxParallelForks = 1
99100
}
100101

102+
// Suppress warnings about incubating test suites feature
103+
@Suppress("UnstableApiUsage")
104+
testing {
105+
suites {
106+
// Separate test suite for module testing
107+
val testModular by registering(JvmTestSuite::class) {
108+
dependencies {
109+
implementation(project)
110+
}
111+
112+
targets {
113+
all {
114+
testTask.configure {
115+
useJUnitPlatform()
116+
117+
// Explicitly set the Java version to use when running this test suite
118+
javaLauncher.set(
119+
javaToolchains.launcherFor {
120+
languageVersion.set(JavaLanguageVersion.of(11))
121+
}
122+
)
123+
}
124+
}
125+
}
126+
}
127+
}
128+
}
129+
130+
tasks.check {
131+
@Suppress("UnstableApiUsage")
132+
dependsOn(testing.suites.named("testModular"))
133+
}
134+
101135
tasks.jacocoTestReport {
102136
dependsOn("test")
103137
reports {
@@ -390,3 +424,19 @@ signing {
390424
repositories {
391425
mavenCentral()
392426
}
427+
428+
tasks.addMainModuleInfo {
429+
version = project.version
430+
// Create Multi-Release JAR with Java 9 as lowest version
431+
jvmVersion.set("9")
432+
// Overwrite the existing JAR file with the modular one
433+
overwriteExistingFiles.set(true)
434+
module {
435+
moduleInfoFile = File("$projectDir/src/main/java-9/module-info.java")
436+
}
437+
}
438+
// Workaround to avoid circular dependencies between tasks, see https://github.com/moditect/moditect-gradle-plugin/issues/14
439+
project.afterEvaluate {
440+
val compileJavaTask = tasks.compileJava.get()
441+
compileJavaTask.setDependsOn(compileJavaTask.dependsOn - tasks.addDependenciesModuleInfo.get())
442+
}

src/main/java-9/module-info.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module com.github.pemistahl.lingua {
2+
exports com.github.pemistahl.lingua.api;
3+
4+
requires kotlin.stdlib;
5+
6+
requires it.unimi.dsi.fastutil;
7+
requires okio;
8+
9+
// Moshi accesses JSON serializer using reflection; must open the package
10+
// TODO: Once new Moshi version has module names, change it to `opens ... to com.squareup.moshi`
11+
// and comment in the `requires` declarations below
12+
opens com.github.pemistahl.lingua.internal;
13+
// requires com.squareup.moshi;
14+
// requires com.squareup.moshi.kotlin;
15+
}

src/main/resources/META-INF/versions/9/module-info.java

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/testModular/java/module-info.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Open complete module for reflection to allow JUnit to access the packages
2+
open module test {
3+
requires com.github.pemistahl.lingua;
4+
5+
requires org.junit.jupiter.api;
6+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package test;
2+
3+
import com.github.pemistahl.lingua.api.*;
4+
import org.junit.jupiter.api.Test;
5+
6+
import static com.github.pemistahl.lingua.api.Language.*;
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
9+
/**
10+
* Tests basic Lingua functionality. The main purpose of this test is to verify that the
11+
* packages can be accessed from a different module.
12+
*/
13+
class LinguaTest {
14+
@Test
15+
void test() {
16+
LanguageDetector detector = LanguageDetectorBuilder.fromLanguages(ENGLISH, FRENCH, GERMAN, SPANISH).build();
17+
Language detectedLanguage = detector.detectLanguageOf("languages are awesome");
18+
assertEquals(ENGLISH, detectedLanguage);
19+
}
20+
}

0 commit comments

Comments
 (0)