|
1 | 1 | ---
|
2 | 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 |
| 3 | +redirect_to: |
| 4 | + - https://docs.scala-lang.org/getting-started-intellij-track/building-a-scala-project-with-intellij-and-sbt.html |
7 | 5 | ---
|
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]({{ site.baseurl }}/documentation/getting-started-intellij-track/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 on the Command Line]({{site.baseurl}}/documentation/getting-started-sbt-track/getting-started-with-scala-and-sbt-on-the-command-line.html) and then come back |
19 |
| - |
20 |
| - here to the section "Writing Scala code". |
21 |
| - |
22 |
| -1. If you didn't create the project from the command line, open up IntelliJ and select "Create New Project" |
23 |
| - * On the left panel, select Scala and on the right panel, select SBT |
24 |
| - * Click **Next** |
25 |
| - * Name the project "sbtExampleProject" |
26 |
| -* If you already created the project on the command line, open up IntelliJ, select *Import Project* and open the `build.sbt` file for your project |
27 |
| -* Make sure the **JDK Version** is 1.8 and the **SBT Version** is at least 0.13.13 |
28 |
| -* Select **Use auto-import** so dependencies are automatically downloaded when available |
29 |
| -* Select **Finish** |
30 |
| - |
31 |
| -## Understanding the directory structure |
32 |
| -sbt creates many directories which can be useful once you start building |
33 |
| -more complex projects. You can ignore most of them for now |
34 |
| -but here's a glance at what everything is for: |
35 |
| - |
36 |
| -``` |
37 |
| -- .idea (IntelliJ files) |
38 |
| -- project (plugins and additional settings for sbt) |
39 |
| -- src (source files) |
40 |
| - - main (application code) |
41 |
| - - java (Java source files) |
42 |
| - - scala (Scala source files) <-- This is all we need for now |
43 |
| - - scala-2.12 (Scala 2.12 specific files) |
44 |
| - - test (unit tests) |
45 |
| -- target (generated files) |
46 |
| -- build.sbt (build definition file for sbt) |
47 |
| -``` |
48 |
| - |
49 |
| - |
50 |
| -## Writing Scala code |
51 |
| -1. On the **Project** panel on the left, expand `sbtExampleProject` => `src` |
52 |
| -=> `main` |
53 |
| -* Right-click `scala` and select **New** => **Package** |
54 |
| -* Name the package `example` and click **OK**. |
55 |
| -* Right-click the package `example` and select **New** => **Scala class**. |
56 |
| -* Name the class `Main` and change the **Kind** to `object`. |
57 |
| -* Change the code in the class to the following: |
58 |
| - |
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 on the command line. |
69 |
| - |
70 |
| -## Running the project |
71 |
| -1. From the **Run** menu, select **Edit configurations** |
72 |
| -* Click the **+** button and select **SBT Task**. |
73 |
| -* Name it `Run the program`. |
74 |
| -* 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 |
| -* Click **OK**. |
77 |
| -* On the **Run** menu. Click **Run 'Run the program'**. |
78 |
| -* In the code, change `64` to `99` |
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 |
| -libraryDependencies += "org.scala-lang.modules" %% "scala-parser-combinators" % "1.0.6" |
88 |
| -
|
89 |
| -``` |
90 |
| -Here, `libraryDependencies` is a set of dependencies, and by using `+=`, |
91 |
| -we're adding the [scala-parser-combinators](https://index.scala-lang.org/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]({{ site.baseurl }}/documentation/getting-started-intellij-track/testing-scala-in-intellij-with-scalatest.html) |
0 commit comments