Script to Generate Standard SBT Project

by 8/16/2014 03:10:00 PM 1 comments
I've recently started playing around with Spark and have decided to give SBT another try. Now normally, I use Maven for building most of my Java and Scala projects. Because of this, I'd like any projects that I build using SBT to work seamlessly with my Maven projects and also, my sbt project should have the same style layout as Maven projects (Just to avoid a sort of build cognitive dissonance). Here's my wish list for my script that generates a default SBT project: It should ...
  • create the standard source and test directory trees
  • create the project/build.properties file
  • create the build.sbt file. The build.sbt file should ...
    • use the local maven repository (~/.m2/repository) as a resolver
    • publish to the local maven repository so my other maven projects can use the project.
    • set the scala version for any sub-projects that are created.
    • include the libraries for JUnit and ScalaTest
  • create a README.md file
  • create a .gitignore file.
  • create a placeholder class and test.
This is what the final project layout will look like:
.
├── .gitignore
├── README.md
├── build.sbt
├── project
│   └── build.properties
└── src
    ├── main
    │   ├── resources
    │   └── scala
    │       └── Main.scala
    └── test
        ├── resources
        └── scala
            └── ExampleSpec.scala

8 directories, 6 files
As SBT doesn't come with scripts that generate projects, I rolled my own. Here's a shell script (Sorry Windows Users) that does the above:

#!/usr/bin/env bash

# Generates a standard layout SBT project

PRJ_DIR=$1

# Build project Structure
mkdir $PRJ_DIR
cd  $PRJ_DIR
mkdir -p src/main/scala
mkdir -p src/main/resources
mkdir -p src/test/scala
mkdir -p src/test/resources

# Write project/build.properties
mkdir -p project
echo "sbt.version=0.13.5" > project/build.properties

# Write build.sbt
cat > build.sbt << EOL

organization := "org.mbari"

name := "$PRJ_DIR"

version := "1.0-SNAPSHOT"

scalaVersion in ThisBuild := "2.10.4"

libraryDependencies ++= Seq(
    "junit" % "junit" % "4.11" % "test",
    "org.scalatest" %% "scalatest" % "2.2.1" % "test"
)

resolvers += Resolver.mavenLocal

publishTo := Some(Resolver.file("file",  new File(Path.userHome.absolutePath+"/.m2/repository")))
EOL

# Write a placeholder class
cat > src/main/scala/Main.scala << EOL
object Main extends App {
  println("Hello World")
}
EOL

# Write a placeholder spec/test
cat > src/test/scala/ExampleSpec.scala << EOL
import collection.mutable.Stack
import org.scalatest._

class ExampleSpec extends FlatSpec with Matchers {

  "A Stack" should "pop values in last-in-first-out order" in {
    val stack = new Stack[Int]
    stack.push(1)
    stack.push(2)
    stack.pop() should be (2)
    stack.pop() should be (1)
  }

  it should "throw NoSuchElementException if an empty stack is popped" in {
    val emptyStack = new Stack[Int]
    a [NoSuchElementException] should be thrownBy {
      emptyStack.pop()
    } 
  }
}
EOL

# Write a README.md
cat > README.md << EOL
# $PRJ_DIR
EOL

# Write a .gitignore file
cat > .gitignore << EOL
*.class
.DS_Store
*.orig

# Package Files #
*.jar
*.war
*.ear

# Build Directory #
target/

# Ignore IntelliJ files
.idea/
*.iml

# eclipse conf file
.settings
.classpath
.project
.manager
.scala_dependencies
EOL

Brian Schlining

Developer

Cras justo odio, dapibus ac facilisis in, egestas eget quam. Curabitur blandit tempus porttitor. Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor.

1 comments:

diana wright said...

Such an nice post on project plan .project plan must be laid down before staring the project inititaion.It shows the proper path to pprogress the project systamatically which altimately leads to sucessfull Project