From c8882142ad498dc89e90941dee445bfb9593b7c9 Mon Sep 17 00:00:00 2001 From: Michael Liarakos Date: Fri, 5 Jan 2024 00:13:45 -0500 Subject: [PATCH 1/3] fix launcher configuration --- .../bloop/integrations/maven/BloopMojo.java | 1 + src/main/java/scala_maven/AppLauncher.java | 7 ++ .../maven/MojoImplementation.scala | 12 +-- src/test/resources/launcher/pom.xml | 78 +++++++++++++++++++ .../maven/MavenConfigGenerationTest.scala | 29 +++++++ 5 files changed, 122 insertions(+), 5 deletions(-) create mode 100644 src/test/resources/launcher/pom.xml diff --git a/src/main/java/bloop/integrations/maven/BloopMojo.java b/src/main/java/bloop/integrations/maven/BloopMojo.java index f4ac8a4..49952ae 100644 --- a/src/main/java/bloop/integrations/maven/BloopMojo.java +++ b/src/main/java/bloop/integrations/maven/BloopMojo.java @@ -85,6 +85,7 @@ public class BloopMojo extends ExtendedScalaContinuousCompileMojo { private ModuleType moduleType; private List javaCompilerArgs; + @Parameter(property = "launchers") private AppLauncher[] launchers; @Parameter(property = "sourceDir", defaultValue = "$mainSourceDir") diff --git a/src/main/java/scala_maven/AppLauncher.java b/src/main/java/scala_maven/AppLauncher.java index d658c1e..f5054de 100644 --- a/src/main/java/scala_maven/AppLauncher.java +++ b/src/main/java/scala_maven/AppLauncher.java @@ -1,6 +1,13 @@ package scala_maven; public class AppLauncher extends Launcher { + public AppLauncher() { + this.id = ""; + this.mainClass = ""; + this.jvmArgs = new String[0]; + this.args = new String[0]; + } + public AppLauncher(String id, String mainClass, String[] jvmArgs, String[] args) { this.id = id; this.mainClass = mainClass; diff --git a/src/main/scala/bloop/integrations/maven/MojoImplementation.scala b/src/main/scala/bloop/integrations/maven/MojoImplementation.scala index 93310ce..acfeefc 100644 --- a/src/main/scala/bloop/integrations/maven/MojoImplementation.scala +++ b/src/main/scala/bloop/integrations/maven/MojoImplementation.scala @@ -55,7 +55,7 @@ object MojoImplementation { } val currentConfig = mojoExecution.getConfiguration - val dom = newConfig.map(nc => Xpp3Dom.mergeXpp3Dom(currentConfig, nc)) + val dom = newConfig.map(nc => Xpp3Dom.mergeXpp3Dom(nc, currentConfig)) Try { dom.foreach(mojoExecution.setConfiguration) val mojo = mavenPluginManager @@ -167,11 +167,13 @@ object MojoImplementation { val launchers = mojo.getLaunchers() val launcher = launchers .find(_.getId == launcherId) + .orElse { + if (launcherId.nonEmpty) + log.warn(s"Falling back to first launcher: Launcher ID '${launcherId}' does not exist") + launchers.headOption + } .getOrElse { - if (launchers.isEmpty) - log.info(s"Using empty launcher: no run setup for ${project.getName}.") - else if (launcherId.nonEmpty) - log.warn(s"Using empty launcher: Launcher ID '${launcherId}' does not exist") + log.info(s"Using empty launcher: no run setup for ${project.getName}.") emptyLauncher } diff --git a/src/test/resources/launcher/pom.xml b/src/test/resources/launcher/pom.xml new file mode 100644 index 0000000..db042b9 --- /dev/null +++ b/src/test/resources/launcher/pom.xml @@ -0,0 +1,78 @@ + + 4.0.0 + com.example + launcher + 1.0-SNAPSHOT + launcher + A minimal Scala project using the Maven build tool. + + + 1.8 + 1.8 + UTF-8 + 2.13.12 + + + + + org.scala-lang + scala-library + ${scala.version} + + + + + org.scalameta + munit_2.13 + 0.7.29 + test + + + + + src/main/scala + src/test/scala + + + + net.alchim31.maven + scala-maven-plugin + 4.8.1 + + + test + + launcher-2 + + + launcher-1 + + + launcher-2 + + --arg a + --arg b + + + --jvm-arg a + --jvm-arg b + + com.example.Main + + + + + + + compile + testCompile + + + + + + + + + + diff --git a/src/test/scala/bloop/integrations/maven/MavenConfigGenerationTest.scala b/src/test/scala/bloop/integrations/maven/MavenConfigGenerationTest.scala index f7b302e..278dfb5 100644 --- a/src/test/scala/bloop/integrations/maven/MavenConfigGenerationTest.scala +++ b/src/test/scala/bloop/integrations/maven/MavenConfigGenerationTest.scala @@ -13,6 +13,7 @@ import scala.util.Try import scala.util.control.NonFatal import bloop.config.Config +import bloop.config.Config.Platform import bloop.config.Tag import org.junit.Assert._ @@ -66,6 +67,34 @@ class MavenConfigGenerationTest extends BaseConfigSuite { } } + @Test + def launcher() = { + check("launcher/pom.xml") { (configFile, projectName, subprojects) => + assert(subprojects.isEmpty) + assert(configFile.project.`scala`.isDefined) + assertEquals("2.13.12", configFile.project.`scala`.get.version) + assertEquals("org.scala-lang", configFile.project.`scala`.get.organization) + assert( + !configFile.project.`scala`.get.jars.exists(_.toString.contains("scala3-compiler_3")), + "No Scala 3 jar should be present." + ) + assert(!hasCompileClasspathEntryName(configFile, "scala3-library_3")) + assert(hasCompileClasspathEntryName(configFile, "scala-library")) + + assert(hasTag(configFile, Tag.Library)) + + assertNoConfigsHaveAnyJars(List(configFile), List(s"$projectName", s"$projectName-test")) + assertAllConfigsMatchJarNames(List(configFile), List("scala-library", "munit")) + + configFile.project.platform match { + case Some(jvm: Platform.Jvm) => + assertEquals(jvm.config.options, List("--jvm-arg a", "--jvm-arg b")) + assertEquals(jvm.mainClass, Some("com.example.Main")) + case _ => fail("Missing platform") + } + } + } + @Test def multiProject() = { check( From d04bf77e9868040ec319bca545ef3ccb1c962e0d Mon Sep 17 00:00:00 2001 From: Michael Liarakos Date: Fri, 5 Jan 2024 00:17:28 -0500 Subject: [PATCH 2/3] remove unused test pom args --- src/test/resources/launcher/pom.xml | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/test/resources/launcher/pom.xml b/src/test/resources/launcher/pom.xml index db042b9..fea42e0 100644 --- a/src/test/resources/launcher/pom.xml +++ b/src/test/resources/launcher/pom.xml @@ -39,9 +39,6 @@ scala-maven-plugin 4.8.1 - - test - launcher-2 From 8a0d85419db783489f47b66eea9ef22702e02b4d Mon Sep 17 00:00:00 2001 From: Michael Liarakos Date: Sat, 6 Jan 2024 20:46:08 -0500 Subject: [PATCH 3/3] handle empty string launcher --- src/main/java/scala_maven/AppLauncher.java | 7 ------- .../maven/MojoImplementation.scala | 19 +++++++------------ 2 files changed, 7 insertions(+), 19 deletions(-) diff --git a/src/main/java/scala_maven/AppLauncher.java b/src/main/java/scala_maven/AppLauncher.java index f5054de..0a4d165 100644 --- a/src/main/java/scala_maven/AppLauncher.java +++ b/src/main/java/scala_maven/AppLauncher.java @@ -8,13 +8,6 @@ public AppLauncher() { this.args = new String[0]; } - public AppLauncher(String id, String mainClass, String[] jvmArgs, String[] args) { - this.id = id; - this.mainClass = mainClass; - this.jvmArgs = jvmArgs; - this.args = args; - } - public String getId() { return id; } diff --git a/src/main/scala/bloop/integrations/maven/MojoImplementation.scala b/src/main/scala/bloop/integrations/maven/MojoImplementation.scala index acfeefc..6da8ef0 100644 --- a/src/main/scala/bloop/integrations/maven/MojoImplementation.scala +++ b/src/main/scala/bloop/integrations/maven/MojoImplementation.scala @@ -70,8 +70,6 @@ object MojoImplementation { } } - private val emptyLauncher = new AppLauncher("", "", Array(), Array()) - def writeCompileAndTestConfiguration(mojo: BloopMojo, session: MavenSession, log: Log): Unit = { import scala.collection.JavaConverters._ def abs(file: File): Path = { @@ -163,19 +161,15 @@ object MojoImplementation { val configDir = mojo.getBloopConfigDir.toPath() if (!Files.exists(configDir)) Files.createDirectory(configDir) - val launcherId = mojo.getLauncher() + val launcherId = Option(mojo.getLauncher()).filter(_.nonEmpty) val launchers = mojo.getLaunchers() - val launcher = launchers - .find(_.getId == launcherId) + val launcher = launcherId + .flatMap(id => launchers.find(_.getId == id)) .orElse { if (launcherId.nonEmpty) log.warn(s"Falling back to first launcher: Launcher ID '${launcherId}' does not exist") launchers.headOption } - .getOrElse { - log.info(s"Using empty launcher: no run setup for ${project.getName}.") - emptyLauncher - } // check if Scala is contained in this project // findScalaContext throws an exception if it can't find Scala @@ -203,7 +197,7 @@ object MojoImplementation { // needs to be lazy, since we resolve artifacts later on classpath0: () => java.util.List[_], resources0: java.util.List[_], - launcher: AppLauncher, + launcher: Option[AppLauncher], configuration: String ): Unit = { val suffix = if (configuration == "compile") "" else s"-$configuration" @@ -282,8 +276,9 @@ object MojoImplementation { Config.Scala(scalaOrganization, mojo.getScalaArtifactID(), context.version().toString(), scalacArgs, allScalaJars, analysisOut, Some(compileSetup)) } val javaHome = Some(abs(mojo.getJavaHome().getParentFile.getParentFile)) - val mainClass = if (launcher.getMainClass().isEmpty) None else Some(launcher.getMainClass()) - val platform = Some(Config.Platform.Jvm(Config.JvmConfig(javaHome, launcher.getJvmArgs().toList), mainClass, None, None, None)) + val jvmArgs = launcher.map(_.getJvmArgs.toList).getOrElse(List.empty) + val mainClass = launcher.map(_.getMainClass).filter(_.nonEmpty) + val platform = Some(Config.Platform.Jvm(Config.JvmConfig(javaHome, jvmArgs), mainClass, None, None, None)) val resources = Some(resources0.asScala.toList.flatMap{ case a: Resource => Option(Paths.get(a.getDirectory())) case _ => None