Skip to content

Commit

Permalink
Merge pull request #74 from mliarakos/fix-launcher-configuration
Browse files Browse the repository at this point in the history
Fix launcher configuration
  • Loading branch information
tgodzik authored Aug 6, 2024
2 parents 4f56703 + 8a0d854 commit aa69298
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 20 deletions.
1 change: 1 addition & 0 deletions src/main/java/bloop/integrations/maven/BloopMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public class BloopMojo extends ExtendedScalaContinuousCompileMojo {
private ModuleType moduleType;
private List<String> javaCompilerArgs;

@Parameter(property = "launchers")
private AppLauncher[] launchers;

@Parameter(property = "sourceDir", defaultValue = "$mainSourceDir")
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/scala_maven/AppLauncher.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package scala_maven;

public class AppLauncher extends Launcher {
public AppLauncher(String id, String mainClass, String[] jvmArgs, String[] args) {
this.id = id;
this.mainClass = mainClass;
this.jvmArgs = jvmArgs;
this.args = args;
public AppLauncher() {
this.id = "";
this.mainClass = "";
this.jvmArgs = new String[0];
this.args = new String[0];
}

public String getId() {
Expand Down
27 changes: 12 additions & 15 deletions src/main/scala/bloop/integrations/maven/MojoImplementation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = {
Expand Down Expand Up @@ -163,16 +161,14 @@ 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)
.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")
emptyLauncher
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
}

// check if Scala is contained in this project
Expand Down Expand Up @@ -201,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"
Expand Down Expand Up @@ -280,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
Expand Down
75 changes: 75 additions & 0 deletions src/test/resources/launcher/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>launcher</artifactId>
<version>1.0-SNAPSHOT</version>
<name>launcher</name>
<description>A minimal Scala project using the Maven build tool.</description>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<encoding>UTF-8</encoding>
<scala.version>2.13.12</scala.version>
</properties>

<dependencies>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>${scala.version}</version>
</dependency>

<!-- Test -->
<dependency>
<groupId>org.scalameta</groupId>
<artifactId>munit_2.13</artifactId>
<version>0.7.29</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<sourceDirectory>src/main/scala</sourceDirectory>
<testSourceDirectory>src/test/scala</testSourceDirectory>
<plugins>
<plugin>
<!-- see http://davidb.github.com/scala-maven-plugin -->
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>4.8.1</version>
<configuration>
<launcher>launcher-2</launcher>
<launchers>
<launcher>
<id>launcher-1</id>
</launcher>
<launcher>
<id>launcher-2</id>
<args>
<arg>--arg a</arg>
<arg>--arg b</arg>
</args>
<jvmArgs>
<jvmArg>--jvm-arg a</jvmArg>
<jvmArg>--jvm-arg b</jvmArg>
</jvmArgs>
<mainClass>com.example.Main</mainClass>
</launcher>
</launchers>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
<configuration>
<args></args>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -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._
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit aa69298

Please sign in to comment.