Skip to content

Commit

Permalink
Tweaking
Browse files Browse the repository at this point in the history
  • Loading branch information
alexarchambault committed Jan 7, 2025
1 parent 273aa5f commit 8ebcb9e
Show file tree
Hide file tree
Showing 10 changed files with 177 additions and 22 deletions.
19 changes: 12 additions & 7 deletions bsp/worker/src/mill/bsp/worker/MillBuildServer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -337,8 +337,13 @@ private class MillBuildServer(
tasks = { case m: JavaModule =>
Task.Anon {
(
m.compileIvyDeps(),
m.mandatoryIvyDeps() ++ m.ivyDeps(),
// full list of dependencies, including transitive ones
m.defaultResolver().allDeps(
Seq(
m.coursierDependency.withConfiguration(coursier.core.Configuration.provided),
m.coursierDependency
)
),
m.unmanagedClasspath()
)
}
Expand All @@ -349,14 +354,14 @@ private class MillBuildServer(
state,
id,
m: JavaModule,
(compileIvyDeps, ivyDeps, unmanagedClasspath)
(ivyDeps, unmanagedClasspath)
) =>
val ivy = compileIvyDeps ++ ivyDeps
val deps = ivy.map { dep =>
// TODO: add data with "maven" data kind using a ...
val deps = ivyDeps.collect {
case dep if dep.module.organization != JavaModule.internalOrg =>
// TODO: add data with "maven" data kind using a ...
// MavenDependencyModule

new DependencyModule(dep.dep.module.repr, dep.dep.version)
new DependencyModule(dep.module.repr, dep.version)
}
val unmanaged = unmanagedClasspath.map { dep =>
new DependencyModule(s"unmanaged-${dep.path.last}", "")
Expand Down
3 changes: 3 additions & 0 deletions build.mill
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,9 @@ trait MillStableScalaModule extends MillPublishScalaModule with Mima {
// Overrides added for new methods, ought to be safe
ProblemFilter.exclude[ReversedMissingMethodProblem]("mill.scalalib.JavaModule.mill$scalalib$JavaModule$$super$internalRepositories"),
ProblemFilter.exclude[ReversedMissingMethodProblem]("mill.scalanativelib.ScalaNativeModule.mill$scalanativelib$ScalaNativeModule$$super$coursierProject"),
ProblemFilter.exclude[ReversedMissingMethodProblem]("mill.scalalib.JavaModule#JavaModuleTests.mill$scalalib$JavaModule$JavaModuleTests$$super$extraBomIvyDeps"),
ProblemFilter.exclude[ReversedMissingMethodProblem]("mill.scalalib.JavaModule#JavaModuleTests.mill$scalalib$JavaModule$JavaModuleTests$$super$extraDepManagement"),
ProblemFilter.exclude[ReversedMissingMethodProblem]("mill.scalalib.PublishModule.mill$scalalib$PublishModule$$super$bomModuleDeps"),

// https://github.com/com-lihaoyi/mill/pull/3503
ProblemFilter.exclude[ReversedMissingMethodProblem](
Expand Down
4 changes: 2 additions & 2 deletions docs/modules/ROOT/pages/fundamentals/library-deps.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ whose Maven coordinates are `com.google.cloud:libraries-bom:26.50.0`

include::partial$example/fundamentals/library-deps/bom-1-external-bom.adoc[]

=== Dependency management task
=== Managed BOMs

include::partial$example/fundamentals/library-deps/bom-2-dependency-management.adoc[]
include::partial$example/fundamentals/library-deps/bom-2-managed.adoc[]

== Searching For Dependency Updates

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,56 @@ object baz extends JavaModule {

// Here, given that grpc-protobuf is fetched during dependency resolution,
// `com.google.protobuf:protobuf-java` is excluded from it because of the dependency management.

// One manage and publish BOM modules from Mill, by using `BomModule`:

object myBom extends BomModule {
def bomIvyDeps = Agg(
ivy"com.google.protobuf:protobuf-bom:4.28.1"
)
def depManagement = Agg(
ivy"io.grpc:grpc-protobuf:1.67.1"
)
}

object bomUser extends JavaModule {
def bomModuleDeps = Seq(
myBom
)
def ivyDeps = Agg(
ivy"io.grpc:grpc-protobuf"
)
}

// These BOM modules can published, resulting in a Maven module with
// packaging type `pom`, usable as a BOM from other build tools:

object myPublishedBom extends BomModule with MyPublishModule {
def bomIvyDeps = Agg(
ivy"com.google.protobuf:protobuf-bom:4.28.1"
)
def depManagement = Agg(
ivy"io.grpc:grpc-protobuf:1.67.1"
)
}

object publishedBomUser extends JavaModule with MyPublishModule {
def bomModuleDeps = Seq(
myPublishedBom
)
def ivyDeps = Agg(
ivy"io.grpc:grpc-protobuf"
)
}

trait MyPublishModule extends PublishModule {
def pomSettings = PomSettings(
description = "My Project",
organization = "com.lihaoyi.mill-examples",
url = "https://github.com/com-lihaoyi/mill",
licenses = Seq(License.MIT),
versionControl = VersionControl.github("com-lihaoyi", "mill"),
developers = Seq(Developer("me", "Me", "https://github.com/me"))
)
def publishVersion = "0.1.0"
}
26 changes: 26 additions & 0 deletions scalalib/src/mill/scalalib/CoursierModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,32 @@ object CoursierModule {
)
)
}

/**
* All dependencies pulled by the passed dependencies
*
* @param deps root dependencies
* @return full - ordered - list of dependencies pulled by `deps`
*/
def allDeps[T: CoursierModule.Resolvable](
deps: IterableOnce[T]
): Seq[coursier.core.Dependency] = {
val deps0 = deps
.map(implicitly[CoursierModule.Resolvable[T]].bind(_, bind))
.iterator.toSeq
val res = Lib.resolveDependenciesMetadataSafe(
repositories = repositories,
deps = deps0,
mapDependencies = mapDependencies,
customizer = customizer,
coursierCacheCustomizer = coursierCacheCustomizer,
ctx = ctx,
resolutionParams = ResolutionParams(),
boms = Nil
).getOrThrow

res.orderedDependencies
}
}

sealed trait Resolvable[T] {
Expand Down
35 changes: 22 additions & 13 deletions scalalib/src/mill/scalalib/JavaModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -479,11 +479,11 @@ trait JavaModule
// where eval'ing a Task would be impractical or not allowed
cs.Dependency(
cs.Module(
coursier.core.Organization("mill-internal"),
JavaModule.internalOrg,
coursier.core.ModuleName(millModuleSegments.parts.mkString("-")),
Map.empty
),
"0+mill-internal"
JavaModule.internalVersion
).withConfiguration(cs.Configuration.compile)

/**
Expand Down Expand Up @@ -533,7 +533,10 @@ trait JavaModule
// We pull their compile scope when our compile scope is asked,
// and pull their runtime scope when our runtime scope is asked.
Seq(
(cs.Configuration.compile, modDep.coursierDependency),
(
cs.Configuration.compile,
modDep.coursierDependency.withConfiguration(cs.Configuration.compile)
),
(
cs.Configuration.runtime,
modDep.coursierDependency.withConfiguration(cs.Configuration.runtime)
Expand All @@ -543,17 +546,17 @@ trait JavaModule
compileModuleDepsChecked.map { modDep =>
// Compile-only (aka provided) dependencies
// We pull their compile scope when our provided scope is asked (see scopes above)
(cs.Configuration.provided, modDep.coursierDependency)
(
cs.Configuration.provided,
modDep.coursierDependency.withConfiguration(cs.Configuration.compile)
)
} ++
runModuleDepsChecked.map { modDep =>
// Runtime dependencies
// We pull their runtime scope when our runtime scope is pulled
(
cs.Configuration.runtime,
modDep.coursierDependency.withConfiguration(
if (modDep.coursierDependency.configuration.isEmpty) cs.Configuration.runtime
else modDep.coursierDependency.configuration
)
modDep.coursierDependency.withConfiguration(cs.Configuration.runtime)
)
}

Expand All @@ -564,23 +567,21 @@ trait JavaModule
// We pull their compile scope when our compile scope is asked,
// and pull their runtime scope when our runtime scope is asked.
Seq(
(cs.Configuration.compile, dep),
(cs.Configuration.compile, dep.withConfiguration(cs.Configuration.compile)),
(cs.Configuration.runtime, dep.withConfiguration(cs.Configuration.runtime))
)
} ++
compileIvyDeps().map(bindDependency()).map(_.dep).map { dep =>
// Compile-only (aka provided) dependencies, like above
// We pull their compile scope when our provided scope is asked (see scopes above)
(cs.Configuration.provided, dep)
(cs.Configuration.provided, dep.withConfiguration(cs.Configuration.compile))
} ++
runIvyDeps().map(bindDependency()).map(_.dep).map { dep =>
// Runtime dependencies, like above
// We pull their runtime scope when our runtime scope is pulled
(
cs.Configuration.runtime,
dep.withConfiguration(
if (dep.configuration.isEmpty) cs.Configuration.runtime else dep.configuration
)
dep.withConfiguration(cs.Configuration.runtime)
)
} ++
allBomDeps().map { bomDep =>
Expand Down Expand Up @@ -1638,8 +1639,16 @@ object JavaModule {
// Mill modules' artifacts are handled by Mill itself
Nil
}

private[mill] def internalOrg = coursier.core.Organization("mill-internal")
private[mill] def internalVersion = "0+mill-internal"
}

/**
* A module that consists solely of dependency management
*
* To be used by other modules via `JavaModule#bomModuleDeps`
*/
trait BomModule extends JavaModule {
def compile: T[CompilationResult] = Task {
val sources = allSourceFiles()
Expand Down
8 changes: 8 additions & 0 deletions scalalib/src/mill/scalalib/PublishModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ trait PublishModule extends JavaModule { outer =>
)
}

override def bomModuleDeps: Seq[BomModule with PublishModule] = super.bomModuleDeps.map {
case m: BomModule with PublishModule => m
case other =>
throw new Exception(
s"PublishModule bomModuleDeps need to be also PublishModules. $other is not a PublishModule"
)
}

/**
* The packaging type. See [[PackagingType]] for specially handled values.
*/
Expand Down
15 changes: 15 additions & 0 deletions scalalib/src/mill/scalalib/publish/Ivy.scala
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,21 @@ object Ivy {
head + pp.format(xml).replaceAll(">", ">")
}

// bin-compat shim
def apply(
artifact: Artifact,
dependencies: Agg[Dependency],
extras: Seq[PublishInfo],
overrides: Seq[Override]
): String =
apply(
artifact,
dependencies,
extras,
overrides,
hasJar = true
)

// bin-compat shim
def apply(
artifact: Artifact,
Expand Down
19 changes: 19 additions & 0 deletions scalalib/src/mill/scalalib/publish/LocalIvyPublisher.scala
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,25 @@ class LocalIvyPublisher(localIvyRepo: os.Path) {
}
}

// bin-compat shim
def publishLocal(
jar: os.Path,
sourcesJar: os.Path,
docJar: os.Path,
pom: os.Path,
ivy: os.Path,
artifact: Artifact,
extras: Seq[PublishInfo]
)(implicit ctx: Ctx.Log): Seq[os.Path] =
publishLocal(
Some(jar),
Some(sourcesJar),
Some(docJar),
pom,
Right(ivy),
artifact,
extras
)
}

object LocalIvyPublisher
Expand Down
17 changes: 17 additions & 0 deletions scalalib/src/mill/scalalib/publish/LocalM2Publisher.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,21 @@ class LocalM2Publisher(m2Repo: os.Path) {
}
}

// bin-compat shim
def publish(
jar: os.Path,
sourcesJar: os.Path,
docJar: os.Path,
pom: os.Path,
artifact: Artifact,
extras: Seq[PublishInfo]
)(implicit ctx: Ctx.Log): Seq[os.Path] =
publish(
Some(jar),
Some(sourcesJar),
Some(docJar),
pom,
artifact,
extras
)
}

0 comments on commit 8ebcb9e

Please sign in to comment.