Skip to content

Commit

Permalink
Add 'Module Info DSL' examples
Browse files Browse the repository at this point in the history
  • Loading branch information
jjohannes committed Jul 6, 2023
1 parent f7dbb4e commit 91468c0
Show file tree
Hide file tree
Showing 54 changed files with 568 additions and 0 deletions.
24 changes: 24 additions & 0 deletions samples/module-info-dsl-no-platform/app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
plugins {
id("org.example.java-module-app")
}

application {
applicationDefaultJvmArgs = listOf("-ea")
mainClass.set("org.example.app.App")
mainModule.set("org.example.app")
}

mainModuleInfo {
runtimeOnly("org.slf4j.simple")
}

testModuleInfo {
requires("org.junit.jupiter.api")
}

moduleInfo {
version("org.slf4j", "2.0.7")
version("org.slf4j.simple", "2.0.7")
version("com.fasterxml.jackson.databind", "2.14.0")
version("org.junit.jupiter.api", "5.9.3")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module org.example.app {
requires org.example.lib;
requires org.slf4j;

exports org.example.app;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.example.app;

import org.example.lib.Lib;
import org.slf4j.LoggerFactory;

public class App {

public static void main(String[] args) {
LoggerFactory.getLogger(App.class).info("App running...");
doWork();
}

public static void doWork() {
new Lib();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.example.app;

import org.example.lib.Lib;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.InputStream;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

public class AppTest {

@Test
public void testApp() throws IOException {
assertEquals("org.example.lib", Lib.class.getModule().getName());
assertEquals("org.example.app", App.class.getModule().getName());
assertEquals("org.example.app", AppTest.class.getModule().getName());
try (InputStream is = AppTest.class.getResourceAsStream("/data.txt")) {
assertNotNull(is);
assertEquals("ABC", new String(is.readAllBytes(), UTF_8));
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ABC
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
open module org.example.app.test.functional {
requires org.example.app;
requires org.junit.jupiter.api;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.example.app.test;

import org.junit.jupiter.api.Test;
import org.example.app.App;

import java.io.IOException;
import java.io.InputStream;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

public class AppFunctionalTest {

@Test
void testAppFunctional() throws IOException {
assertEquals("org.example.app", App.class.getModule().getName());
assertEquals("org.example.app.test.functional", AppFunctionalTest.class.getModule().getName());
try (InputStream is = AppFunctionalTest.class.getResourceAsStream("/data.txt")) {
assertNotNull(is);
assertEquals("DEF", new String(is.readAllBytes(), UTF_8));
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DEF
3 changes: 3 additions & 0 deletions samples/module-info-dsl-no-platform/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
plugins {
id("org.example.root")
}
38 changes: 38 additions & 0 deletions samples/module-info-dsl-no-platform/build.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
> Task :lib:compileJava
> Task :lib:processResources NO-SOURCE
> Task :lib:classes
> Task :lib:jar
> Task :app:compileJava
> Task :app:processResources NO-SOURCE
> Task :app:classes
> Task :app:jar
> Task :app:startScripts
> Task :app:distTar
> Task :app:distZip
> Task :app:assemble
> Task :app:compileTestJava
> Task :app:processTestResources
> Task :app:testClasses
> Task :app:test
> Task :app:compileTestFunctionalJava
> Task :app:processTestFunctionalResources
> Task :app:testFunctionalClasses
> Task :app:testFunctionalJar
> Task :app:testFunctional
> Task :app:check
> Task :app:build
> Task :lib:assemble
> Task :lib:compileTestJava
> Task :lib:processTestResources
> Task :lib:testClasses
> Task :lib:test
> Task :lib:compileTestFunctionalJava
> Task :lib:processTestFunctionalResources
> Task :lib:testFunctionalClasses
> Task :lib:testFunctionalJar
> Task :lib:testFunctional
> Task :lib:check
> Task :lib:build

> Task :app:run
[main] INFO org.example.app.App - App running...
2 changes: 2 additions & 0 deletions samples/module-info-dsl-no-platform/build.sample.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
executable: gradlew
expected-output-file: build.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
plugins {
`kotlin-dsl`
}

dependencies {
implementation("com.autonomousapps:dependency-analysis-gradle-plugin:1.20.0")
implementation("org.gradlex:java-module-dependencies:1.3.1")
implementation("org.gradlex:java-module-testing:1.2")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
dependencyResolutionManagement {
repositories.gradlePluginPortal()
}

// This is for testing against the latest version of the plugin, remove if you copied this for a real project
includeBuild(extra.properties["pluginLocation"] ?: rootDir.parentFile.parentFile.parentFile.parent)
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
plugins {
id("java")
id("org.gradlex.java-module-dependencies")
id("org.gradlex.java-module-testing")
}

group = "org.example"

java.toolchain.languageVersion.set(JavaLanguageVersion.of(17))
testing.suites.register<JvmTestSuite>("testFunctional")
tasks.check { dependsOn(tasks.named("testFunctional")) }

javaModuleDependencies {
versionsFromPlatformAndConsistentResolution(":app", ":app")
}

tasks.withType<Test>().configureEach {
jvmArgs("-Dorg.slf4j.simpleLogger.defaultLogLevel=error")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
plugins {
id("org.example.java-base")
id("application")
id("org.gradlex.java-module-versions")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
plugins {
id("org.example.java-base")
id("java-library")
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
plugins {
id("com.autonomousapps.dependency-analysis")
}
7 changes: 7 additions & 0 deletions samples/module-info-dsl-no-platform/lib/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
plugins {
id("org.example.java-module")
}

testModuleInfo {
requires("org.junit.jupiter.api")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module org.example.lib {
requires transitive com.fasterxml.jackson.databind;

requires java.logging; // JDK module

exports org.example.lib;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.example.lib;

import com.fasterxml.jackson.databind.ObjectMapper;

public class Lib {

public void process(ObjectMapper mapper) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.example.lib;

import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.InputStream;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

class LibTest {

@Test
void testLib() throws IOException {
assertEquals("org.example.lib", Lib.class.getModule().getName());
assertEquals("org.example.lib", LibTest.class.getModule().getName());
try (InputStream is = LibTest.class.getResourceAsStream("/data.txt")) {
assertNotNull(is);
assertEquals("2*21", new String(is.readAllBytes(), UTF_8));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2*21
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
open module org.example.lib.test.functional {
requires org.example.lib;
requires org.junit.jupiter.api;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.example.lib.test;

import org.example.lib.Lib;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.InputStream;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

class LibFunctionalTest {

@Test
void testLibFunctional() throws IOException {
assertEquals("org.example.lib", Lib.class.getModule().getName());
assertEquals("org.example.lib.test.functional", LibFunctionalTest.class.getModule().getName());
try (InputStream is = LibFunctionalTest.class.getResourceAsStream("/data.txt")) {
assertNotNull(is);
assertEquals("42", new String(is.readAllBytes(), UTF_8));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
42
8 changes: 8 additions & 0 deletions samples/module-info-dsl-no-platform/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
pluginManagement {
includeBuild("gradle/plugins")
}
dependencyResolutionManagement {
repositories.mavenCentral()
}

include("app", "lib")
17 changes: 17 additions & 0 deletions samples/module-info-dsl/app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
plugins {
id("org.example.java-module-app")
}

application {
applicationDefaultJvmArgs = listOf("-ea")
mainClass.set("org.example.app.App")
mainModule.set("org.example.app")
}

mainModuleInfo {
runtimeOnly("org.slf4j.simple")
}

testModuleInfo {
requires("org.junit.jupiter.api")
}
6 changes: 6 additions & 0 deletions samples/module-info-dsl/app/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module org.example.app {
requires org.example.lib;
requires org.slf4j;

exports org.example.app;
}
16 changes: 16 additions & 0 deletions samples/module-info-dsl/app/src/main/java/org/example/app/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.example.app;

import org.example.lib.Lib;
import org.slf4j.LoggerFactory;

public class App {

public static void main(String[] args) {
LoggerFactory.getLogger(App.class).info("App running...");
doWork();
}

public static void doWork() {
new Lib();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.example.app;

import org.example.lib.Lib;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.InputStream;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

public class AppTest {

@Test
public void testApp() throws IOException {
assertEquals("org.example.lib", Lib.class.getModule().getName());
assertEquals("org.example.app", App.class.getModule().getName());
assertEquals("org.example.app", AppTest.class.getModule().getName());
try (InputStream is = AppTest.class.getResourceAsStream("/data.txt")) {
assertNotNull(is);
assertEquals("ABC", new String(is.readAllBytes(), UTF_8));
}
}

}
1 change: 1 addition & 0 deletions samples/module-info-dsl/app/src/test/resources/data.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ABC
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
open module org.example.app.test.functional {
requires org.example.app;
requires org.junit.jupiter.api;
}
Loading

0 comments on commit 91468c0

Please sign in to comment.