Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 40 additions & 8 deletions libs/init/maven/src/mill/main/maven/MillMavenBuildGenMain.scala
Original file line number Diff line number Diff line change
Expand Up @@ -81,22 +81,27 @@ object MillMavenBuildGenMain {
def moduleDeps(scope: String) = mavenModuleDeps.collect {
case dep if dep.getScope == scope => moduleDepLookup(dep)
}
val (effectiveBomMvnDeps, effectiveDepManagement, effectiveBomModuleDeps) =
Option(model.getDependencyManagement).fold((Nil, Nil, Nil)) { dm =>
collectDependencyManagement(dm, toMvnOrModuleDep, moduleDepLookup)
}

val isSpringParentProject = isSpringBootProject(model)
val springBootVersion = detectSpringBootVersion(model)

val quarkusVersionOpt = detectQuarkusPluginVersion(model)

val (bomMvnDeps, depManagement, bomModuleDeps) =
Option(model.getDependencyManagement).fold((Nil, Nil, Nil)) { dm =>
val (bomDeps, deps) = dm.getDependencies.asScala.toSeq.partition(isBom)
val (bomMvnDeps, bomModuleDeps) = bomDeps.partitionMap(toMvnOrModuleDep)
val depManagement = deps.collect {
case dep if !moduleDepLookup.isDefinedAt(dep) => toMvnDep(dep)
}
(bomMvnDeps, depManagement, bomModuleDeps)
val (rawBomMvnDeps, rawDepManagement, rawBomModuleDeps) =
Option(result.getRawModel.getDependencyManagement).fold((Nil, Nil, Nil)) { dm =>
collectDependencyManagement(dm, toMvnOrModuleDep, moduleDepLookup)
}

val (bomMvnDeps, depManagement, bomModuleDeps) = selectDependencyManagement(
isSpringParentProject = isSpringParentProject,
effective = (effectiveBomMvnDeps, effectiveDepManagement, effectiveBomModuleDeps),
raw = (rawBomMvnDeps, rawDepManagement, rawBomModuleDeps)
)

mainModule = mainModule.copy(
imports = "mill.javalib.*" +: mainModule.imports,
supertypes = "MavenModule" +: mainModule.supertypes,
Expand Down Expand Up @@ -212,10 +217,14 @@ object MillMavenBuildGenMain {

private val SpringBootGroupId = "org.springframework.boot"
private val SpringBootParentArtifactId = "spring-boot-starter-parent"
private val SpringBootDependenciesArtifactId = "spring-boot-dependencies"

private def isSpringBootParent(parent: Parent): Boolean =
parent.getGroupId == SpringBootGroupId && parent.getArtifactId == SpringBootParentArtifactId

private def isSpringBootDependenciesBom(dep: MvnDep): Boolean =
dep.organization == SpringBootGroupId && dep.name == SpringBootDependenciesArtifactId

/**
* Detect if the project is a Spring Boot project by checking if it inherits from spring-boot-starter-parent.
*/
Expand All @@ -224,6 +233,29 @@ object MillMavenBuildGenMain {

private def nonEmpty(value: String): Option[String] = Option(value).filter(_.nonEmpty)

private def collectDependencyManagement(
dm: DependencyManagement,
toMvnOrModuleDep: Dependency => Either[MvnDep, ModuleDep],
moduleDepLookup: PartialFunction[Dependency, ModuleDep]
): (Seq[MvnDep], Seq[MvnDep], Seq[ModuleDep]) = {
val (bomDeps, deps) = dm.getDependencies.asScala.toSeq.partition(isBom)
val (bomMvnDeps, bomModuleDeps) = bomDeps.partitionMap(toMvnOrModuleDep)
val depManagement = deps.collect {
case dep if !moduleDepLookup.isDefinedAt(dep) => toMvnDep(dep)
}
(bomMvnDeps, depManagement, bomModuleDeps)
}

private def selectDependencyManagement(
isSpringParentProject: Boolean,
effective: (Seq[MvnDep], Seq[MvnDep], Seq[ModuleDep]),
raw: (Seq[MvnDep], Seq[MvnDep], Seq[ModuleDep])
): (Seq[MvnDep], Seq[MvnDep], Seq[ModuleDep]) =
if (isSpringParentProject) {
// Effective model pulls in a very large inherited set from spring-boot-dependencies BOM.
(raw._1.filterNot(isSpringBootDependenciesBom), raw._2, raw._3)
} else effective

/** Detect Spring Boot platform version from spring-boot-starter-parent. */
private def detectSpringBootVersion(model: Model): Option[String] = {
Option(model.getParent)
Expand Down
Loading
Loading