|
| 1 | +--- |
| 2 | +title: Building a Scala Project with IntelliJ and sbt |
| 3 | +layout: inner-page-no-masthead |
| 4 | +disqus: true |
| 5 | +previous-page: getting-started-intellij-track/getting-started-with-scala-in-intellij |
| 6 | +next-page: testing-scala-in-intellij-with-scalatest |
| 7 | +--- |
| 8 | + |
| 9 | +In this tutorial, we'll see how to build a Scala project using [sbt](http://www.scala-sbt.org/0.13/docs/index.html). sbt is a popular tool for compiling, running, and testing Scala projects of any |
| 10 | +size. Using a build tool such as sbt (or Maven/Gradle) becomes essential once you create projects with dependencies |
| 11 | +or more than one code file. |
| 12 | + We assume you've completed the |
| 13 | +[first tutorial](getting-started-with-scala-in-intellij.html). |
| 14 | + |
| 15 | +## Creating the project |
| 16 | +In this section, we'll show you how to create the project in IntelliJ. However, if you're |
| 17 | +comfortable with the command line, we recommend you try [Getting |
| 18 | +Started with Scala and sbt in the Command Line]({{site.baseurl}}/documentation/getting-started-sbt-track/getting-started-with-scala-and-sbt-in-the-command-line.html) and then come back |
| 19 | + here to the section "Writing Scala code". |
| 20 | + |
| 21 | +1. If you didn't create the project from the command line, open up IntelliJ and select "Create New Project" |
| 22 | + * On the left panel, select Scala and on the right panel, select SBT |
| 23 | + * Click **Next** |
| 24 | + * Name the project "SBTExampleProject" |
| 25 | +1. If you already created the project in the command line, open up IntelliJ, select *Import Project* and open the `build.sbt` file for your project |
| 26 | +1. Make sure the **JDK Version** is 1.8 and the **SBT Version** is at least 0.13.13 |
| 27 | +1. Select **Use auto-import** so dependencies are automatically downloaded when available |
| 28 | +1. Select **Finish** |
| 29 | + |
| 30 | +## Understanding the directory structure |
| 31 | +sbt creates many directories which can be useful once you start building |
| 32 | +more complex projects. You can ignore most of them for now |
| 33 | +but here's a glance at what everything is for: |
| 34 | + |
| 35 | +``` |
| 36 | +- .idea (IntelliJ files) |
| 37 | +- project (plugins and additional settings for sbt) |
| 38 | +- src (source files) |
| 39 | + - main (application code) |
| 40 | + - java (Java source files) |
| 41 | + - scala (Scala source files) <-- This is all we need for now |
| 42 | + - scala-2.12 (Scala 2.12 specific files) |
| 43 | + - test (unit tests) |
| 44 | +- target (generated files) |
| 45 | +- build.sbt (build definition file for sbt) |
| 46 | +
|
| 47 | +
|
| 48 | +``` |
| 49 | + |
| 50 | + |
| 51 | +## Writing Scala code |
| 52 | +1. On the **Project** panel on the left, expand `SBTExampleProject` => `src` |
| 53 | +=> `main` |
| 54 | +1. Right-click `scala` and select **New** => **Package** |
| 55 | +1. Name the package `example` and click **OK**. |
| 56 | +1. Right-click the package `example` and select **New** => **Scala class**. |
| 57 | +1. Name the class `Main` and change the **Kind** to `object`. |
| 58 | +1. Change the code in the class to the following: |
| 59 | +``` |
| 60 | +object Main extends App { |
| 61 | + val ages = Seq(42, 75, 29, 64) |
| 62 | + println(s"The oldest person is ${ages.max}") |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +Note: IntelliJ has its own syntax highlighter and sometimes your code is |
| 67 | +correct even though IntelliJ indicates otherwise. You can always check |
| 68 | +to see if sbt can run your project in the command line. |
| 69 | + |
| 70 | +## Running the project |
| 71 | +1. From the **Run** menu, select **Edit configurations** |
| 72 | +1. Click the **+** button and select **SBT Task**. |
| 73 | +1. Name it `Run the program`. |
| 74 | +1. In the **Tasks** field, type `~run`. The `~` causes SBT to rebuild and rerun the project |
| 75 | +when you save changes to a file in the project. |
| 76 | +1. Click **OK**. |
| 77 | +1. On the **Run** menu. Click **Run 'Run the program'**. |
| 78 | +1. In the code, change `currentYear - 1` to ` currentYear - 2` |
| 79 | +and look at the updated output in the console. |
| 80 | + |
| 81 | +## Adding a dependency |
| 82 | +Changing gears a bit, let's look at how to use published libraries to add |
| 83 | +extra functionality to our apps. |
| 84 | +1. Open up `build.sbt` and add the following line: |
| 85 | + |
| 86 | +``` |
| 87 | +"org.scala-lang.modules" %% "scala-parser-combinators" % "1.0.5" |
| 88 | +
|
| 89 | +``` |
| 90 | +Here, `libraryDependencies` is a set of dependencies, and by using `+=`, |
| 91 | +we're adding the [scala-parser-combinators]({{site.baseurl}}/scala/scala-parser-combinators) dependency to the set of dependencies that sbt will go |
| 92 | +and fetch when it starts up. Now, in any Scala file, you can import classes, |
| 93 | +objects, etc, from scala-parser-combinators with a regular import. |
| 94 | + |
| 95 | +Find published libraries at [Scaladex](https://index.scala-lang.org/). |
| 96 | + |
| 97 | +## Next steps |
| 98 | +Continue learning the language for free online with |
| 99 | + [Scala Exercises](http://www.scala-exercises.org). |
| 100 | +You can also check out our [list of learning resources](http://scala-lang.org/documentation/). |
| 101 | + |
| 102 | +[Up Next: Testing Scala in IntelliJ with scalatest](testing-scala-in-intellij-with-scalatest.html) |
0 commit comments