From 7e80778956b6c2f0b43e680b9675613aeaed60a8 Mon Sep 17 00:00:00 2001 From: "Vladislav.Soroka" Date: Fri, 3 Feb 2017 15:50:42 +0300 Subject: [PATCH] IDEA-167412 Gradle 3.4-rc-1 changes compile dependencies to provided --- .../project/GradleProjectResolverUtil.java | 6 - .../GradleDependenciesImportingTest.java | 27 +++ .../util/DependencyResolverImpl.groovy | 16 +- .../ModelDependenciesBuilderImplTest.java | 168 ------------------ 4 files changed, 38 insertions(+), 179 deletions(-) delete mode 100644 plugins/gradle/tooling-extension-impl/testSources/org/jetbrains/plugins/gradle/tooling/builder/ModelDependenciesBuilderImplTest.java diff --git a/plugins/gradle/src/org/jetbrains/plugins/gradle/service/project/GradleProjectResolverUtil.java b/plugins/gradle/src/org/jetbrains/plugins/gradle/service/project/GradleProjectResolverUtil.java index 1601a8c6c035..2a5920b3e346 100644 --- a/plugins/gradle/src/org/jetbrains/plugins/gradle/service/project/GradleProjectResolverUtil.java +++ b/plugins/gradle/src/org/jetbrains/plugins/gradle/service/project/GradleProjectResolverUtil.java @@ -336,12 +336,6 @@ public class GradleProjectResolverUtil { if (prevScope.isForProductionCompile()) continue; if (prevScope.isForProductionRuntime() && currentScope.isForProductionRuntime()) continue; - // consider dependency that exists in both PROVIDED and RUNTIME scope as COMPILE one - if(prevScope == DependencyScope.PROVIDED && currentScope == DependencyScope.RUNTIME) { - if(seenDependency instanceof AbstractExternalDependency) { - ((AbstractExternalDependency)seenDependency).setScope(DependencyScope.COMPILE.name()); - } - } } dependencyMap.put(new DefaultExternalDependencyId(dependency.getId()), dependency); diff --git a/plugins/gradle/testSources/org/jetbrains/plugins/gradle/importing/GradleDependenciesImportingTest.java b/plugins/gradle/testSources/org/jetbrains/plugins/gradle/importing/GradleDependenciesImportingTest.java index 2a9ffb239d11..cd9817f21614 100644 --- a/plugins/gradle/testSources/org/jetbrains/plugins/gradle/importing/GradleDependenciesImportingTest.java +++ b/plugins/gradle/testSources/org/jetbrains/plugins/gradle/importing/GradleDependenciesImportingTest.java @@ -860,6 +860,33 @@ public class GradleDependenciesImportingTest extends GradleImportingTestCase { assertModuleLibDepScope("project", "Gradle: junit:junit:4.11", DependencyScope.PROVIDED); } + @Test + @TargetVersions("2.12+") + public void testCompileOnlyAndRuntimeScope() throws Exception { + importProject( + "apply plugin: 'java'\n" + + "dependencies {\n" + + " runtime 'org.hamcrest:hamcrest-core:1.3'\n" + + " compileOnly 'org.hamcrest:hamcrest-core:1.3'\n" + + "}" + ); + + assertModules("project", "project_main", "project_test"); + assertModuleModuleDepScope("project_test", "project_main", DependencyScope.COMPILE); + + assertModuleLibDepScope("project_main", "Gradle: org.hamcrest:hamcrest-core:1.3", DependencyScope.COMPILE); + assertModuleLibDepScope("project_test", "Gradle: org.hamcrest:hamcrest-core:1.3", DependencyScope.RUNTIME); + + importProjectUsingSingeModulePerGradleProject(); + assertModules("project"); + + if (isGradleOlderThen_3_4()) { + assertModuleLibDepScope("project", "Gradle: org.hamcrest:hamcrest-core:1.3", DependencyScope.PROVIDED, DependencyScope.RUNTIME); + } else { + assertModuleLibDepScope("project", "Gradle: org.hamcrest:hamcrest-core:1.3", DependencyScope.TEST, DependencyScope.PROVIDED, DependencyScope.RUNTIME); + } + } + @Test @TargetVersions("2.12+") public void testNonTransitiveConfiguration() throws Exception { diff --git a/plugins/gradle/tooling-extension-impl/src/org/jetbrains/plugins/gradle/tooling/util/DependencyResolverImpl.groovy b/plugins/gradle/tooling-extension-impl/src/org/jetbrains/plugins/gradle/tooling/util/DependencyResolverImpl.groovy index 2a83909e0f87..c01519266c05 100644 --- a/plugins/gradle/tooling-extension-impl/src/org/jetbrains/plugins/gradle/tooling/util/DependencyResolverImpl.groovy +++ b/plugins/gradle/tooling-extension-impl/src/org/jetbrains/plugins/gradle/tooling/util/DependencyResolverImpl.groovy @@ -235,7 +235,8 @@ class DependencyResolverImpl implements DependencyResolver { Collection dependencies = resolvedMap.get(resolve(it)) if (dependencies && !dependencies.isEmpty() && it.dependencies.isEmpty()) { runtimeDependencies.remove(it) - ((AbstractExternalDependency)it).scope = dependencies.find{true}.scope + ((AbstractExternalDependency)it).scope = compileScope + dependencies.each {((AbstractExternalDependency)it).scope = compileScope} } else { resolvedMap.put(resolve(it), it) @@ -436,6 +437,12 @@ class DependencyResolverImpl implements DependencyResolver { def providedConfigurations = new LinkedHashSet() resolvedMap = ArrayListMultimap.create() new DependencyTraverser(result).each { resolvedMap.put(resolve(it), it) } + + if (sourceSet.name == 'main' && myProject.plugins.findPlugin(WarPlugin)) { + providedConfigurations.add(myProject.configurations.findByName('providedCompile')) + providedConfigurations.add(myProject.configurations.findByName('providedRuntime')) + } + final IdeaPlugin ideaPlugin = myProject.getPlugins().findPlugin(IdeaPlugin.class) if (ideaPlugin) { def scopes = ideaPlugin.model.module.scopes @@ -443,6 +450,9 @@ class DependencyResolverImpl implements DependencyResolver { if (providedPlusScopes && providedPlusScopes.get("plus")) { // filter default 'compileClasspath' for slight optimization since it has been already processed as compile dependencies def ideaPluginProvidedConfigurations = providedPlusScopes.get("plus").findAll { it.name != "compileClasspath"} + // since gradle 3.4 'idea' plugin PROVIDED scope.plus contains 'providedCompile' and 'providedRuntime' configurations + // see https://github.com/gradle/gradle/commit/c46897ae840c5ebb32946009c83d861ee194ab96#diff-0fa13ec419e839ef2d355b7feb88b815R432 + ideaPluginProvidedConfigurations.removeAll(providedConfigurations) ideaPluginProvidedConfigurations.each { def (providedDependencies, _) = resolveDependencies(it, providedScope) new DependencyTraverser(providedDependencies).each { resolvedMap.put(resolve(it), it) } @@ -450,10 +460,6 @@ class DependencyResolverImpl implements DependencyResolver { } } } - if (sourceSet.name == 'main' && myProject.plugins.findPlugin(WarPlugin)) { - providedConfigurations.add(myProject.configurations.findByName('providedCompile')) - providedConfigurations.add(myProject.configurations.findByName('providedRuntime')) - } providedConfigurations.each { def (providedDependencies, _) = resolveDependencies(it, providedScope) new DependencyTraverser(providedDependencies).each { diff --git a/plugins/gradle/tooling-extension-impl/testSources/org/jetbrains/plugins/gradle/tooling/builder/ModelDependenciesBuilderImplTest.java b/plugins/gradle/tooling-extension-impl/testSources/org/jetbrains/plugins/gradle/tooling/builder/ModelDependenciesBuilderImplTest.java deleted file mode 100644 index dfd44d1921d3..000000000000 --- a/plugins/gradle/tooling-extension-impl/testSources/org/jetbrains/plugins/gradle/tooling/builder/ModelDependenciesBuilderImplTest.java +++ /dev/null @@ -1,168 +0,0 @@ -/* - * Copyright 2000-2014 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.jetbrains.plugins.gradle.tooling.builder; - -import com.intellij.util.Function; -import com.intellij.util.containers.ContainerUtil; -import org.gradle.tooling.model.DomainObjectSet; -import org.gradle.tooling.model.idea.IdeaDependency; -import org.gradle.tooling.model.idea.IdeaModule; -import org.gradle.tooling.model.idea.IdeaModuleDependency; -import org.gradle.tooling.model.idea.IdeaSingleEntryLibraryDependency; -import org.gradle.util.GradleVersion; -import org.jetbrains.annotations.NotNull; -import org.junit.Test; - -import java.util.Collections; -import java.util.List; -import java.util.Locale; -import java.util.Set; - -import static org.junit.Assert.*; - -/** - * @author Vladislav.Soroka - * @since 11/29/13 - */ -public class ModelDependenciesBuilderImplTest extends AbstractModelBuilderTest { - - public ModelDependenciesBuilderImplTest(@NotNull String gradleVersion) { - super(gradleVersion); - } - - @Test - public void testDefaultDependenciesModel() throws Exception { - DomainObjectSet ideaModules = allModels.getIdeaProject().getModules(); - - final int modulesSize = 3; - assertEquals(modulesSize, ideaModules.size()); - - for (IdeaModule ideaModule : ideaModules) { - if (ideaModule.getName().equals("dependencyProject") || - ideaModule.getName().equals("testDefaultDependenciesModel")) { - DomainObjectSet dependencies = ideaModule.getDependencies(); - assertTrue((dependencies.isEmpty())); - } - else if (ideaModule.getName().equals("dependentProject")) { - DomainObjectSet dependencies = ideaModule.getDependencies(); - assertEquals(1, dependencies.size()); - assertTrue(dependencies.getAt(0) instanceof IdeaModuleDependency); - IdeaModuleDependency moduleDependency = (IdeaModuleDependency)dependencies.getAt(0); - - assertEquals("dependencyProject", moduleDependency.getDependencyModule().getName()); - assertEquals("COMPILE", moduleDependency.getScope().getScope()); - - if(GradleVersion.version(gradleVersion).compareTo(GradleVersion.version("2.5")) < 0) { - assertTrue(moduleDependency.getExported()); - } else { - assertFalse(moduleDependency.getExported()); - } - - } - else { - fail(); - } - } - } - - @Test - public void testGradleIdeaPluginPlusScopesDependenciesModel() throws Exception { - DomainObjectSet ideaModules = allModels.getIdeaProject().getModules(); - - final int modulesSize = 6; - assertEquals(modulesSize, ideaModules.size()); - - for (IdeaModule ideaModule : ideaModules) { - DomainObjectSet dependencies = ideaModule.getDependencies(); - if (ideaModule.getName().equals("lib") || - ideaModule.getName().equals("testGradleIdeaPluginPlusScopesDependenciesModel")) { - assertTrue((dependencies.isEmpty())); - } - else if (ideaModule.getName().equals("api")) { - assertEquals(1, dependencies.size()); - IdeaDependency libDependency = dependencies.getAt(0); - assertEquals("provided", libDependency.getScope().getScope().toLowerCase(Locale.ENGLISH)); - assertTrue(libDependency instanceof IdeaModuleDependency); - - IdeaModuleDependency libModuleDependency = (IdeaModuleDependency)libDependency; - assertNotNull(libModuleDependency.getDependencyModule()); - assertEquals("lib", libModuleDependency.getDependencyModule().getName()); - } - else if (ideaModule.getName().equals("service")) { - assertEquals(1, dependencies.size()); - IdeaDependency apiDependency = dependencies.getAt(0); - assertEquals("compile", apiDependency.getScope().getScope().toLowerCase(Locale.ENGLISH)); - assertTrue(apiDependency instanceof IdeaModuleDependency); - - IdeaModuleDependency apiModuleDependency = (IdeaModuleDependency)apiDependency; - assertNotNull(apiModuleDependency.getDependencyModule()); - assertEquals("api", apiModuleDependency.getDependencyModule().getName()); - } - else if (ideaModule.getName().equals("withIdeaModelCustomisations")) { - - assertTrue(findLocalLibraries(dependencies, "test").isEmpty()); - - List libraryDependencies = - findLocalLibraries(dependencies, "compile"); - assertEquals(2, libraryDependencies.size()); - - IdeaSingleEntryLibraryDependency someDep = libraryDependencies.get(0); - assertEquals("compile", someDep.getScope().getScope().toLowerCase(Locale.ENGLISH)); - assertEquals("someDep.jar", someDep.getFile().getName()); - - IdeaSingleEntryLibraryDependency someTestDep = libraryDependencies.get(1); - assertEquals("compile", someTestDep.getScope().getScope().toLowerCase(Locale.ENGLISH)); - assertEquals("someTestDep.jar", someTestDep.getFile().getName()); - } - else if (ideaModule.getName().equals("withIdeRepoFileDependency")) { - assertEquals(2, dependencies.size()); - assertTrue(dependencies.getAt(0) instanceof IdeaSingleEntryLibraryDependency); - IdeaSingleEntryLibraryDependency libraryDependency = (IdeaSingleEntryLibraryDependency)dependencies.getAt(0); - - assertNotNull(libraryDependency.getGradleModuleVersion()); - assertEquals("junit", libraryDependency.getGradleModuleVersion().getName()); - assertEquals("junit", libraryDependency.getGradleModuleVersion().getGroup()); - assertEquals("4.11", libraryDependency.getGradleModuleVersion().getVersion()); - assertEquals("TEST", libraryDependency.getScope().getScope()); - assertFalse(libraryDependency.getExported()); - } - else { - fail(); - } - } - } - - @NotNull - private static List findLocalLibraries( - @NotNull final DomainObjectSet dependencies, @NotNull final String scope) { - return ContainerUtil.mapNotNull( - dependencies, - new Function() { - @Override - public IdeaSingleEntryLibraryDependency fun(IdeaDependency dependency) { - return dependency instanceof IdeaSingleEntryLibraryDependency && - scope.equals(dependency.getScope().getScope().toLowerCase(Locale.ENGLISH)) - ? (IdeaSingleEntryLibraryDependency)dependency : null; - } - } - ); - } - - @Override - protected Set getModels() { - return Collections.emptySet(); - } -}