diff --git a/sbt-coursier/src/main/scala-2.10/coursier/Tasks.scala b/sbt-coursier/src/main/scala-2.10/coursier/Tasks.scala index a7624c3bb..a33b10774 100644 --- a/sbt-coursier/src/main/scala-2.10/coursier/Tasks.scala +++ b/sbt-coursier/src/main/scala-2.10/coursier/Tasks.scala @@ -117,6 +117,31 @@ object Tasks { lazy val sv = scalaVersion.in(projectRef).get(state) lazy val sbv = scalaBinaryVersion.in(projectRef).get(state) + lazy val exclusions = { + + var anyNonSupportedExclusionRule = false + + val res = excludeDependencies + .in(projectRef) + .get(state) + .flatMap { rule => + if (rule.artifact != "*" || rule.configurations.nonEmpty) { + state.log.warn(s"Unsupported exclusion rule $rule") + anyNonSupportedExclusionRule = true + Nil + } else + Seq( + (rule.organization, FromSbt.sbtCrossVersionName(rule.name, rule.crossVersion, sv, sbv)) + ) + } + .toSet + + if (anyNonSupportedExclusionRule) + state.log.warn("Only supported exclusion rule fields: organization, name") + + res + } + for { allDependencies <- allDependenciesTask } yield { @@ -125,13 +150,20 @@ object Tasks { .map { cfg => cfg.name -> cfg.extendsConfigs.map(_.name) } .toMap - FromSbt.project( + val proj = FromSbt.project( projId, allDependencies, configMap, sv, sbv ) + + proj.copy( + dependencies = proj.dependencies.map { + case (config, dep) => + (config, dep.copy(exclusions = dep.exclusions ++ exclusions)) + } + ) } } @@ -443,24 +475,6 @@ object Tasks { FromSbt.moduleVersion(_, sv, sbv) ).toMap - var anyNonSupportedExclusionRule = false - val exclusions = excludeDependencies.value.flatMap { - rule => - if ( - rule.artifact != "*" || - rule.configurations.nonEmpty - ) { - log.warn(s"Unsupported exclusion rule $rule") - anyNonSupportedExclusionRule = true - Nil - } else - Seq((rule.organization, - FromSbt.sbtCrossVersionName(rule.name, rule.crossVersion, sv, sbv))) - }.toSet - - if (anyNonSupportedExclusionRule) - log.warn("Only supported exclusion rule fields: organization, name") - val resolvers = if (sbtClassifiers) coursierSbtResolvers.value @@ -479,10 +493,7 @@ object Tasks { val userEnabledProfiles = mavenProfiles.value val startRes = Resolution( - currentProject.dependencies.map { - case (_, dep) => - dep.copy(exclusions = dep.exclusions ++ exclusions) - }.toSet, + currentProject.dependencies.map(_._2).toSet, filter = Some(dep => !dep.optional), userActivations = if (userEnabledProfiles.isEmpty) diff --git a/sbt-coursier/src/sbt-test/sbt-coursier/exclude-dependencies/build.sbt b/sbt-coursier/src/sbt-test/sbt-coursier/exclude-dependencies/build.sbt index d3a9c89b9..471a4db63 100644 --- a/sbt-coursier/src/sbt-test/sbt-coursier/exclude-dependencies/build.sbt +++ b/sbt-coursier/src/sbt-test/sbt-coursier/exclude-dependencies/build.sbt @@ -1,5 +1,9 @@ scalaVersion := "2.11.8" +organization := "io.get-coursier.test" +name := "sbt-coursier-exclude-dependencies" +version := "0.1.0-SNAPSHOT" + coursierCachePolicies := { if (sys.props("os.name").startsWith("Windows")) coursierCachePolicies.value diff --git a/sbt-coursier/src/sbt-test/sbt-coursier/exclude-dependencies/coursier b/sbt-coursier/src/sbt-test/sbt-coursier/exclude-dependencies/coursier new file mode 100755 index 000000000..6cada6f8b Binary files /dev/null and b/sbt-coursier/src/sbt-test/sbt-coursier/exclude-dependencies/coursier differ diff --git a/sbt-coursier/src/sbt-test/sbt-coursier/exclude-dependencies/src/main/scala/Main.scala b/sbt-coursier/src/sbt-test/sbt-coursier/exclude-dependencies/src/main/scala/Main.scala index a2d8f92e8..1bc056610 100644 --- a/sbt-coursier/src/sbt-test/sbt-coursier/exclude-dependencies/src/main/scala/Main.scala +++ b/sbt-coursier/src/sbt-test/sbt-coursier/exclude-dependencies/src/main/scala/Main.scala @@ -15,9 +15,18 @@ object Main extends App { val argonautFound = classFound("argonaut.Json") val argonautShapelessFound = classFound("argonaut.derive.MkEncodeJson") - assert(argonautShapelessFound) - assert(!shapelessFound) - assert(!argonautFound) + assert( + argonautShapelessFound, + "Expected to find class from argonaut-shapeless" + ) + assert( + !shapelessFound, + "Expected not to find classes from shapeless" + ) + assert( + !argonautFound, + "Expected not to find classes from argonaut" + ) Files.write(new File("output").toPath, "OK".getBytes("UTF-8")) } diff --git a/sbt-coursier/src/sbt-test/sbt-coursier/exclude-dependencies/test b/sbt-coursier/src/sbt-test/sbt-coursier/exclude-dependencies/test index 2182f57b0..ad685c023 100644 --- a/sbt-coursier/src/sbt-test/sbt-coursier/exclude-dependencies/test +++ b/sbt-coursier/src/sbt-test/sbt-coursier/exclude-dependencies/test @@ -1,3 +1,5 @@ $ delete output > run $ exists output +> publishLocal +$ exec java -jar coursier launch io.get-coursier.test:sbt-coursier-exclude-dependencies_2.11:0.1.0-SNAPSHOT diff --git a/sbt-shading/src/sbt-test/sbt-shading/exclude-dependencies/build.sbt b/sbt-shading/src/sbt-test/sbt-shading/exclude-dependencies/build.sbt new file mode 100644 index 000000000..091b971ab --- /dev/null +++ b/sbt-shading/src/sbt-test/sbt-shading/exclude-dependencies/build.sbt @@ -0,0 +1,17 @@ + +enablePlugins(coursier.ShadingPlugin) +shadingNamespace := "test.shaded" +shadeNamespaces += "argonaut" + +libraryDependencies ++= Seq( + "com.github.alexarchambault" %% "argonaut-shapeless_6.2" % "1.2.0-M5" % "shaded", + // directly depending on that one for it not to be shaded + "org.scala-lang" % "scala-reflect" % scalaVersion.value +) + +excludeDependencies += SbtExclusionRule("com.chuusai", "shapeless_2.11") + +scalaVersion := "2.11.8" +organization := "io.get-coursier.test" +name := "shading-exclude-dependencies" +version := "0.1.0-SNAPSHOT" diff --git a/sbt-shading/src/sbt-test/sbt-shading/exclude-dependencies/coursier b/sbt-shading/src/sbt-test/sbt-shading/exclude-dependencies/coursier new file mode 100755 index 000000000..13c8a2b55 Binary files /dev/null and b/sbt-shading/src/sbt-test/sbt-shading/exclude-dependencies/coursier differ diff --git a/sbt-shading/src/sbt-test/sbt-shading/exclude-dependencies/project/plugins.sbt b/sbt-shading/src/sbt-test/sbt-shading/exclude-dependencies/project/plugins.sbt new file mode 100644 index 000000000..8f83e814d --- /dev/null +++ b/sbt-shading/src/sbt-test/sbt-shading/exclude-dependencies/project/plugins.sbt @@ -0,0 +1,31 @@ +{ + val pluginVersion = sys.props.getOrElse( + "plugin.version", + throw new RuntimeException( + """|The system property 'plugin.version' is not defined. + |Specify this property using the scriptedLaunchOpts -D.""".stripMargin + ) + ) + + addSbtPlugin("io.get-coursier" % "sbt-shading" % pluginVersion) +} + +// for the locally publish jarjar +resolvers += Resolver.mavenLocal + +val coursierJarjarVersion = "1.0.1-coursier-SNAPSHOT" + +def coursierJarjarFoundInM2 = + (file(sys.props("user.home")) / s".m2/repository/org/anarres/jarjar/jarjar-core/$coursierJarjarVersion").exists() + +def jarjarVersion = + if (coursierJarjarFoundInM2) + coursierJarjarVersion + else + sys.error( + "Ad hoc jarjar version not found. Run\n" + + " git clone https://github.com/alexarchambault/jarjar.git && cd jarjar && git checkout 249c8dbb970f8 && ./gradlew install\n" + + "to run this test" + ) + +libraryDependencies += "org.anarres.jarjar" % "jarjar-core" % jarjarVersion diff --git a/sbt-shading/src/sbt-test/sbt-shading/exclude-dependencies/src/main/scala/Main.scala b/sbt-shading/src/sbt-test/sbt-shading/exclude-dependencies/src/main/scala/Main.scala new file mode 100644 index 000000000..80584c442 --- /dev/null +++ b/sbt-shading/src/sbt-test/sbt-shading/exclude-dependencies/src/main/scala/Main.scala @@ -0,0 +1,44 @@ +import java.io.File +import java.nio.file.Files + +import argonaut._ + +object Main extends App { + + def excludeCheck(): Unit = { + val className = "shapeless.HList" + val loader = Thread.currentThread.getContextClassLoader + + val found = + try { + loader.loadClass(className) + true + } catch { + case _: java.lang.ClassNotFoundException => false + } + + assert(!found, s"Expected class $className not to be found") + } + + excludeCheck() + + val expectedClassName = + if (args.contains("--shaded")) + "test.shaded.argonaut.Json" + else + // Don't use the literal "argonaut.Json", that seems to get + // changed to "test.shaded.argonaut.Json" by shading + "argonaut" + '.' + "Json" + + val className = classOf[Json].getName + + Console.err.println(s"Expected class name: $expectedClassName") + Console.err.println(s"Class name: $className") + + if (className != expectedClassName) + sys.error(s"Expected class name $expectedClassName, got $className") + + val msg = Json.obj().nospaces + + Files.write(new File("output").toPath, msg.getBytes("UTF-8")) +} diff --git a/sbt-shading/src/sbt-test/sbt-shading/exclude-dependencies/test b/sbt-shading/src/sbt-test/sbt-shading/exclude-dependencies/test new file mode 100644 index 000000000..8665a512d --- /dev/null +++ b/sbt-shading/src/sbt-test/sbt-shading/exclude-dependencies/test @@ -0,0 +1,9 @@ +$ delete output +> run +$ exists output +> publishLocal +$ exec java -jar coursier launch io.get-coursier.test:shading-exclude-dependencies_2.11:0.1.0-SNAPSHOT +-$ exec java -jar coursier launch io.get-coursier.test:shading-exclude-dependencies_2.11:0.1.0-SNAPSHOT -- --shaded +> shading:publishLocal +-$ exec java -jar coursier launch io.get-coursier.test:shading-exclude-dependencies_2.11:0.1.0-SNAPSHOT +$ exec java -jar coursier launch io.get-coursier.test:shading-exclude-dependencies_2.11:0.1.0-SNAPSHOT -- --shaded