Skip to content

Commit

Permalink
Add JavaModule.artifactTypes (#3703)
Browse files Browse the repository at this point in the history
Allowing users to ask for more artifact types to be fetched and put in
the classpath
  • Loading branch information
alexarchambault authored Oct 10, 2024
1 parent 01ab379 commit 80d1640
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ object DocAnnotationsTests extends UtestIntegrationTestSuite {

assert(
globMatches(
"""core.ivyDepsTree(JavaModule.scala:884)
"""core.ivyDepsTree(JavaModule.scala:893)
| Command to print the transitive dependency tree to STDOUT.
|
| --inverse Invert the tree representation, so that the root is on the bottom val
Expand Down
30 changes: 28 additions & 2 deletions main/util/src/mill/util/CoursierSupport.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import coursier.error.ResolutionError.CantDownloadModule
import coursier.params.ResolutionParams
import coursier.parse.RepositoryParser
import coursier.util.Task
import coursier.{Artifacts, Classifier, Dependency, Repository, Resolution, Resolve}
import coursier.{Artifacts, Classifier, Dependency, Repository, Resolution, Resolve, Type}
import mill.api.Loose.Agg
import mill.api.{Ctx, PathRef, Result}

Expand Down Expand Up @@ -44,7 +44,8 @@ trait CoursierSupport {
customizer: Option[Resolution => Resolution] = None,
ctx: Option[mill.api.Ctx.Log] = None,
coursierCacheCustomizer: Option[FileCache[Task] => FileCache[Task]] = None,
resolveFilter: os.Path => Boolean = _ => true
resolveFilter: os.Path => Boolean = _ => true,
artifactTypes: Option[Set[Type]] = None
): Result[Agg[PathRef]] = {
def isLocalTestDep(dep: Dependency): Option[Seq[PathRef]] = {
val org = dep.module.organization.value
Expand Down Expand Up @@ -86,6 +87,7 @@ trait CoursierSupport {
if (sources) Set(Classifier("sources"))
else Set.empty
)
.withArtifactTypesOpt(artifactTypes)
.eitherResult()

artifactsResultOrError match {
Expand All @@ -112,6 +114,30 @@ trait CoursierSupport {
}
}

@deprecated("Use the override accepting artifactTypes", "Mill after 0.12.0-RC3")
def resolveDependencies(
repositories: Seq[Repository],
deps: IterableOnce[Dependency],
force: IterableOnce[Dependency],
sources: Boolean,
mapDependencies: Option[Dependency => Dependency],
customizer: Option[Resolution => Resolution],
ctx: Option[mill.api.Ctx.Log],
coursierCacheCustomizer: Option[FileCache[Task] => FileCache[Task]],
resolveFilter: os.Path => Boolean
): Result[Agg[PathRef]] =
resolveDependencies(
repositories,
deps,
force,
sources,
mapDependencies,
customizer,
ctx,
coursierCacheCustomizer,
resolveFilter
)

@deprecated(
"Prefer resolveDependenciesMetadataSafe instead, which returns a Result instead of throwing exceptions",
"0.12.0"
Expand Down
28 changes: 25 additions & 3 deletions scalalib/src/mill/scalalib/CoursierModule.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package mill.scalalib

import coursier.cache.FileCache
import coursier.{Dependency, Repository, Resolve}
import coursier.{Dependency, Repository, Resolve, Type}
import coursier.core.Resolution
import mill.define.Task
import mill.api.PathRef
Expand Down Expand Up @@ -48,21 +48,34 @@ trait CoursierModule extends mill.Module {
*
* @param deps The dependencies to resolve.
* @param sources If `true`, resolve source dependencies instead of binary dependencies (JARs).
* @param artifactTypes If non-empty, pull the passed artifact types rather than the default ones from coursier
* @return The [[PathRef]]s to the resolved files.
*/
def resolveDeps(deps: Task[Agg[BoundDep]], sources: Boolean = false): Task[Agg[PathRef]] =
def resolveDeps(
deps: Task[Agg[BoundDep]],
sources: Boolean = false,
artifactTypes: Option[Set[Type]] = None
): Task[Agg[PathRef]] =
Task.Anon {
Lib.resolveDependencies(
repositories = repositoriesTask(),
deps = deps(),
sources = sources,
artifactTypes = artifactTypes,
mapDependencies = Some(mapDependencies()),
customizer = resolutionCustomizer(),
coursierCacheCustomizer = coursierCacheCustomizer(),
ctx = Some(implicitly[mill.api.Ctx.Log])
)
}

@deprecated("Use the override accepting artifactTypes", "Mill after 0.12.0-RC3")
def resolveDeps(
deps: Task[Agg[BoundDep]],
sources: Boolean
): Task[Agg[PathRef]] =
resolveDeps(deps, sources, None)

/**
* Map dependencies before resolving them.
* Override this to customize the set of dependencies.
Expand Down Expand Up @@ -134,18 +147,27 @@ object CoursierModule {

def resolveDeps[T: CoursierModule.Resolvable](
deps: IterableOnce[T],
sources: Boolean = false
sources: Boolean = false,
artifactTypes: Option[Set[coursier.Type]] = None
): Agg[PathRef] = {
Lib.resolveDependencies(
repositories = repositories,
deps = deps.map(implicitly[CoursierModule.Resolvable[T]].bind(_, bind)),
sources = sources,
artifactTypes = artifactTypes,
mapDependencies = mapDependencies,
customizer = customizer,
coursierCacheCustomizer = coursierCacheCustomizer,
ctx = ctx
).getOrThrow
}

@deprecated("Use the override accepting artifactTypes", "Mill after 0.12.0-RC3")
def resolveDeps[T: CoursierModule.Resolvable](
deps: IterableOnce[T],
sources: Boolean
): Agg[PathRef] =
resolveDeps(deps, sources, None)
}

sealed trait Resolvable[T] {
Expand Down
13 changes: 11 additions & 2 deletions scalalib/src/mill/scalalib/JavaModule.scala
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package mill
package scalalib

import coursier.Repository
import coursier.core.Resolution
import coursier.parse.JavaOrScalaModule
import coursier.parse.ModuleParser
import coursier.util.ModuleMatcher
import coursier.{Repository, Type}
import mainargs.{Flag, arg}
import mill.Agg
import mill.api.{Ctx, JarManifest, MillException, PathRef, Result, internal}
Expand Down Expand Up @@ -155,6 +155,12 @@ trait JavaModule
*/
def runIvyDeps: T[Agg[Dep]] = Task { Agg.empty[Dep] }

/**
* Default artifact types to fetch and put in the classpath. Add extra types
* here if you'd like fancy artifact extensions to be fetched.
*/
def artifactTypes: T[Set[Type]] = Task { coursier.core.Resolution.defaultTypes }

/**
* Options to pass to the java compiler
*/
Expand Down Expand Up @@ -551,7 +557,10 @@ trait JavaModule
}

def resolvedRunIvyDeps: T[Agg[PathRef]] = Task {
defaultResolver().resolveDeps(runIvyDeps().map(bindDependency()) ++ transitiveIvyDeps())
defaultResolver().resolveDeps(
runIvyDeps().map(bindDependency()) ++ transitiveIvyDeps(),
artifactTypes = Some(artifactTypes())
)
}

/**
Expand Down
29 changes: 27 additions & 2 deletions scalalib/src/mill/scalalib/Lib.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package mill
package scalalib

import coursier.util.Task
import coursier.{Dependency, Repository, Resolution}
import coursier.{Dependency, Repository, Resolution, Type}
import mill.api.{Ctx, Loose, PathRef, Result}
import mill.main.BuildInfo
import mill.main.client.EnvVars
Expand Down Expand Up @@ -89,21 +89,46 @@ object Lib {
ctx: Option[Ctx.Log] = None,
coursierCacheCustomizer: Option[
coursier.cache.FileCache[Task] => coursier.cache.FileCache[Task]
] = None
] = None,
artifactTypes: Option[Set[Type]] = None
): Result[Agg[PathRef]] = {
val depSeq = deps.iterator.toSeq
mill.util.Jvm.resolveDependencies(
repositories = repositories,
deps = depSeq.map(_.dep),
force = depSeq.filter(_.force).map(_.dep),
sources = sources,
artifactTypes = artifactTypes,
mapDependencies = mapDependencies,
customizer = customizer,
ctx = ctx,
coursierCacheCustomizer = coursierCacheCustomizer
).map(_.map(_.withRevalidateOnce))
}

@deprecated("Use the override accepting artifactTypes", "Mill after 0.12.0-RC3")
def resolveDependencies(
repositories: Seq[Repository],
deps: IterableOnce[BoundDep],
sources: Boolean,
mapDependencies: Option[Dependency => Dependency],
customizer: Option[coursier.core.Resolution => coursier.core.Resolution],
ctx: Option[Ctx.Log],
coursierCacheCustomizer: Option[
coursier.cache.FileCache[Task] => coursier.cache.FileCache[Task]
]
): Result[Agg[PathRef]] =
resolveDependencies(
repositories,
deps,
sources,
mapDependencies,
customizer,
ctx,
coursierCacheCustomizer,
None
)

def scalaCompilerIvyDeps(scalaOrganization: String, scalaVersion: String): Loose.Agg[Dep] =
if (ZincWorkerUtil.isDotty(scalaVersion))
Agg(
Expand Down
4 changes: 2 additions & 2 deletions scalalib/src/mill/scalalib/ZincWorkerModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ trait ZincWorkerModule extends mill.Module with OfflineSupportModule { self: Cou
val bridgeJar = resolveDependencies(
repositories,
Seq(bridgeDep.bindDep("", "", "")),
useSources,
Some(overrideScalaLibrary(scalaVersion, scalaOrganization))
sources = useSources,
mapDependencies = Some(overrideScalaLibrary(scalaVersion, scalaOrganization))
).map(deps =>
ZincWorkerUtil.grepJar(deps, bridgeName, bridgeVersion, useSources)
)
Expand Down

0 comments on commit 80d1640

Please sign in to comment.