Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically add Scala library if it's not added explicitly #1564

Merged
merged 1 commit into from
Sep 13, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ object MojoImplementation {
.getReactorProjects()
.asScala
.map { project =>
(project.getGroupId(), project.getName(), project.getVersion())
(project.getGroupId(), project.getArtifactId(), project.getVersion())
}
.toSet

Expand Down Expand Up @@ -172,10 +172,29 @@ object MojoImplementation {
val analysisOut = None
val sourceDirs = sourceDirs0.map(abs).toList
val classesDir = abs(classesDir0)
val artifacts = project.getArtifacts().asScala
lazy val libraryAndDependencies = scalaContext.toList
.flatMap(_.findLibraryAndDependencies().asScala)
adpi2 marked this conversation as resolved.
Show resolved Hide resolved

// if we don't add scala-library explicitely it will not be available in artifacts
val hasScalaLibrary = artifacts.exists {
case a: Artifact => a.getArtifactId() == "scala-library"
}
val allArtifacts = if (hasScalaLibrary) artifacts else artifacts ++ libraryAndDependencies
val modules =
project.getArtifacts().asScala.collect {
allArtifacts.collect {
case art: Artifact if art.getType() == "jar" && isNotReactorProjectArtifact(art) =>
if (art.getArtifactId() == "scala-library")
scalaContext match {
case Some(context) =>
/* If the scala library is not specified explicitely as recommended
* it might sometimes be wrong, this doesn't happen for Scala 3.
*/
val scalaVersion = context.version().toString()
if (!scalaVersion.startsWith("3.") && scalaVersion != art.getVersion())
art.setVersion(context.version.toString)
case _ =>
}
resolveArtifact(art).foreach { resolvedFile =>
//since we don't resolve dependencies automatically in the plugin, this will be null
art.setFile(resolvedFile)
Expand All @@ -195,7 +214,12 @@ object MojoImplementation {
}

val cp = classpath0().asScala.toList.asInstanceOf[List[String]].map(u => abs(new File(u)))
(projectDependencies.map(u => abs(new File(u))) ++ cp).toList
// scalaLibrary might not be added by default to classpath and it's needed for the compilation
val hasScalaLibrary = cp.exists(p => p.toFile().getName().contains("scala_library-"))

val fullClasspath =
if (hasScalaLibrary) cp else cp ++ libraryAndDependencies.map(_.getFile().toPath())
(projectDependencies.map(u => abs(new File(u))) ++ fullClasspath).toList
}

val tags = if (configuration == "test") List(Tag.Test) else List(Tag.Library)
Expand Down Expand Up @@ -284,6 +308,8 @@ object MojoImplementation {
} else {
Nil
}
if (artifact.getFile() == null)
throw new IllegalArgumentException(s"Could not resolve $artifact")
Config.Module(
organization = artifact.getGroupId(),
name = artifact.getArtifactId(),
Expand Down
43 changes: 43 additions & 0 deletions integrations/maven-bloop/src/test/resources/no_library/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<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>no_library</artifactId>
<version>1.0-SNAPSHOT</version>
<name>No library</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.6</scala.version>
</properties>

<dependencies>
</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.1.1</version>
<configuration></configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
<configuration>
<scalaVersion>${scala.version}</scalaVersion>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,27 @@ class MavenConfigGenerationSuite extends BaseConfigSuite {
}
}

@Test
def noLibrary() = {
check("no_library/pom.xml") { (configFile, projectName, subprojects) =>
assert(subprojects.isEmpty)
assert(configFile.project.`scala`.isDefined)
assertEquals("2.13.6", configFile.project.`scala`.get.version)
assertEquals("org.scala-lang", configFile.project.`scala`.get.organization)
assert(configFile.project.`scala`.get.jars.exists(_.toString.contains("scala-compiler")))
assert(hasCompileClasspathEntryName(configFile, "scala-library"))
assert(hasTag(configFile, Tag.Library))
assertNoConfigsHaveAnyJars(List(configFile), List(s"$projectName", s"$projectName-test"))

val resolutionModules = configFile.project.resolution.get.modules
val scalaLibraryModule = resolutionModules.find(_.name == "scala-library")
assert(scalaLibraryModule.exists { m =>
m.artifacts.exists(_.path.toString().contains("scala-library-2.13.6-sources.jar"))
m.artifacts.exists(_.path.toString().contains("scala-library-2.13.6.jar"))
})
}
}

private def check(testProject: String, submodules: List[String] = Nil)(
checking: (Config.File, String, List[Config.File]) => Unit
): Unit = {
Expand Down