Skip to content

Commit

Permalink
fix #100: change destinationDir only when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
siordache committed Aug 11, 2019
1 parent f0955d3 commit 0bcc21a
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 11 deletions.
Expand Up @@ -2,11 +2,15 @@

import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.file.SourceDirectorySet;
import org.gradle.api.internal.plugins.DslObject;
import org.gradle.api.plugins.JavaPlugin;
import org.gradle.api.tasks.GroovySourceSet;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.SourceSetContainer;
import org.gradle.api.tasks.compile.JavaCompile;

import java.lang.reflect.InvocationTargetException;
import java.util.Optional;

/**
Expand Down Expand Up @@ -52,6 +56,40 @@ public SourceSet mainSourceSet() {
public SourceSet testSourceSet() {
return sourceSet(SourceSet.TEST_SOURCE_SET_NAME);
}

public GroovySourceSet groovySourceSet(SourceSet javaSourceSet) {
return (GroovySourceSet)new DslObject(javaSourceSet).getConvention().getPlugins().get("groovy");
}

public GroovySourceSet groovyMainSourceSet() {
return groovySourceSet(mainSourceSet());
}

public GroovySourceSet groovyTestSourceSet() {
return groovySourceSet(testSourceSet());
}

public Object kotlinSourceSet(SourceSet javaSourceSet) {
return new DslObject(javaSourceSet).getConvention().getPlugins().get("kotlin");
}

public SourceDirectorySet kotlinSourceDirectorySet(SourceSet javaSourceSet) {
var kotlinSourceSet = new DslObject(javaSourceSet).getConvention().getPlugins().get("kotlin");
try {
return (SourceDirectorySet)kotlinSourceSet.getClass().getMethod("getKotlin").invoke(kotlinSourceSet);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

public SourceDirectorySet kotlinMainSourceDirectorySet() {
return kotlinSourceDirectorySet(mainSourceSet());
}

public SourceDirectorySet kotlinTestSourceDirectorySet() {
return kotlinSourceDirectorySet(testSourceSet());
}

//endregion

//region TASKS
Expand Down
Expand Up @@ -2,7 +2,6 @@

import org.gradle.api.Project;
import org.gradle.api.file.FileCollection;
import org.gradle.api.tasks.compile.AbstractCompile;
import org.gradle.api.tasks.compile.JavaCompile;
import org.javamodularity.moduleplugin.JavaProjectHelper;
import org.javamodularity.moduleplugin.extensions.CompileModuleOptions;
Expand All @@ -12,9 +11,6 @@

class CompileJavaTaskMutator {

private static final String COMPILE_KOTLIN_TASK_NAME = "compileKotlin";
static final String COMPILE_GROOVY_TASK_NAME = "compileGroovy";

private final Project project;
/**
* {@linkplain JavaCompile#getClasspath() Classpath} of {@code compileJava} task.
Expand All @@ -41,10 +37,6 @@ void modularizeJavaCompileTask(JavaCompile javaCompile) {
List<String> compilerArgs = buildCompilerArgs(javaCompile);
javaCompile.getOptions().setCompilerArgs(compilerArgs);
javaCompile.setClasspath(project.files());

// https://github.com/java9-modularity/gradle-modules-plugin/issues/45
helper().findTask(COMPILE_KOTLIN_TASK_NAME, AbstractCompile.class)
.ifPresent(compileKotlin -> javaCompile.setDestinationDir(compileKotlin.getDestinationDir()));
}

private List<String> buildCompilerArgs(JavaCompile javaCompile) {
Expand Down
Expand Up @@ -4,11 +4,16 @@
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.plugins.JavaPlugin;
import org.gradle.api.tasks.compile.AbstractCompile;
import org.gradle.api.tasks.compile.GroovyCompile;
import org.gradle.api.tasks.compile.JavaCompile;
import org.javamodularity.moduleplugin.extensions.CompileModuleOptions;

import java.io.File;

public class CompileTask extends AbstractCompileTask {
private static final String COMPILE_KOTLIN_TASK_NAME = "compileKotlin";
private static final String COMPILE_GROOVY_TASK_NAME = "compileGroovy";

public CompileTask(Project project) {
super(project);
Expand All @@ -25,11 +30,29 @@ public void configureCompileJava() {
private void configureCompileJava(JavaCompile compileJava) {
var moduleOptions = compileJava.getExtensions().create("moduleOptions", CompileModuleOptions.class, project);
project.afterEvaluate(p -> {
helper().findTask(CompileJavaTaskMutator.COMPILE_GROOVY_TASK_NAME, GroovyCompile.class)
helper().findTask(COMPILE_GROOVY_TASK_NAME, GroovyCompile.class)
.ifPresent(compileGroovy -> {
moduleOptions.setCompileModuleInfoSeparately(true);
compileGroovy.setDestinationDir(compileJava.getDestinationDir());
if(!helper().groovyMainSourceSet().getGroovy().getFiles().isEmpty()) {
moduleOptions.setCompileModuleInfoSeparately(true);
compileGroovy.setDestinationDir(compileJava.getDestinationDir());
}
});

helper().findTask(COMPILE_KOTLIN_TASK_NAME, AbstractCompile.class)
.ifPresent(compileKotlin -> {
boolean hasKt = helper().kotlinMainSourceDirectorySet().getFiles().stream()
.map(File::getName)
.anyMatch(name -> name.endsWith(".kt"));
boolean hasJava = helper().mainSourceSet().getJava().getFiles().stream()
.map(File::getName)
.filter(name -> ! name.equals("module-info.java"))
.anyMatch(name -> name.endsWith(".java"));
if(hasKt && !hasJava) {
moduleOptions.setCompileModuleInfoSeparately(true);
compileJava.setDestinationDir(compileKotlin.getDestinationDir());
}
});

if (moduleOptions.getCompileModuleInfoSeparately()) {
compileJava.exclude("module-info.java");
} else {
Expand Down

0 comments on commit 0bcc21a

Please sign in to comment.