Skip to content

Instantly share code, notes, and snippets.

@ashrithr
Last active November 18, 2022 08:24
Show Gist options
  • Save ashrithr/6486907 to your computer and use it in GitHub Desktop.
Save ashrithr/6486907 to your computer and use it in GitHub Desktop.
creates scala project structure using sbt 0.13
#!/bin/bash
function usage () {
script=$0
cat <<USAGE
Creates scala project structure to be used with sbt
Syntax
`basename $script` project_name artifact_id
Example
`basename $script` test com.test
USAGE
exit 1
}
if [ $# -ne 2 ];
then
echo "Arguments required"
usage
fi
directoryName=$1
projectName=$1
artifactId=$2
[ ! -d $directoryName ] && mkdir $directoryName
echo "[*] Creating directory structure"
mkdir -p ${directoryName}/src/{main,test}/{java,resources,scala}
mkdir ${directoryName}/lib ${directoryName}/project ${directoryName}/target
if which git &> /dev/null; then
echo "[*] Initializing git repository"
( cd ${directoryName} && git init &>/dev/null )
echo "[*] Creating files for git"
echo 'target/
.cache
.classpath
project/project
project/target
.project/
.settings
.idea
.idea_modules
*.log
' > ${directoryName}/.gitignore
touch ${directoryName}/README.md
touch ${directoryName}/CHANGES.md
touch ${directoryName}/LICENSE.txt
fi
if [ ! -f ~/.sbt/0.13/plugins/build.sbt ]; then
[ ! -d ~/.sbt/0.13/plugins ] && mkdir -p ~/.sbt/0.13/plugins
echo "[*] Creating 'build.sbt' in '~/.sbt/0.13/plugins'"
echo "[*] Adding 'sbt-idea' as dependency plugin"
echo 'addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.6.0")' > ~/.sbt/0.13/plugins/build.sbt
echo "[*] Adding 'sbt-assembly' as dependency plugin"
echo 'addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.10.1")' >> ~/.sbt/0.13/plugins/build.sbt
fi
echo "[*] Creating scala project build files"
cat > ${directoryName}/assembly.sbt <<EOF
import AssemblyKeys._
assemblySettings
EOF
cat > ${directoryName}/project/build.properties <<EOF
sbt.version=0.13.0
EOF
cat > ${directoryName}/project/plugins.sbt <<EOF
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.10.1")
EOF
cat > ${directoryName}/project/Build.scala <<BUILDEOF
import sbt._
import Keys._
object Build extends Build {
val ScalaVersion = "2.10.3"
lazy val root = Project("${projectName}", file(".")) settings(
version := "0.1",
scalaVersion := ScalaVersion,
organization := "${artifactId}",
scalacOptions ++= Seq("-unchecked", "-deprecation"),
libraryDependencies ++= Dependencies.compile,
libraryDependencies ++= Dependencies.testDependencies,
resolvers ++= Dependencies.resolvers
)
object Dependencies {
val compile = Seq(
"ch.qos.logback" % "logback-classic" % "1.0.13"
)
val testDependencies = Seq(
"org.specs2" %% "specs2" % "1.14" % "test",
"org.mockito" % "mockito-all" % "1.9.0" % "test",
"org.hamcrest" % "hamcrest-all" % "1.1" % "test"
)
val resolvers = Seq(
"amateras-repo" at "http://amateras.sourceforge.jp/mvn/"
)
}
}
BUILDEOF
echo "[*] Placing logback configuration file at ${directoryName}/src/main/resources/logback.xml"
cat > ${directoryName}/src/main/resources/logback.xml <<EOF
<configuration debug="false">
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logFile.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>logFile.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- keep 1 days worth of history -->
<maxHistory>5</maxHistory>
</rollingPolicy>
<append>true</append>
<encoder>
<pattern>%d{HH:mm:ss.SSS} %-4relative [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<!-- log to both stdout and file -->
<root level="INFO">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</root>
</configuration>
EOF
echo ""
echo "Project structure created. See the following URL(s)"
echo "* for build.sbt usage:"
echo -e "\thttp://www.scala-sbt.org/release/docs/Getting-Started/Basic-Def.html"
echo "* for sbt commands:"
echo -e "\thttp://www.scala-sbt.org/release/docs/Getting-Started/Running.html"
echo "Comman Tasks:"
echo "* To export the project to Intellij:"
echo " Add the following line to ~/.sbt/plugins/build.sbt (or) PROJECT_DIR/project/plugins.sbt"
echo ' addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.6.0")'
echo " Then use 'gen-idea' sbt task to export the project to intellij"
echo "* To export the project to Eclipse:"
echo " Use 'SbtEclipsify' @ https://github.com/musk/SbtEclipsify"

Sbt is a build tool for scala projects.

###Installing scala and sbt on Mac using brew:

brew install scala --with-docs
brew install sbt

Also, optionally install drip whihc is a JVM launcher which preloads and keeps a warm JVM instance to make things appear faster.

Export the following variables (probablt to ~/.bashrc or ~/.bash_profile), to make life easier:

export JAVA_HOME=$(/usr/libexec/java_home)
export SCALA_HOME=/usr/local/Cellar/scala/2.10.3/libexec
export JAVACMD=drip
export DRIP_SHUTDOWN=30
# to avaoid jvm out of space errors while using sbt
export SBT_OPTS="-XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled -XX:PermSize=256M -XX:MaxPermSize=512M"

###Creating Project Structure To create a project structure for scala projects, use the attached buildScalaProject.sh scipt in this gist.

###Woring with IntelliJ To configure IDEA 12, start by installing the Scala plugin from the plugin repository and restart IDEA.

To work with IntelliJ use, sbt task 'gen-idea' from inside the project.

(Or)

Alternatively creating scala project from inside IntelliJ

  1. start a new project in IDEA and select "Scala Module". Enter the project name, click on the New button for Project SDK. The $JAVA_HOME directory should already be selected in the dialog which appears, just click on Choose to proceed.

In Scala settings, select the "Set Scala Home" radio button and enter "/usr/local/opt/scala/idea" in the text box below it. Click on "Finish" to create the new project.

@balachandra
Copy link

Thank you so much. i have spent like 4 hrs on getting my head around packaging and with sbt version issues. this script solved everything in one shot.

@ashrithr
Copy link
Author

@balachandra you're welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment