Skip to content

Commit

Permalink
Fix resolution of test-jar artifacts (#1747)
Browse files Browse the repository at this point in the history
Resolve test-jar dependencies
  • Loading branch information
keynmol authored Jul 1, 2022
1 parent 6197f55 commit 2b7064d
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import org.apache.maven.plugin.logging.Log
import org.apache.maven.project.MavenProject
import org.codehaus.plexus.util.xml.Xpp3Dom
import org.eclipse.aether.artifact.DefaultArtifact
import org.eclipse.aether.artifact.DefaultArtifactType
import org.eclipse.aether.resolution.ArtifactRequest
import scala_maven.AppLauncher

Expand Down Expand Up @@ -84,13 +85,24 @@ object MojoImplementation {
val suffix = if (classifier.nonEmpty) s":$classifier" else ""
log.info("Resolving artifact: " + artifact + suffix)
val request = new ArtifactRequest()
val handler = artifact.getArtifactHandler()
val artifactType = new DefaultArtifactType(
artifact.getType(),
handler.getExtension(),
handler.getClassifier(),
handler.getLanguage(),
handler.isAddedToClasspath(),
handler.isIncludesDependencies()
)
request.setArtifact(
new DefaultArtifact(
artifact.getGroupId(),
artifact.getArtifactId(),
classifier,
artifact.getType(),
artifact.getVersion()
handler.getExtension(),
artifact.getVersion(),
null,
artifactType
)
)
request.setRepositories(mojo.getRemoteRepositories())
Expand Down Expand Up @@ -192,9 +204,10 @@ object MojoImplementation {
case a: Artifact => a.getArtifactId() == "scala-library"
}
val allArtifacts = if (hasScalaLibrary) artifacts else artifacts ++ libraryAndDependencies
val isJar = Set("jar", "test-jar")
val modules =
allArtifacts.collect {
case art: Artifact if art.getType() == "jar" && isNotReactorProjectArtifact(art) =>
case art: Artifact if isJar(art.getType()) && isNotReactorProjectArtifact(art) =>
if (art.getArtifactId() == "scala-library")
scalaContext match {
case Some(context) =>
Expand Down
57 changes: 57 additions & 0 deletions integrations/maven-bloop/src/test/resources/test_jars/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<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>test_jars</artifactId>
<version>1.0-SNAPSHOT</version>
<name>test_jars</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>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>2.13.6</version>
</dependency>

<!-- Test -->
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-tags_2.13</artifactId>
<type>test-jar</type>
<scope>test</scope>
<version>3.3.0</version>
</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>3.3.2</version>
<configuration></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 @@ -173,6 +173,31 @@ class MavenConfigGenerationSuite extends BaseConfigSuite {
}
}

@Test
def dependencyTestJars() = {
check("test_jars/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("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))
val testJar = configFile.project.resolution.get.modules.find(_.name == "spark-tags_2.13")
assert(testJar.exists { m =>
m.artifacts.exists(_.path.toString().endsWith("spark-tags_2.13-3.3.0-tests.jar"))
})

assertNoConfigsHaveAnyJars(List(configFile), List(s"$projectName", s"$projectName-test"))
assertAllConfigsMatchJarNames(List(configFile), List("scala-library", "spark-tags"))
}
}

@Test
def multiModuleTestJar() = {
check(
Expand Down

0 comments on commit 2b7064d

Please sign in to comment.