final Project project = pluginConvention.getProject();
final ProjectSourceSet projectSourceSet = project.getExtensions().getByType(ProjectSourceSet.class);
pluginConvention.getSourceSets().all(new Action<SourceSet>() {
public void execute(final SourceSet sourceSet) {
ConventionMapping outputConventionMapping = ((IConventionAware) sourceSet.getOutput()).getConventionMapping();
ConfigurationContainer configurations = project.getConfigurations();
Configuration compileConfiguration = configurations.findByName(sourceSet.getCompileConfigurationName());
if (compileConfiguration == null) {
compileConfiguration = configurations.create(sourceSet.getCompileConfigurationName());
}
compileConfiguration.setVisible(false);
compileConfiguration.setDescription(String.format("Compile classpath for %s.", sourceSet));
Configuration runtimeConfiguration = configurations.findByName(sourceSet.getRuntimeConfigurationName());
if (runtimeConfiguration == null) {
runtimeConfiguration = configurations.create(sourceSet.getRuntimeConfigurationName());
}
runtimeConfiguration.setVisible(false);
runtimeConfiguration.extendsFrom(compileConfiguration);
runtimeConfiguration.setDescription(String.format("Runtime classpath for %s.", sourceSet));
sourceSet.setCompileClasspath(compileConfiguration);
sourceSet.setRuntimeClasspath(sourceSet.getOutput().plus(runtimeConfiguration));
outputConventionMapping.map("classesDir", new Callable<Object>() {
public Object call() throws Exception {
String classesDirName = String.format("classes/%s", sourceSet.getName());
return new File(project.getBuildDir(), classesDirName);
}
});
outputConventionMapping.map("resourcesDir", new Callable<Object>() {
public Object call() throws Exception {
String classesDirName = String.format("resources/%s", sourceSet.getName());
return new File(project.getBuildDir(), classesDirName);
}
});
sourceSet.getJava().srcDir(String.format("src/%s/java", sourceSet.getName()));
sourceSet.getResources().srcDir(String.format("src/%s/resources", sourceSet.getName()));
sourceSet.compiledBy(sourceSet.getClassesTaskName());
FunctionalSourceSet functionalSourceSet = projectSourceSet.create(sourceSet.getName());
Classpath compileClasspath = new SourceSetCompileClasspath(sourceSet);
DefaultJavaSourceSet javaSourceSet = instantiator.newInstance(DefaultJavaSourceSet.class, "java", sourceSet.getJava(), compileClasspath, functionalSourceSet);
functionalSourceSet.add(javaSourceSet);
JvmResourceSet resourceSet = instantiator.newInstance(DefaultJvmResourceSet.class, "resources", sourceSet.getResources(), functionalSourceSet);
functionalSourceSet.add(resourceSet);
BinaryContainer binaryContainer = project.getExtensions().getByType(BinaryContainer.class);
ClassDirectoryBinarySpec binary = binaryContainer.create(String.format("%sClasses", sourceSet.getName()), ClassDirectoryBinarySpec.class);
ConventionMapping conventionMapping = new DslObject(binary).getConventionMapping();
conventionMapping.map("classesDir", new Callable<File>() {
public File call() throws Exception {
return sourceSet.getOutput().getClassesDir();
}
});
conventionMapping.map("resourcesDir", new Callable<File>() {
public File call() throws Exception {
return sourceSet.getOutput().getResourcesDir();
}
});