Skip to content
Permalink
Browse files

java9-modularity#72: initial refactorings/improvements

necessary for further commits

MAIN:
1) renamed TestModuleOptions.isRunOnClasspath() to getRunOnClasspath() (otherwise won't work with Kotlin DSL) + added a Kotlin DSL example for "runOnClasspath = true" to README.md
2) introduced JavaProjectHelper and applied it to CompileTask
3) added comments for all anonymous classes that should not be removed
4) minor improvements in ModuleSystemPlugin
5) minor fixes in test-project-kotlin/README.md

TEST:
1) bumped smoke-test Gradle version to 5.0 (for improved Kotlin DSL)
2) introduced a no-op "moduleOptions" access in greeter.api (for testing DSL)
3) introduced SmokeTestHelper for ModulePluginSmokeTest
4) disabled stackTraceFilters in all tests
5) updated Kotlin to 1.3.20
  • Loading branch information...
tlinkowski committed Mar 17, 2019
1 parent 6b3d091 commit 365192ca72b51b786fac1bcc2c5d008da723061d
@@ -218,7 +218,10 @@ See `src/test/java/module-info.test` and `src/test/java/greeter/ScriptingTest.ja
Fall-back to classpath mode
----

If for whatever reason this is unwanted or introduces problems, you can enable classpath mode, which essentially turns of the plugin while running tests.
If for whatever reason this is unwanted or introduces problems, you can enable classpath mode, which essentially turns off the plugin while running tests.

<details open>
<summary>Groovy DSL</summary>

```groovy
test {
@@ -228,6 +231,22 @@ test {
}
```

</details>
<details>
<summary>Kotlin DSL</summary>

```kotlin
tasks {
test {
extensions.configure(TestModuleOptions::class) {
runOnClasspath = true
}
}
}
```

</details>

Blackbox testing
===

@@ -239,7 +258,7 @@ This module `requires` and/or `uses` the module under test, and tests it's exter
In the following example we test a module `greeter.provider`, which provides a service implementation of type `Greeter`.
The `Greeter` type is provided by yet another module `greeter.api`.

The test module would typically be named something similar to the the module it's testing, e.g. `greeter.provider.test`.
The test module would typically be named something similar to the module it's testing, e.g. `greeter.provider.test`.
In `src/main/java` it has some code that looks like code that you would normally write to use the module that's being tested.
For example, we do a service lookup.

@@ -58,6 +58,7 @@ test {

testLogging {
events 'PASSED', 'FAILED', 'SKIPPED'
stackTraceFilters = []
}
}

@@ -0,0 +1,63 @@
package org.javamodularity.moduleplugin;

import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.plugins.JavaPlugin;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.SourceSetContainer;
import org.gradle.api.tasks.compile.JavaCompile;

import java.util.Optional;

/**
* Generic helper for Gradle {@link Project} API that has {@link JavaPlugin} applied.
*/
public final class JavaProjectHelper {

private final Project project;

public JavaProjectHelper(Project project) {
this.project = project;
}

public Project project() {
return project;
}

//region SOURCE SETS
public SourceSetContainer sourceSets() {
return project.getExtensions().getByType(SourceSetContainer.class);
}

public SourceSet sourceSet(String sourceSetName) {
return sourceSets().getByName(sourceSetName);
}

public SourceSet mainSourceSet() {
return sourceSet(SourceSet.MAIN_SOURCE_SET_NAME);
}

public SourceSet testSourceSet(String sourceSetName) {
return sourceSet(SourceSet.TEST_SOURCE_SET_NAME);
}
//endregion

//region TASKS
public Task task(String taskName) {
return project.getTasks().getByName(taskName);
}

public JavaCompile compileJavaTask(String taskName) {
return (JavaCompile) task(taskName);
}

public Optional<Task> findTask(String taskName) {
return Optional.ofNullable(project.getTasks().findByName(taskName));
}

public Optional<JavaCompile> findCompileJavaTask(String taskName) {
return findTask(taskName).map(JavaCompile.class::cast);
}
//endregion

}
@@ -2,28 +2,29 @@

import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.plugins.ExtensionContainer;
import org.gradle.api.plugins.JavaPlugin;
import org.javamodularity.moduleplugin.tasks.*;

import java.util.Optional;

public class ModuleSystemPlugin implements Plugin<Project> {

@Override
public void apply(Project project) {
project.getPlugins().apply(JavaPlugin.class);
Optional<String> foundModuleName = new ModuleName().findModuleName(project);
foundModuleName.ifPresent(moduleName -> {
project.getExtensions().add("moduleName", moduleName);
project.getExtensions().create("patchModules", PatchModuleExtension.class);
new ModuleName().findModuleName(project).ifPresent(moduleName -> configureModularity(project, moduleName));
}

private void configureModularity(Project project, String moduleName) {
ExtensionContainer extensions = project.getExtensions();
extensions.add("moduleName", moduleName);
extensions.create("patchModules", PatchModuleExtension.class);

new CompileTask().configureCompileJava(project);
new CompileTestTask().configureCompileTestJava(project, moduleName);
new TestTask().configureTestJava(project, moduleName);
new RunTask().configureRun(project, moduleName);
new JavadocTask().configureJavaDoc(project);
ModularJavaExec.configure(project, moduleName);
ModularCreateStartScripts.configure(project, moduleName);
});
new CompileTask(project).configureCompileJava();
new CompileTestTask().configureCompileTestJava(project, moduleName);
new TestTask().configureTestJava(project, moduleName);
new RunTask().configureRun(project, moduleName);
new JavadocTask().configureJavaDoc(project);
ModularJavaExec.configure(project, moduleName);
ModularCreateStartScripts.configure(project, moduleName);
}
}
@@ -9,6 +9,8 @@

class CompileJavaTaskMutator {

private static final String COMPILE_KOTLIN_TASK_NAME = "compileKotlin";

static void mutateJavaCompileTask(Project project, JavaCompile compileJava) {
ModuleOptions moduleOptions = compileJava.getExtensions().getByType(ModuleOptions.class);
PatchModuleExtension patchModuleExtension = project.getExtensions().getByType(PatchModuleExtension.class);
@@ -29,7 +31,8 @@ static void mutateJavaCompileTask(Project project, JavaCompile compileJava) {
compileJava.getOptions().setCompilerArgs(compilerArgs);
compileJava.setClasspath(project.files());

AbstractCompile compileKotlin = (AbstractCompile) project.getTasks().findByName("compileKotlin");
// https://github.com/java9-modularity/gradle-modules-plugin/issues/45
AbstractCompile compileKotlin = (AbstractCompile) project.getTasks().findByName(COMPILE_KOTLIN_TASK_NAME);
if (compileKotlin != null) {
compileJava.setDestinationDir(compileKotlin.getDestinationDir());
}
@@ -5,26 +5,34 @@
import org.gradle.api.Task;
import org.gradle.api.plugins.JavaPlugin;
import org.gradle.api.tasks.compile.JavaCompile;
import org.javamodularity.moduleplugin.JavaProjectHelper;

public class CompileTask {

public void configureCompileJava(Project project) {
JavaCompile compileJava = (JavaCompile) project.getTasks().findByName(JavaPlugin.COMPILE_JAVA_TASK_NAME);
if (compileJava != null) {
compileJava.getExtensions().create("moduleOptions", ModuleOptions.class, project);
private final Project project;

compileJava.doFirst(new Action<Task>() {
public CompileTask(Project project) {
this.project = project;
}

public void configureCompileJava() {
helper().findCompileJavaTask(JavaPlugin.COMPILE_JAVA_TASK_NAME)
.ifPresent(this::configureCompileJava);
}

/* (non-Javadoc)
* @see org.gradle.api.Action#execute(java.lang.Object)
*/
@Override
public void execute(Task task) {
CompileJavaTaskMutator.mutateJavaCompileTask(project, compileJava);
}
private void configureCompileJava(JavaCompile compileJava) {
compileJava.getExtensions().create("moduleOptions", ModuleOptions.class, project);

});
}
// don't convert to lambda: https://github.com/java9-modularity/gradle-modules-plugin/issues/54
compileJava.doFirst(new Action<Task>() {
@Override
public void execute(Task task) {
CompileJavaTaskMutator.mutateJavaCompileTask(project, compileJava);
}
});
}

private JavaProjectHelper helper() {
return new JavaProjectHelper(project);
}
}
@@ -1,9 +1,5 @@
package org.javamodularity.moduleplugin.tasks;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

import org.gradle.api.Action;
import org.gradle.api.Project;
import org.gradle.api.Task;
@@ -13,6 +9,9 @@
import org.gradle.api.tasks.compile.JavaCompile;
import org.javamodularity.moduleplugin.TestEngine;

import java.util.ArrayList;
import java.util.List;

public class CompileTestTask {

public void configureCompileTestJava(Project project, String moduleName) {
@@ -23,11 +22,8 @@ public void configureCompileTestJava(Project project, String moduleName) {
compileTestJava.getExtensions().create("moduleOptions", ModuleOptions.class, project);
SourceSet testSourceSet = javaConvention.getSourceSets().getByName(SourceSet.TEST_SOURCE_SET_NAME);

// don't convert to lambda: https://github.com/java9-modularity/gradle-modules-plugin/issues/54
compileTestJava.doFirst(new Action<Task>() {

/* (non-Javadoc)
* @see org.gradle.api.Action#execute(java.lang.Object)
*/
@Override
public void execute(Task task) {
var args = new ArrayList<>(compileTestJava.getOptions().getCompilerArgs());
@@ -38,19 +34,10 @@ public void execute(Task task) {
"--patch-module", moduleName + "=" + testSourceSet.getJava().getSourceDirectories().getAsPath()
));

TestEngine.select(project).ifPresent(new Consumer<TestEngine>() {

/* (non-Javadoc)
* @see java.util.function.Consumer#accept(java.lang.Object)
*/
@Override
public void accept(TestEngine testEngine) {
args.addAll(List.of(
"--add-modules", testEngine.moduleName,
"--add-reads", moduleName + "=" + testEngine.moduleName));
}

});
TestEngine.select(project).ifPresent(testEngine -> args.addAll(List.of(
"--add-modules", testEngine.moduleName,
"--add-reads", moduleName + "=" + testEngine.moduleName
)));

ModuleOptions moduleOptions = compileTestJava.getExtensions().getByType(ModuleOptions.class);
if (!moduleOptions.getAddModules().isEmpty()) {
@@ -4,7 +4,6 @@
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.plugins.JavaPlugin;
import org.gradle.api.tasks.compile.JavaCompile;
import org.gradle.api.tasks.javadoc.Javadoc;
import org.gradle.external.javadoc.CoreJavadocOptions;

@@ -16,11 +15,8 @@ public void configureJavaDoc(Project project) {
javadoc.getExtensions().create("moduleOptions", ModuleOptions.class, project);
PatchModuleExtension patchModuleExtension = project.getExtensions().getByType(PatchModuleExtension.class);

// don't convert to lambda: https://github.com/java9-modularity/gradle-modules-plugin/issues/54
javadoc.doFirst(new Action<Task>() {

/* (non-Javadoc)
* @see org.gradle.api.Action#execute(java.lang.Object)
*/
@Override
public void execute(Task task) {
ModuleOptions moduleOptions = javadoc.getExtensions().getByType(ModuleOptions.class);
@@ -7,7 +7,6 @@
import org.gradle.api.distribution.Distribution;
import org.gradle.api.distribution.DistributionContainer;
import org.gradle.api.file.CopySpec;
import org.gradle.api.file.FileCollection;
import org.gradle.api.logging.Logger;
import org.gradle.api.logging.Logging;
import org.gradle.api.plugins.JavaPluginConvention;
@@ -21,7 +20,6 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class RunTaskMutator {
@@ -54,6 +52,7 @@ public void updateStartScriptsTask(String taskStartScriptsName) {
public void updateStartScriptsTask(CreateStartScripts startScriptsTask) {
PatchModuleExtension patchModuleExtension = project.getExtensions().getByType(PatchModuleExtension.class);

// don't convert to lambda: https://github.com/java9-modularity/gradle-modules-plugin/issues/54
startScriptsTask.doFirst(new Action<Task>() {
@Override
public void execute(final Task task) {
@@ -90,6 +89,7 @@ public void execute(final Task task) {
}
});

// don't convert to lambda: https://github.com/java9-modularity/gradle-modules-plugin/issues/54
startScriptsTask.doLast(new Action<Task>() {
@Override
public void execute(final Task task) {
@@ -106,6 +106,7 @@ public void movePatchedLibs() {

if(!patchModuleExtension.getConfig().isEmpty()) {
Distribution distribution = ((DistributionContainer) project.getExtensions().getByName("distributions")).getByName("main");
// don't convert to lambda: https://github.com/java9-modularity/gradle-modules-plugin/issues/54
distribution.contents(new Action<CopySpec>() {
@Override
public void execute(CopySpec copySpec) {
@@ -118,6 +119,7 @@ public void execute(CopySpec copySpec) {
}

private void updateJavaExecTask() {
// don't convert to lambda: https://github.com/java9-modularity/gradle-modules-plugin/issues/54
execTask.doFirst(new Action<Task>() {
@Override
public void execute(final Task task) {
@@ -10,7 +10,7 @@ public TestModuleOptions(Project project) {
super(project);
}

public boolean isRunOnClasspath() {
public boolean getRunOnClasspath() {
return runOnClasspath;
}

0 comments on commit 365192c

Please sign in to comment.
You can’t perform that action at this time.