gradle: use original output path during mapping of library dependencies on module deps

This commit is contained in:
Vladislav.Soroka
2016-06-09 17:48:12 +03:00
parent 85f8adf90f
commit bc6e8fc85b
6 changed files with 124 additions and 20 deletions
@@ -78,6 +78,7 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static org.jetbrains.plugins.gradle.service.project.GradleProjectResolver.CONFIGURATION_ARTIFACTS;
import static org.jetbrains.plugins.gradle.service.project.GradleProjectResolver.MODULES_OUTPUTS;
import static org.jetbrains.plugins.gradle.service.project.GradleProjectResolverUtil.*;
/**
@@ -339,6 +340,11 @@ public class BaseGradleProjectResolverExtension implements GradleProjectResolver
@NotNull DataNode<ModuleData> ideModule) {
ExternalProject externalProject = resolverCtx.getExtraProject(gradleModule, ExternalProject.class);
if (resolverCtx.isResolveModulePerSourceSet() && externalProject != null) {
DataNode<ProjectData> projectDataNode = ideModule.getDataNode(ProjectKeys.PROJECT);
assert projectDataNode != null;
final Map<String, Pair<String, ExternalSystemSourceType>> moduleOutputsMap = projectDataNode.getUserData(MODULES_OUTPUTS);
assert moduleOutputsMap != null;
processSourceSets(externalProject, ideModule, new SourceSetsProcessor() {
@Override
public void process(@NotNull DataNode<? extends ModuleData> dataNode, @NotNull ExternalSourceSet sourceSet) {
@@ -346,8 +352,23 @@ public class BaseGradleProjectResolverExtension implements GradleProjectResolver
ExternalSystemSourceType sourceType = ExternalSystemSourceType.from(directorySetEntry.getKey());
ExternalSourceDirectorySet sourceDirectorySet = directorySetEntry.getValue();
final ModuleData moduleData = dataNode.getData();
moduleData.setCompileOutputPath(sourceType, sourceDirectorySet.getOutputDir().getAbsolutePath());
File outputDir = sourceDirectorySet.getOutputDir();
moduleData.setCompileOutputPath(sourceType, outputDir.getAbsolutePath());
moduleData.setInheritProjectCompileOutputPath(sourceDirectorySet.isCompilerOutputPathInherited());
File gradleOutputDir = sourceDirectorySet.getGradleOutputDir();
String gradleOutputPath = moduleData.getCompileOutputPath(sourceType);
if(!gradleOutputDir.getPath().equals(outputDir.getPath())) {
gradleOutputPath = ExternalSystemApiUtil.toCanonicalPath(gradleOutputDir.getAbsolutePath());
moduleOutputsMap.put(gradleOutputPath, Pair.create(moduleData.getId(), sourceType));
}
Map<ExternalSystemSourceType, String> map = dataNode.getUserData(GradleProjectResolver.GRADLE_OUTPUTS);
if(map == null) {
map = ContainerUtil.newHashMap();
dataNode.putUserData(GradleProjectResolver.GRADLE_OUTPUTS, map);
}
map.put(sourceType, gradleOutputPath);
}
}
});
@@ -81,6 +81,7 @@ public class GradleProjectResolver implements ExternalSystemProjectResolver<Grad
Key.create("resolvedSourceSets");
public static final Key<Map<String/* output path */, Pair<String /* module id*/, ExternalSystemSourceType>>> MODULES_OUTPUTS =
Key.create("moduleOutputsMap");
public static final Key<Map<ExternalSystemSourceType, String /* output path*/>> GRADLE_OUTPUTS = Key.create("gradleOutputs");
public static final Key<Map<String/* artifact path */, String /* module id*/>> CONFIGURATION_ARTIFACTS =
Key.create("gradleArtifactsMap");
@@ -426,17 +427,23 @@ public class GradleProjectResolver implements ExternalSystemProjectResolver<Grad
final ModuleData moduleData = pair.first.getData();
if (targetModuleOutputPaths == null) {
final Set<String> compileSet = ContainerUtil.newHashSet();
ContainerUtil.addAllNotNull(compileSet,
moduleData.getCompileOutputPath(ExternalSystemSourceType.SOURCE),
moduleData.getCompileOutputPath(ExternalSystemSourceType.RESOURCE));
Map<ExternalSystemSourceType, String> gradleOutputs = pair.first.getUserData(GRADLE_OUTPUTS);
if(gradleOutputs != null) {
ContainerUtil.addAllNotNull(compileSet,
gradleOutputs.get(ExternalSystemSourceType.SOURCE),
gradleOutputs.get(ExternalSystemSourceType.RESOURCE));
}
if (!compileSet.isEmpty() && ContainerUtil.intersects(libraryPaths, compileSet)) {
targetModuleOutputPaths = compileSet;
}
else {
final Set<String> testSet = ContainerUtil.newHashSet();
ContainerUtil.addAllNotNull(testSet,
moduleData.getCompileOutputPath(ExternalSystemSourceType.TEST),
moduleData.getCompileOutputPath(ExternalSystemSourceType.TEST_RESOURCE));
Map<ExternalSystemSourceType, String> gradleTestOutputs = pair.first.getUserData(GRADLE_OUTPUTS);
if(gradleTestOutputs != null) {
ContainerUtil.addAllNotNull(compileSet,
gradleTestOutputs.get(ExternalSystemSourceType.TEST),
gradleTestOutputs.get(ExternalSystemSourceType.TEST_RESOURCE));
}
if (compileSet.isEmpty() && ContainerUtil.intersects(libraryPaths, testSet)) {
targetModuleOutputPaths = testSet;
}
@@ -581,6 +581,65 @@ public class GradleDependenciesImportingTest extends GradleImportingTestCase {
assertModuleModuleDeps("project2", "project1");
}
@Test
@TargetVersions("2.0+")
public void testTestModuleDependencyAsArtifactFromTestSourceSetOutput3() throws Exception {
createSettingsFile("include 'project1'\n" +
"include 'project2'\n");
importProject(
"allprojects {\n" +
" apply plugin: 'idea'\n" +
" idea {\n" +
" module {\n" +
" inheritOutputDirs = false\n" +
" outputDir = file(\"buildIdea/main\")\n" +
" testOutputDir = file(\"buildIdea/test\")\n" +
" excludeDirs += file('buildIdea')\n" +
" }\n" +
" }\n" +
"}\n" +
"\n" +
"project(':project1') {\n" +
" apply plugin: 'java'\n" +
" configurations {\n" +
" testArtifacts\n" +
" }\n" +
"\n" +
" task testJar(type: Jar) {\n" +
" classifier = 'tests'\n" +
" from sourceSets.test.output\n" +
" }\n" +
"\n" +
" artifacts {\n" +
" testArtifacts testJar\n" +
" }\n" +
"}\n" +
"\n" +
"project(':project2') {\n" +
" apply plugin: 'java'\n" +
" dependencies {\n" +
" testCompile project(path: ':project1', configuration: 'testArtifacts')\n" +
" }\n" +
"}\n"
);
assertModules("project", "project1", "project1_main", "project1_test", "project2", "project2_main", "project2_test");
assertModuleOutput("project1_main", getProjectPath() + "/project1/buildIdea/main", "");
assertModuleOutput("project1_test", "", getProjectPath() + "/project1/buildIdea/test");
assertModuleOutput("project2_main", getProjectPath() + "/project2/buildIdea/main", "");
assertModuleOutput("project2_test", "", getProjectPath() + "/project2/buildIdea/test");
assertModuleModuleDeps("project2_main");
assertModuleModuleDeps("project2_test", "project2_main", "project1_test");
importProjectUsingSingeModulePerGradleProject();
assertModules("project", "project1", "project2");
assertModuleModuleDeps("project2", "project1");
}
@Test
@TargetVersions("2.6+")
public void testProjectSubstitutions() throws Exception {
@@ -36,6 +36,9 @@ public interface ExternalSourceDirectorySet extends Serializable {
@NotNull
File getOutputDir();
@NotNull
File getGradleOutputDir();
/**
* Returns <code>true</code> if compiler output for this ExternalSourceDirectorySet should is inherited from IDEA project
* @return true if compiler output path is inherited, false otherwise
@@ -34,8 +34,8 @@ public class DefaultExternalSourceDirectorySet implements ExternalSourceDirector
private String myName;
@NotNull
private Set<File> mySrcDirs;
@NotNull
private File myOutputDir;
private File myGradleOutputDir;
@NotNull
private Set<String> myExcludes;
@NotNull
@@ -57,6 +57,7 @@ public class DefaultExternalSourceDirectorySet implements ExternalSourceDirector
myName = sourceDirectorySet.getName();
mySrcDirs = new HashSet<File>(sourceDirectorySet.getSrcDirs());
myOutputDir = sourceDirectorySet.getOutputDir();
myGradleOutputDir = sourceDirectorySet.getGradleOutputDir();
myExcludes = new HashSet<String>(sourceDirectorySet.getExcludes());
myIncludes = new HashSet<String>(sourceDirectorySet.getIncludes());
for (ExternalFilter filter : sourceDirectorySet.getFilters()) {
@@ -91,6 +92,23 @@ public class DefaultExternalSourceDirectorySet implements ExternalSourceDirector
return myOutputDir;
}
public void setOutputDir(@NotNull File outputDir) {
myOutputDir = outputDir;
}
@NotNull
@Override
public File getGradleOutputDir() {
return myGradleOutputDir;
}
public void setGradleOutputDir(@NotNull File outputDir) {
myGradleOutputDir = outputDir;
if (myOutputDir == null) {
myOutputDir = outputDir;
}
}
@Override
public boolean isCompilerOutputPathInherited() {
return myInheritedCompilerOutput;
@@ -129,8 +147,4 @@ public class DefaultExternalSourceDirectorySet implements ExternalSourceDirector
public void setFilters(@NotNull List<ExternalFilter> filters) {
myFilters = filters;
}
public void setOutputDir(@NotNull File outputDir) {
myOutputDir = outputDir;
}
}
@@ -236,13 +236,13 @@ class ExternalProjectBuilderImpl implements ModelBuilderService {
ExternalSourceDirectorySet resourcesDirectorySet = new DefaultExternalSourceDirectorySet()
resourcesDirectorySet.name = sourceSet.resources.name
resourcesDirectorySet.srcDirs = sourceSet.resources.srcDirs
resourcesDirectorySet.outputDir = chooseNotNull(sourceSet.output.resourcesDir, sourceSet.output.classesDir, project.buildDir)
resourcesDirectorySet.gradleOutputDir = chooseNotNull(sourceSet.output.resourcesDir, sourceSet.output.classesDir, project.buildDir)
resourcesDirectorySet.inheritedCompilerOutput = inheritOutputDirs
ExternalSourceDirectorySet javaDirectorySet = new DefaultExternalSourceDirectorySet()
javaDirectorySet.name = sourceSet.allJava.name
javaDirectorySet.srcDirs = sourceSet.allJava.srcDirs
javaDirectorySet.outputDir = chooseNotNull(sourceSet.output.classesDir, project.buildDir);
javaDirectorySet.gradleOutputDir = chooseNotNull(sourceSet.output.classesDir, project.buildDir);
javaDirectorySet.inheritedCompilerOutput = inheritOutputDirs
// javaDirectorySet.excludes = javaExcludes + sourceSet.java.excludes;
// javaDirectorySet.includes = javaIncludes + sourceSet.java.includes;
@@ -260,7 +260,7 @@ class ExternalProjectBuilderImpl implements ModelBuilderService {
generatedDirectorySet = new DefaultExternalSourceDirectorySet()
generatedDirectorySet.name = "generated " + javaDirectorySet.name
generatedDirectorySet.srcDirs = files
generatedDirectorySet.outputDir = javaDirectorySet.outputDir
generatedDirectorySet.gradleOutputDir = javaDirectorySet.outputDir
generatedDirectorySet.inheritedCompilerOutput = javaDirectorySet.isCompilerOutputPathInherited()
}
additionalIdeaGenDirs.removeAll(files)
@@ -299,7 +299,7 @@ class ExternalProjectBuilderImpl implements ModelBuilderService {
def testDirectorySet = new DefaultExternalSourceDirectorySet()
testDirectorySet.name = javaDirectorySet.name
testDirectorySet.srcDirs = testDirs
testDirectorySet.outputDir = javaDirectorySet.outputDir
testDirectorySet.gradleOutputDir = javaDirectorySet.outputDir
testDirectorySet.inheritedCompilerOutput = javaDirectorySet.isCompilerOutputPathInherited()
sources.put(ExternalSystemSourceType.TEST, testDirectorySet)
}
@@ -311,7 +311,7 @@ class ExternalProjectBuilderImpl implements ModelBuilderService {
def testResourcesDirectorySet = new DefaultExternalSourceDirectorySet()
testResourcesDirectorySet.name = resourcesDirectorySet.name
testResourcesDirectorySet.srcDirs = testResourcesDirs
testResourcesDirectorySet.outputDir = resourcesDirectorySet.outputDir
testResourcesDirectorySet.gradleOutputDir = resourcesDirectorySet.outputDir
testResourcesDirectorySet.inheritedCompilerOutput = resourcesDirectorySet.isCompilerOutputPathInherited()
sources.put(ExternalSystemSourceType.TEST_RESOURCE, testResourcesDirectorySet)
}
@@ -327,7 +327,7 @@ class ExternalProjectBuilderImpl implements ModelBuilderService {
def testGeneratedDirectorySet = new DefaultExternalSourceDirectorySet()
testGeneratedDirectorySet.name = generatedDirectorySet.name
testGeneratedDirectorySet.srcDirs = testGeneratedDirs
testGeneratedDirectorySet.outputDir = generatedDirectorySet.outputDir
testGeneratedDirectorySet.gradleOutputDir = generatedDirectorySet.outputDir
testGeneratedDirectorySet.inheritedCompilerOutput = generatedDirectorySet.isCompilerOutputPathInherited()
sources.put(ExternalSystemSourceType.TEST_GENERATED, testGeneratedDirectorySet)
@@ -371,7 +371,7 @@ class ExternalProjectBuilderImpl implements ModelBuilderService {
def generatedDirectorySet = new DefaultExternalSourceDirectorySet()
generatedDirectorySet.name = "generated " + mainSourceSet.name
generatedDirectorySet.srcDirs.addAll(mainAdditionalGenDirs)
generatedDirectorySet.outputDir = mainSourceDirectorySet.outputDir
generatedDirectorySet.gradleOutputDir = mainSourceDirectorySet.outputDir
generatedDirectorySet.inheritedCompilerOutput = mainSourceDirectorySet.isCompilerOutputPathInherited()
mainSourceSet.sources.put(ExternalSystemSourceType.SOURCE_GENERATED, generatedDirectorySet)
}
@@ -398,7 +398,7 @@ class ExternalProjectBuilderImpl implements ModelBuilderService {
def generatedDirectorySet = new DefaultExternalSourceDirectorySet()
generatedDirectorySet.name = "generated " + testSourceSet.name
generatedDirectorySet.srcDirs.addAll(testAdditionalGenDirs)
generatedDirectorySet.outputDir = testSourceDirectorySet.outputDir
generatedDirectorySet.gradleOutputDir = testSourceDirectorySet.outputDir
generatedDirectorySet.inheritedCompilerOutput = testSourceDirectorySet.isCompilerOutputPathInherited()
testSourceSet.sources.put(ExternalSystemSourceType.TEST_GENERATED, generatedDirectorySet)
}