Maven JAR file only includes HTML files, not Scala classes

456 views Asked by At

I followed this guide to release a Scala / SBT JAR file in Maven.

I ran the sbt publishSigned and sbt sonatypeRelease for the spark-fast-tests 0.11.0 release and the JAR file was correctly built. See here. These commands come from the sbt-sonatype plugin.

For some reason, when I did the 0.12.0 release, the Maven JAR file only includes HTML files and images. For example, the downloaded JAR file contains this file com/github/mrpowers/spark/fast/tests/DatasetComparer.html, but doesn't include DatasetComparer.class.

The target/scala-2.11/spark-fast-tests_2.11-2.3.0_0.12.0.jar file also only includes the HTML files (either sbt publishSigned or sbt sonatypeRelease must have generated this JAR file).

When I run sbt package, the JAR file that's generated includes the Scala classes like com/github/mrpowers/spark/fast/tests/DatasetComparer.class, as expected.

spark-fast-tests is an open source project and here is the build.sbt file.

How can I include my project classes in the JAR file that's uploaded to Maven? Any tips / tricks on how to debug this better?

2

There are 2 answers

0
Nightscape On

A little late to the party, but I had the same problem with spark-testing-base. It stems from the way you modify the artifactName:

artifactName := { (sv: ScalaVersion, module: ModuleID, artifact: Artifact) =>
  artifact.name + "-" + module.revision + "." + artifact.extension
}

which is probably copied from the official SBT docs. Note that the docs have the following hint below the code though:

(Note that in practice you rarely want to drop the classifier.)

My solution for spark-testing-base is the following:

  artifactName := { (sv: ScalaVersion, module: ModuleID, artifact: Artifact) =>
    Artifact.artifactName(sv, module, artifact).replaceAll(s"-${module.revision}", s"-${sparkVersion.value}${module.revision}")
  }

This should make sure to keep the classifier (and whatever additional things might go into the artifactName) and solved the problem of JAR files containing documentation instead of class files.

7
Mario Galic On

Analysing spark-fast-tests build.sbt I would make the following recommendations:

  1. Add sbt-release to plugins.sbt to enable release process customisation:

    addSbtPlugin("com.github.gseitz"  % "sbt-release"   % "1.0.8")
    addSbtPlugin("org.xerial.sbt"     % "sbt-sonatype"  % "2.0")
    addSbtPlugin("com.jsuereth"       % "sbt-pgp"       % "1.1.0")
    
  2. Create version.sbt at project root and move version setting out of build.sbt:

    version in ThisBuild := "0.12.1-SNAPSHOT"
    
  3. Create sonatype.sbt at project root and move the following settings out of build.sbt:

    homepage := Some(url("https://github.com/mrpowers/spark-fast-tests/"))
    scmInfo := Some(
        ScmInfo(
        url("https://github.com/mrpowers/spark-fast-tests/"),
        "[email protected]:mrpowers/spark-fast-tests.git"
        )
    )
    developers := List(
        Developer(
        "mrpowers",
        "Matthew Powers",
        "[email protected]",
        url("https://github.com/mrpowers/spark-fast-tests/")
        )
    )
    licenses += ("MIT", url("http://opensource.org/licenses/MIT"))
    publishMavenStyle := true
    
  4. Add the following release settings to build.sbt:

    import sbtrelease.ReleaseStateTransformations._
    
    publishTo := Some(
      if (isSnapshot.value) { Opts.resolver.sonatypeSnapshots }
      else { Opts.resolver.sonatypeReleases }
    )
    
    releasePublishArtifactsAction := PgpKeys.publishSigned.value
    releaseProcess := Seq[ReleaseStep](
      checkSnapshotDependencies,
      inquireVersions,
      runClean,
      runTest,
      setReleaseVersion,
      commitReleaseVersion,
      tagRelease,
      publishArtifacts,
      setNextVersion,
      commitNextVersion,
      releaseStepCommand("sonatypeReleaseAll"),
      pushChanges
    )
    
  5. Release to Maven Central with sbt release

For a working example have a look at build configuration of sbt-sonatype itself.