Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,23 @@ In this tutorial, we'll see how to build a Scala project using [sbt](http://www.
size. Using a build tool such as sbt (or Maven/Gradle) becomes essential once you create projects with dependencies
or more than one code file.
We assume you've completed the
[first tutorial](getting-started-with-scala-in-intellij.html).
[first tutorial]({{ site.baseurl }}/documentation/getting-started-intellij-track/getting-started-with-scala-in-intellij.html).

## Creating the project
In this section, we'll show you how to create the project in IntelliJ. However, if you're
comfortable with the command line, we recommend you try [Getting
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

here to the section "Writing Scala code".

1. If you didn't create the project from the command line, open up IntelliJ and select "Create New Project"
* On the left panel, select Scala and on the right panel, select SBT
* Click **Next**
* Name the project "SBTExampleProject"
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
1. Make sure the **JDK Version** is 1.8 and the **SBT Version** is at least 0.13.13
1. Select **Use auto-import** so dependencies are automatically downloaded when available
1. Select **Finish**
* 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
* Make sure the **JDK Version** is 1.8 and the **SBT Version** is at least 0.13.13
* Select **Use auto-import** so dependencies are automatically downloaded when available
* Select **Finish**

## Understanding the directory structure
sbt creates many directories which can be useful once you start building
Expand All @@ -51,11 +52,11 @@ but here's a glance at what everything is for:
## Writing Scala code
1. On the **Project** panel on the left, expand `SBTExampleProject` => `src`
=> `main`
1. Right-click `scala` and select **New** => **Package**
1. Name the package `example` and click **OK**.
1. Right-click the package `example` and select **New** => **Scala class**.
1. Name the class `Main` and change the **Kind** to `object`.
1. Change the code in the class to the following:
* Right-click `scala` and select **New** => **Package**
* Name the package `example` and click **OK**.
* Right-click the package `example` and select **New** => **Scala class**.
* Name the class `Main` and change the **Kind** to `object`.
* Change the code in the class to the following:
```
object Main extends App {
val ages = Seq(42, 75, 29, 64)
Expand All @@ -69,13 +70,13 @@ to see if sbt can run your project in the command line.

## Running the project
1. From the **Run** menu, select **Edit configurations**
1. Click the **+** button and select **SBT Task**.
1. Name it `Run the program`.
1. In the **Tasks** field, type `~run`. The `~` causes SBT to rebuild and rerun the project
* Click the **+** button and select **SBT Task**.
* Name it `Run the program`.
* In the **Tasks** field, type `~run`. The `~` causes SBT to rebuild and rerun the project
when you save changes to a file in the project.
1. Click **OK**.
1. On the **Run** menu. Click **Run 'Run the program'**.
1. In the code, change `currentYear - 1` to ` currentYear - 2`
* Click **OK**.
* On the **Run** menu. Click **Run 'Run the program'**.
* In the code, change `currentYear - 1` to ` currentYear - 2`
and look at the updated output in the console.

## Adding a dependency
Expand All @@ -88,7 +89,7 @@ extra functionality to our apps.

```
Here, `libraryDependencies` is a set of dependencies, and by using `+=`,
we're adding the [scala-parser-combinators]({{site.baseurl}}/scala/scala-parser-combinators) dependency to the set of dependencies that sbt will go
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
and fetch when it starts up. Now, in any Scala file, you can import classes,
objects, etc, from scala-parser-combinators with a regular import.

Expand All @@ -99,4 +100,5 @@ Continue learning the language for free online with
[Scala Exercises](http://www.scala-exercises.org).
You can also check out our [list of learning resources](http://scala-lang.org/documentation/).

[Up Next: Testing Scala in IntelliJ with scalatest](testing-scala-in-intellij-with-scalatest.html)
[Up Next: Testing Scala in IntelliJ with scalatest]({{ site.baseurl }}/documentation/getting-started-intellij-track/testing-scala-in-intellij-with-scalatest.html)

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Scala for you.
* If you don't have version 1.8 or higher, [install the JDK](http://www.oracle.com/technetwork/java/javase/downloads/index.html)
1. Install [IntelliJ Community Edition](https://www.jetbrains.com/idea/download/)
1. Install the Scala plugin by following the instructions on

[how to install IntelliJ plugins](https://www.jetbrains.com/help/idea/installing-updating-and-uninstalling-repository-plugins.html)

When we create the project, we'll install the latest version of Scala.
Expand All @@ -24,24 +25,23 @@ when you start IntelliJ.

## Creating the Project
1. Open up IntelliJ and click **File** => **New** => **Project**
1. On the left panel, select Scala. On the right panel, select Scala once again.
1. Name the project **HelloWorld**
1. Assuming this is your first time creating a Scala project with IntelliJ,
* On the left panel, select Scala. On the right panel, select Scala once again.
* Name the project **HelloWorld**
* Assuming this is your first time creating a Scala project with IntelliJ,
you'll need to install a Scala SDK. To the right of the Scala SDK field,
click the **Create** button.
1. Select the highest version number (e.g. 2.12.1) and click **Download**. This might
* Select the highest version number (e.g. 2.12.1) and click **Download**. This might
take a few minutes but subsequent projects can use the same SDK.
1. Once the SDK is created and you're back to the "New Project" window click **Finish**.

* Once the SDK is created and you're back to the "New Project" window click **Finish**.

## Writing code

1. On the **Project** pane on the left, right-click `src` and select
**New** => **Scala class**.
1. Name the class `Hello` and change the **Kind** to `object`.
1. Change the code in the class to the following:

```
* Name the class `Hello` and change the **Kind** to `object`.
* Change the code in the class to the following:
```tut
object Hello extends App {
println("Hello, World!")
}
Expand All @@ -56,11 +56,8 @@ A good way to try out code samples is with Scala Worksheets

1. In the project pane on the left, right click
`src` and select **New** => **Scala Worksheet**.
1. Enter the following code into the worksheet:


```

* Enter the following code into the worksheet:
```tut
def square(x: Int) = x * x

square(2)
Expand All @@ -75,4 +72,5 @@ for starting to learn the language. In the next tutorial, we'll introduce
an important build tool called sbt which can be used for simple projects
and production apps.

Up Next: [Building a Scala Project with IntelliJ and sbt](building-a-scala-project-with-intellij-and-sbt.html)

Up next: [Building a Scala project with IntelliJ and sbt](building-a-scala-project-with-intellij-and-sbt.html)
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,22 @@ previous-page: building-a-scala-project-with-intellij-and-sbt
There are multiple libraries and testing methodologies for Scala,
but in this tutorial, we'll demonstrate one popular option from the ScalaTest framework
called [FunSuite](http://www.scalatest.org/getting_started_with_fun_suite).
We assume you know [how to build a project in IntelliJ](building-a-scala-project-with-intellij-and-sbt.html).

We assume you know [how to build a project in IntelliJ]({{ site.baseurl }}documentation/getting-started-sbt-track/building-a-scala-project-with-intellij-and-sbt.html).

## Setup
1. Create an sbt project in IntelliJ.
* Add the ScalaTest dependency to your build.sbt file:

```
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.1" % "test"
```

1. this will cause sbt to pull down the ScalaTest library
1. If you get a notification "build.sbt was changed", select **auto-import**.
1. On the project pane on the left, expand `src` => `main`.
1. Right-click on `scala` and select **New** => **Scala class**.
1. Call it `CubeCalculator`, change the **Kind** to `object`, and click **OK**.
1. Replace the code with the following:

```
* this will cause sbt to pull down the ScalaTest library
* If you get a notification "build.sbt was changed", select **auto-import**.
* On the project pane on the left, expand `src` => `main`.
* Right-click on `scala` and select **New** => **Scala class**.
* Call it `CubeCalculator`, change the **Kind** to `object`, and click **OK**.
* Replace the code with the following:
```tut
object CubeCalculator extends App {
def cube(x: Int) = {
x * x * x
Expand All @@ -35,9 +33,10 @@ object CubeCalculator extends App {

## Creating a test
1. On the project pane on the left, expand `src` => `test`.
1. Right-click on `scala` and select **New** => **Scala class**.
1. Name the class `CubeCalculatorTest` and click **OK**.
1. Replace the code with the following:

* Right-click on `scala` and select **New** => **Scala class**.
* Name the class `CubeCalculatorTest` and click **OK**.
* Replace the code with the following:

```
import org.scalatest.FunSuite
Expand All @@ -49,8 +48,7 @@ class CubeCalculatorTest extends FunSuite {
}
```

1. In the source code, right-click `CubeCalculatorTest` and select **Run
'CubeCalculatorTest'**.
* In the source code, right-click `CubeCalculatorTest` and select **Run 'CubeCalculatorTest'**.

## Understanding the code
Let's go over this line by line.
Expand All @@ -67,9 +65,10 @@ one convention is "ClassName.methodName".
indeed 27. The `===` is part of ScalaTest and provides clean error messages.

## Adding another test case
1. Add another `assert` statement after the first one that checks for the cube

* Add another `assert` statement after the first one that checks for the cube
of `0`.
1. Re-run the test again by right-clicking `CubeCalculatorTest` and selecting
* Re-run the test again by right-clicking `CubeCalculatorTest` and selecting
'Run **CubeCalculatorTest**'.

## Conclusion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ We assume you know how to use a terminal.
* Run `javac -version` in the command line and make sure you see
`javac 1.8.___`
* If you don't have version 1.8 or higher, [install the JDK](http://www.oracle.com/technetwork/java/javase/downloads/index.html)
1. Install sbt
* [Mac](http://www.scala-sbt.org/0.13/docs/Installing-sbt-on-Mac.html)
* [Windows](http://www.scala-sbt.org/0.13/docs/Installing-sbt-on-Windows.html)
* [Linux](http://www.scala-sbt.org/0.13/docs/Installing-sbt-on-Linux.html)
* Install sbt
* [Mac](http://www.scala-sbt.org/0.13/docs/Installing-sbt-on-Mac.html)
* [Windows](http://www.scala-sbt.org/0.13/docs/Installing-sbt-on-Windows.html)
* [Linux](http://www.scala-sbt.org/0.13/docs/Installing-sbt-on-Linux.html)

## Create the project
1. `cd` to an empty folder.
1. Run the following command `sbt new scala/hello-world.g8`.
* Run the following command `sbt new scala/hello-world.g8`.
This pulls the 'hello-world' template from GitHub.
It will also create a `target` folder, which you can ignore.
1. When prompted, name the application `hello-world`. This will
* When prompted, name the application `hello-world`. This will
create a project called "hello-world".
1. Let's take a look at what just got generated
* Let's take a look at what just got generated

```

Expand All @@ -48,24 +48,26 @@ for generated files. You can ignore these.

## Running the project
1. `cd` into `hello-world`.
1. Run `sbt`. This will open up the sbt console.
1. Type `~run`. The `~` is optional and causes sbt to re-run on every file save,
* Run `sbt`. This will open up the sbt console.
* Type `~run`. The `~` is optional and causes sbt to re-run on every file save,

allowing for a fast edit/run/debug cycle. sbt will also generate a `target` directory
which you can ignore.

## Modifying the code
1. Open the file `src/main/scala/Main.scala` in your favorite text editor.
1. Change "Hello, World!" to "Hello, New York!"
1. If you haven't stopped the sbt command, you should see "Hello, New York!"

* Change "Hello, World!" to "Hello, New York!"
* If you haven't stopped the sbt command, you should see "Hello, New York!"
printed to the console.
1. You can continue to make changes and see the results.
* You can continue to make changes and see the results.


## Adding a dependency
Changing gears a bit, let's look at how to use published libraries to add
extra functionality to our apps.

1. Open up `build.sbt` and add the following line:

```
"org.scala-lang.modules" %% "scala-parser-combinators" % "1.0.5"

Expand All @@ -83,4 +85,4 @@ Now that you know how to create a Scala project, you
can continue learning online for free with [Scala Exercises](http://scala-exercises.org) or choose
from our [list of educational resources](http://scala-lang.org/documentation/).

[Up Next: Testing Scala with sbt in the command line](testing-scala-with-sbt-in-the-command-line.html)
[Up Next: Testing Scala with sbt in the command line]({{ site.baseurl }}/getting-started-sbt-track/testing-scala-with-sbt-in-the-command-line.html)
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ previous-page: getting-started-with-scala-and-sbt-in-the-command-line
There are multiple libraries and testing methodologies for Scala,
but in this tutorial, we'll demonstrate one popular option from the ScalaTest framework
called [FunSuite](http://www.scalatest.org/getting_started_with_fun_suite).
We assume you know [how to create a Scala project with sbt](getting-started-with-scala-and-sbt-in-the-command-line.html).

We assume you know [how to create a Scala project with sbt]({{ site.baseurl }}/documentation/getting-started-sbt-track/getting-started-with-scala-and-sbt-in-the-command-line.html).

## Setup
1. In the command line, create a new directory somewhere (e.g. `Documents`).
1. `cd` into the directory and run `sbt new scala/scalatest-example.g8`
1. Name the project `ScalaTestTutorial`.
1. The project comes with ScalaTest as a dependency in the `build.sbt` file.
1. `cd` into the directory and run `sbt test`. This will run the test suite
* `cd` into the directory and run `sbt new scala/scalatest-example.g8`
* Name the project `ScalaTestTutorial`.
* The project comes with ScalaTest as a dependency in the `build.sbt` file.
* `cd` into the directory and run `sbt test`. This will run the test suite
`CubeCalculatorTest` with a single test called `CubeCalculatorTest.cube`.

```
Expand All @@ -37,8 +38,9 @@ sbt test
1. Open up two files in a text editor:
* `src/main/scala/CubeCalculator.scala`
* `src/test/scala/CubeCalculatorTest.scala`
1. In the file `CubeCalculator.scala`, you'll see how we define the function `cube`.
1. In the file `CubeCalculatorTest.scala`, you'll see that we have a class

* In the file `CubeCalculator.scala`, you'll see how we define the function `cube`.
* In the file `CubeCalculatorTest.scala`, you'll see that we have a class
named after the object we're testing.

```
Expand Down Expand Up @@ -66,9 +68,10 @@ one convention is "ClassName.methodName".
indeed 27. The `===` is part of ScalaTest and provides clean error messages.

## Adding another test case
1. Add another `assert` statement after the first one that checks for the cube

* Add another `assert` statement after the first one that checks for the cube
of `0`.
1. Execute `sbt test` again to see the results.
* Execute `sbt test` again to see the results.

## Conclusion
You've seen one way to test your Scala code. You can learn more about
Expand Down