Skip to content

Commit 3dfa408

Browse files
committed
Getting Started tutorial series
1 parent 558d546 commit 3dfa408

5 files changed

+418
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
---
2+
title: Getting Started with Scala in IntelliJ
3+
layout: inner-page-no-masthead
4+
disqus: true
5+
next-page: building-a-scala-project-with-intellij-and-sbt
6+
---
7+
8+
In this tutorial, we'll see how to build a minimal Scala project
9+
using IntelliJ IDE with the Scala plugin. We'll have IntelliJ download
10+
Scala for you.
11+
12+
## Installation
13+
1. Make sure you have the Java 8 JDK (also known as 1.8)
14+
* Run `javac -version` in the command line and make sure you see
15+
`javac 1.8.___`
16+
* If you don't have version 1.8 or higher, [install the JDK](http://www.oracle.com/technetwork/java/javase/downloads/index.html)
17+
1. Install [IntelliJ Community Edition](https://www.jetbrains.com/idea/download/)
18+
1. Install the Scala plugin by following the instructions on
19+
[how to install IntelliJ plugins](https://www.jetbrains.com/help/idea/installing-updating-and-uninstalling-repository-plugins.html)
20+
21+
When we create the project, we'll install the latest version of Scala.
22+
Note: If you want to open an existing Scala project, you can click **Open**
23+
when you start IntelliJ.
24+
25+
## Creating the Project
26+
1. Open up IntelliJ and click **File** => **New** => **Project**
27+
1. On the left panel, select Scala. On the right panel, select Scala once again.
28+
1. Name the project **HelloWorld**
29+
1. Assuming this is your first time creating a Scala project with IntelliJ,
30+
you'll need to install a Scala SDK. To the right of the Scala SDK field,
31+
click the **Create** button.
32+
1. Select the highest version number (e.g. 2.12.1) and click **Download**. This might
33+
take a few minutes but subsequent projects can use the same SDK.
34+
1. Once the SDK is created and you're back to the "New Project" window click **Finish**.
35+
36+
37+
## Writing code
38+
39+
1. On the **Project** pane on the left, right-click `src` and select
40+
**New** => **Scala class**.
41+
1. Name the class `Hello` and change the **Kind** to `object`.
42+
1. Change the code in the class to the following:
43+
44+
```
45+
object Hello extends App {
46+
println("Hello, World!")
47+
}
48+
```
49+
50+
## Running it
51+
* Right click on `Hello` in your code and select **Run 'Hello'**.
52+
* You're done!
53+
54+
## Experimenting with Scala
55+
A good way to try out code samples is with Scala Worksheets
56+
57+
1. In the project pane on the left, right click
58+
`src` and select **New** => **Scala Worksheet**.
59+
1. Enter the following code into the worksheet:
60+
61+
62+
```
63+
64+
def square(x: Int) = x * x
65+
66+
square(2)
67+
```
68+
69+
As you change your code, you'll notice that it gets evaluated
70+
in the right pane.
71+
72+
## Next Steps
73+
Now you know how to create a simple Scala project which can be used
74+
for starting to learn the language. In the next tutorial, we'll introduce
75+
an important build tool called sbt which can be used for simple projects
76+
and production apps.
77+
78+
Up Next: [Building a Scala Project with IntelliJ and sbt](building-a-scala-project-with-intellij-and-sbt.html)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
---
2+
title: Testing Scala in IntelliJ with ScalaTest
3+
layout: inner-page-no-masthead
4+
disqus: true
5+
previous-page: building-a-scala-project-with-intellij-and-sbt
6+
---
7+
8+
There are multiple libraries and testing methodologies for Scala,
9+
but in this tutorial, we'll demonstrate one popular option from the ScalaTest framework
10+
called [FunSuite](http://www.scalatest.org/getting_started_with_fun_suite).
11+
We assume you know [how to build a project in IntelliJ](building-a-scala-project-with-intellij-and-sbt.html).
12+
13+
## Setup
14+
1. Create an sbt project in IntelliJ.
15+
* Add the ScalaTest dependency to your build.sbt file:
16+
17+
```
18+
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.1" % "test"
19+
```
20+
21+
1. this will cause sbt to pull down the ScalaTest library
22+
1. If you get a notification "build.sbt was changed", select **auto-import**.
23+
1. On the project pane on the left, expand `src` => `main`.
24+
1. Right-click on `scala` and select **New** => **Scala class**.
25+
1. Call it `CubeCalculator`, change the **Kind** to `object`, and click **OK**.
26+
1. Replace the code with the following:
27+
28+
```
29+
object CubeCalculator extends App {
30+
def cube(x: Int) = {
31+
x * x * x
32+
}
33+
}
34+
```
35+
36+
## Creating a test
37+
1. On the project pane on the left, expand `src` => `test`.
38+
1. Right-click on `scala` and select **New** => **Scala class**.
39+
1. Name the class `CubeCalculatorTest` and click **OK**.
40+
1. Replace the code with the following:
41+
42+
```
43+
import org.scalatest.FunSuite
44+
45+
class CubeCalculatorTest extends FunSuite {
46+
test("CubeCalculator.cube") {
47+
assert(CubeCalculator.cube(3) === 27)
48+
}
49+
}
50+
```
51+
52+
1. In the source code, right-click `CubeCalculatorTest` and select **Run
53+
'CubeCalculatorTest'**.
54+
55+
## Understanding the code
56+
Let's go over this line by line.
57+
58+
* `class CubeCalculatorTest` means we are testing the object `CubeCalculator`
59+
* `extends FunSuite` lets us use functionality of ScalaTest's FunSuite class
60+
such as the `test` function
61+
* `test` is function that comes from the FunSuite library that collects
62+
results from assertions within the function body.
63+
* `"CubeCalculator.cube"` is a name for the test. You can call it anything but
64+
one convention is "ClassName.methodName".
65+
* `assert` takes a boolean condition and determines whether the test passes or fails.
66+
* `CubeCalculator.cube(3) === 27` checks whether the output of the `cube` function is
67+
indeed 27. The `===` is part of ScalaTest and provides clean error messages.
68+
69+
## Adding another test case
70+
1. Add another `assert` statement after the first one that checks for the cube
71+
of `0`.
72+
1. Re-run the test again by right-clicking `CubeCalculatorTest` and selecting
73+
'Run **CubeCalculatorTest**'.
74+
75+
## Conclusion
76+
You've seen one way to test your Scala code. You can learn more about
77+
ScalaTest's FunSuite on the [official website](http://www.scalatest.org/getting_started_with_fun_suite).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
---
2+
title: Getting Started with Scala and sbt in the Command Line
3+
layout: inner-page-no-masthead
4+
disqus: true
5+
next-page: testing-scala-with-sbt-in-the-command-line
6+
---
7+
8+
In this tutorial, you'll see how to create a Scala project from
9+
a template. You can use this as a starting point for your own
10+
projects. We'll use [sbt](http://www.scala-sbt.org/0.13/docs/index.html), the de facto build tool for Scala. sbt compiles,
11+
runs, and tests your projects among other related tasks.
12+
We assume you know how to use a terminal.
13+
14+
## Installation
15+
1. Make sure you have the Java 8 JDK (also known as 1.8)
16+
* Run `javac -version` in the command line and make sure you see
17+
`javac 1.8.___`
18+
* If you don't have version 1.8 or higher, [install the JDK](http://www.oracle.com/technetwork/java/javase/downloads/index.html)
19+
1. Install sbt
20+
* [Mac](http://www.scala-sbt.org/0.13/docs/Installing-sbt-on-Mac.html)
21+
* [Windows](http://www.scala-sbt.org/0.13/docs/Installing-sbt-on-Windows.html)
22+
* [Linux](http://www.scala-sbt.org/0.13/docs/Installing-sbt-on-Linux.html)
23+
24+
## Create the project
25+
1. `cd` to an empty folder.
26+
1. Run the following command `sbt new scala/hello-world.g8`.
27+
This pulls the 'hello-world' template from GitHub.
28+
It will also create a `target` folder, which you can ignore.
29+
1. When prompted, name the application `hello-world`. This will
30+
create a project called "hello-world".
31+
1. Let's take a look at what just got generated
32+
33+
```
34+
35+
- hello-world
36+
- project (sbt uses this to install manage plugins and dependencies)
37+
- build.properties
38+
- src
39+
- main
40+
- scala (All of your scala code goes here)
41+
-Main.scala (Entry point of program) <-- this is all we need for now
42+
build.sbt (sbt's build definition file)
43+
44+
```
45+
46+
After you build your project, sbt will create more `target` directories
47+
for generated files. You can ignore these.
48+
49+
## Running the project
50+
1. `cd` into `hello-world`.
51+
1. Run `sbt`. This will open up the sbt console.
52+
1. Type `~run`. The `~` is optional and causes sbt to re-run on every file save,
53+
allowing for a fast edit/run/debug cycle. sbt will also generate a `target` directory
54+
which you can ignore.
55+
56+
## Modifying the code
57+
1. Open the file `src/main/scala/Main.scala` in your favorite text editor.
58+
1. Change "Hello, World!" to "Hello, New York!"
59+
1. If you haven't stopped the sbt command, you should see "Hello, New York!"
60+
printed to the console.
61+
1. You can continue to make changes and see the results.
62+
63+
## Adding a dependency
64+
Changing gears a bit, let's look at how to use published libraries to add
65+
extra functionality to our apps.
66+
67+
1. Open up `build.sbt` and add the following line:
68+
69+
```
70+
"org.scala-lang.modules" %% "scala-parser-combinators" % "1.0.5"
71+
72+
```
73+
74+
Here, `libraryDependencies` is a set of dependencies, and by using `+=`,
75+
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
76+
and fetch when it starts up. Now, in any Scala file, you can import classes,
77+
objects, etc, from scala-parser-combinators with a regular import.
78+
79+
Find published libraries at [Scaladex](https://index.scala-lang.org/).
80+
81+
## Next steps
82+
Now that you know how to create a Scala project, you
83+
can continue learning online for free with [Scala Exercises](http://scala-exercises.org) or choose
84+
from our [list of educational resources](http://scala-lang.org/documentation/).
85+
86+
[Up Next: Testing Scala with sbt in the command line](testing-scala-with-sbt-in-the-command-line.html)

0 commit comments

Comments
 (0)