Skip to content

Commit

Permalink
Add maven dependency fetcher support for MAVEN_CLI_OPTS flag used by …
Browse files Browse the repository at this point in the history
…gitlab (#4631)
  • Loading branch information
johannescoetzee committed Jun 3, 2024
1 parent 49f6b5f commit 8999ce1
Showing 1 changed file with 17 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,23 @@ import scala.util.{Failure, Success}
object MavenDependencies {
private val logger = LoggerFactory.getLogger(getClass)

private val MavenCliOpts = "MAVEN_CLI_OPTS"
// we can't use -Dmdep.outputFile because that keeps overwriting its own output for each sub-project it's running for
// also separate this from fetchCommandWithOpts to log a version that clearly separates options we provide from
// options specified by the user via the MAVEN_CLI_OPTS environment variable, while also making it clear that this
// environment variable is being considered.
private val fetchCommand =
"mvn --fail-never -B dependency:build-classpath -DincludeScope=compile -Dorg.slf4j.simpleLogger.defaultLogLevel=info -Dorg.slf4j.simpleLogger.logFile=System.out"
s"mvn $$$MavenCliOpts --fail-never -B dependency:build-classpath -DincludeScope=compile -Dorg.slf4j.simpleLogger.defaultLogLevel=info -Dorg.slf4j.simpleLogger.logFile=System.out"

private val fetchCommandWithOpts = {
// These options suppress output, so if they're provided we won't get any results.
// "-q" and "--quiet" are the only ones that would realistically be used.
val optionsToStrip = Set("-h", "--help", "-q", "--quiet", "-v", "--version")

val mavenOpts = Option(System.getenv(MavenCliOpts)).getOrElse("")
val mavenOptsStripped = mavenOpts.split(raw"\s").filterNot(optionsToStrip.contains).mkString(" ")
fetchCommand.replace(s"$$$MavenCliOpts", mavenOptsStripped)
}

private def logErrors(output: String): Unit = {

Expand All @@ -25,8 +40,7 @@ object MavenDependencies {
}

private[dependency] def get(projectDir: Path): Option[collection.Seq[String]] = {
// we can't use -Dmdep.outputFile because that keeps overwriting its own output for each sub-project it's running for
val lines = ExternalCommand.run(fetchCommand, projectDir.toString) match {
val lines = ExternalCommand.run(fetchCommandWithOpts, projectDir.toString) match {
case Success(lines) =>
if (lines.contains("[INFO] Build failures were ignored.")) {
logErrors(lines.mkString(System.lineSeparator()))
Expand Down

0 comments on commit 8999ce1

Please sign in to comment.