Skip to content

Commit ae953cd

Browse files
authored
Fix Java module descriptor configuration (#138)
1 parent 7fbe829 commit ae953cd

File tree

5 files changed

+80
-3
lines changed

5 files changed

+80
-3
lines changed

build.gradle.kts

Lines changed: 38 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,27 @@ 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+
}
113+
}
114+
115+
// Module tests require Java 9 or newer to compile and execute
116+
if (JavaVersion.current().isJava9Compatible) {
117+
tasks.check {
118+
@Suppress("UnstableApiUsage")
119+
dependsOn(testing.suites.named("testModular"))
120+
}
121+
}
122+
101123
tasks.jacocoTestReport {
102124
dependsOn("test")
103125
reports {
@@ -250,6 +272,22 @@ tasks.register<PythonTask>("writeAccuracyTable") {
250272
command = "src/python-scripts/write_accuracy_table.py"
251273
}
252274

275+
tasks.addMainModuleInfo {
276+
version = project.version
277+
// Create Multi-Release JAR with Java 9 as lowest version
278+
jvmVersion.set("9")
279+
// Overwrite the output JAR file (if any) from a previous Gradle execution
280+
overwriteExistingFiles.set(true)
281+
module {
282+
moduleInfoFile = File("$projectDir/src/main/java-9/module-info.java")
283+
}
284+
}
285+
// Workaround to avoid circular dependencies between tasks, see https://github.com/moditect/moditect-gradle-plugin/issues/14
286+
project.afterEvaluate {
287+
val compileJavaTask = tasks.compileJava.get()
288+
compileJavaTask.setDependsOn(compileJavaTask.dependsOn - tasks.addDependenciesModuleInfo.get())
289+
}
290+
253291
tasks.withType<DokkaTask>().configureEach {
254292
dokkaSourceSets.configureEach {
255293
jdkVersion.set(6)

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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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 and that Lingua specifies all required
12+
* modules and can be used successfully.
13+
*/
14+
class LinguaTest {
15+
@Test
16+
void test() {
17+
LanguageDetector detector = LanguageDetectorBuilder.fromLanguages(ENGLISH, FRENCH, GERMAN, SPANISH).build();
18+
Language detectedLanguage = detector.detectLanguageOf("languages are awesome");
19+
assertEquals(ENGLISH, detectedLanguage);
20+
}
21+
}

0 commit comments

Comments
 (0)