41

Historically I have used Ant+Ivy or Maven for building my Java projects. I'm now looking at non-xml based solutions.

Gradle can compile, jar and publish my project with few issues.

Can I do the same with SBT? If so, can you provide a simple example of using sbt to build a java only project.

opticyclic
  • 7,412
  • 12
  • 81
  • 155

4 Answers4

36

Yes this is entirely possible. Nothing to setup really, a small build.sbt file should do the trick, something like:

organization := "your.group.id"

name := "Your project"

version := "1.0-SNAPSHOT"

libraryDependencies ++= Seq( <any normal jar deps> )

And run sbt package from the command line.

Hiery Nomus
  • 17,429
  • 2
  • 41
  • 37
  • 1
    I missed the sections in the Getting started guide that mentioned sbt package! https://github.com/harrah/xsbt/wiki/Getting-Started-Running https://github.com/harrah/xsbt/wiki/Java-Sources – opticyclic Nov 29 '11 at 01:28
  • The new location for the second link above is http://www.scala-sbt.org/release/docs/Detailed-Topics/Java-Sources – adelarsq Apr 30 '13 at 20:52
  • Running `sbt package` does not seem to be required anymore (unless I'm missing somethig); http://www.scala-sbt.org/release/docs/Detailed-Topics/Java-Sources doesn't even mention it. – Erik Kaplun Nov 15 '13 at 14:14
  • Running `sbt package` will generate a jar of your built sources... It is not and never was needed for the setup. – Hiery Nomus Nov 15 '13 at 14:30
  • 2
    I like SBT, too, but let's be honest. Will that configuration allow you to publish your project to maven/ivy? Not conveniently, it will append the unnecessary Scala suffix to the package name. Will that configuration allow you to create executable jar-s? No, because `package` does not include dependencies. Will that project be test-able and runnable from within SBT? Yes, that's the only thing that will work out of the box in the described configuration. – VasiliNovikov Nov 01 '15 at 15:57
33

For me, it also helped a bit to remove the scala version information from the generated artifact paths, as described in this answer. You'll also want to remove the Scala library as a dependency from any pom or ivy file you publish.

Here's what you'll need:

crossPaths := false
autoScalaLibrary := false
Community
  • 1
  • 1
raimohanska
  • 3,265
  • 17
  • 28
13

Sure, you can. Example configuration:

name := "myName"
version := "0.1"
organization := "org.myorganization"

javacOptions in (Compile, compile) ++= Seq("-source", "1.8", "-target", "1.8", "-g:lines")

crossPaths := false // drop off Scala suffix from artifact names.
autoScalaLibrary := false // exclude scala-library from dependencies

Summing up. I love SBT, but I felt necessary to write the full build with all the tricky parts of using it for java. Note that this setup might be better than a maven one because you'll have nice features such as incremental testing or even incremental runs. Also consider adding sbt-assembly plugin if you have dependencies and want to create fat jars (executables).

VasiliNovikov
  • 9,681
  • 4
  • 44
  • 62
2

There's a nice example of build.sbt for pure Java sources at the Xerial blog including how to publish Maven style artifacts with no Scala version tag.

michael
  • 762
  • 7
  • 13