forked from java9-modularity/gradle-modules-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
java9-modularity#72: support for "moduleOptions.compileModuleInfoSepa…
…rately" added CompileModuleOptions and CompileModuleInfoTask NOTE: potentially breaking change for "compileJava" in Kotlin DSL (see modified "build.gradle.kts")
- Loading branch information
1 parent
86e65c4
commit 00c78ce
Showing
11 changed files
with
216 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/main/java/org/javamodularity/moduleplugin/extensions/CompileModuleOptions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package org.javamodularity.moduleplugin.extensions; | ||
|
||
import org.gradle.api.Project; | ||
import org.gradle.api.tasks.compile.JavaCompile; | ||
import org.javamodularity.moduleplugin.tasks.ModuleOptions; | ||
|
||
public class CompileModuleOptions extends ModuleOptions { | ||
|
||
/** | ||
* Name of the extra Java compile task created if {@code compileModuleInfoSeparately} is {@code true}. | ||
*/ | ||
public static final String COMPILE_MODULE_INFO_TASK_NAME = "compileModuleInfoJava"; | ||
|
||
private final Project project; | ||
|
||
private boolean compileModuleInfoSeparately = false; | ||
|
||
public CompileModuleOptions(Project project) { | ||
super(project); | ||
this.project = project; | ||
} | ||
|
||
public boolean getCompileModuleInfoSeparately() { | ||
return compileModuleInfoSeparately; | ||
} | ||
|
||
public void setCompileModuleInfoSeparately(boolean compileModuleInfoSeparately) { | ||
if (compileModuleInfoSeparately) { | ||
// we need to create "compileModuleInfoJava" task eagerly so that the user can configure it immediately | ||
project.getTasks().maybeCreate(COMPILE_MODULE_INFO_TASK_NAME, JavaCompile.class); | ||
} | ||
this.compileModuleInfoSeparately = compileModuleInfoSeparately; | ||
} | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/org/javamodularity/moduleplugin/tasks/AbstractCompileTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package org.javamodularity.moduleplugin.tasks; | ||
|
||
import org.gradle.api.Project; | ||
import org.gradle.api.tasks.compile.JavaCompile; | ||
import org.javamodularity.moduleplugin.JavaProjectHelper; | ||
import org.javamodularity.moduleplugin.extensions.CompileModuleOptions; | ||
|
||
abstract class AbstractCompileTask { | ||
|
||
protected final Project project; | ||
|
||
AbstractCompileTask(Project project) { | ||
this.project = project; | ||
} | ||
|
||
final CompileJavaTaskMutator createCompileJavaTaskMutator( | ||
JavaCompile compileJava, CompileModuleOptions moduleOptions) { | ||
return new CompileJavaTaskMutator(project, compileJava.getClasspath(), moduleOptions); | ||
} | ||
|
||
final JavaProjectHelper helper() { | ||
return new JavaProjectHelper(project); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
src/main/java/org/javamodularity/moduleplugin/tasks/CompileModuleInfoTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package org.javamodularity.moduleplugin.tasks; | ||
|
||
import org.gradle.api.Action; | ||
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.javamodularity.moduleplugin.extensions.CompileModuleOptions; | ||
|
||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
public class CompileModuleInfoTask extends AbstractCompileTask { | ||
|
||
public CompileModuleInfoTask(Project project) { | ||
super(project); | ||
} | ||
|
||
/** | ||
* @see CompileTask#configureCompileJava() | ||
*/ | ||
public void configureCompileModuleInfoJava() { | ||
helper().findCompileJavaTask(JavaPlugin.COMPILE_JAVA_TASK_NAME) | ||
.ifPresent(this::configureCompileModuleInfoJava); | ||
} | ||
|
||
private void configureCompileModuleInfoJava(JavaCompile compileJava) { | ||
var moduleOptions = compileJava.getExtensions().getByType(CompileModuleOptions.class); | ||
project.afterEvaluate(p -> { | ||
if (moduleOptions.getCompileModuleInfoSeparately()) { | ||
configureModularityForCompileModuleInfoJava(compileJava, moduleOptions); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* @see CompileTask#configureModularityForCompileJava | ||
*/ | ||
void configureModularityForCompileModuleInfoJava( | ||
JavaCompile compileJava, CompileModuleOptions moduleOptions) { | ||
JavaCompile compileModuleInfoJava = preconfigureCompileModuleInfoJava(compileJava); | ||
CompileJavaTaskMutator mutator = createCompileJavaTaskMutator(compileJava, moduleOptions); | ||
|
||
// don't convert to lambda: https://github.com/java9-modularity/gradle-modules-plugin/issues/54 | ||
compileModuleInfoJava.doFirst(new Action<Task>() { | ||
@Override | ||
public void execute(Task task) { | ||
mutator.modularizeJavaCompileTask(compileModuleInfoJava); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Preconfigures a separate task that is meant to compile {@code module-info.java} separately. | ||
* Final (modular) configuration is performed later by {@link CompileJavaTaskMutator}. | ||
*/ | ||
private JavaCompile preconfigureCompileModuleInfoJava(JavaCompile compileJava) { | ||
var compileModuleInfoJava = helper().compileJavaTask(CompileModuleOptions.COMPILE_MODULE_INFO_TASK_NAME); | ||
|
||
compileModuleInfoJava.setClasspath(project.files()); // empty | ||
compileModuleInfoJava.setSource(pathToModuleInfoJava()); | ||
compileModuleInfoJava.setDestinationDir(compileJava.getDestinationDir()); | ||
|
||
// we need all the compiled classes before compiling module-info.java | ||
compileModuleInfoJava.dependsOn(compileJava); | ||
|
||
// make "classes" trigger module-info.java compilation | ||
helper().task(JavaPlugin.CLASSES_TASK_NAME).dependsOn(compileModuleInfoJava); | ||
|
||
return compileModuleInfoJava; | ||
} | ||
|
||
private Path pathToModuleInfoJava() { | ||
return helper().mainSourceSet().getJava().getSrcDirs().stream() | ||
.map(srcDir -> srcDir.toPath().resolve("module-info.java")) | ||
.filter(Files::exists) | ||
.findFirst() | ||
.orElseThrow(() -> new IllegalStateException("module-info.java not found")); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ plugins { | |
compileJava { | ||
moduleOptions { | ||
addModules = [] | ||
compileModuleInfoSeparately = false | ||
} | ||
} | ||
|
||
|