|
| 1 | +--- |
| 2 | +title: Setup a Scala CLI project in IntelliJ alongside your existing SBT project |
| 3 | +sidebar_position: 7 |
| 4 | +--- |
| 5 | + |
| 6 | +import {ChainedSnippets} from "../../src/components/MarkdownComponents.js"; |
| 7 | + |
| 8 | +If you've read through [the basic IDEA IntelliJ cookbook](intellij.md), then you already know how to import a Scala CLI |
| 9 | +project using `BSP`. However, did you know that it's possible to import one alongside an `SBT` project? (Or any other |
| 10 | +build tool's project, for that matter.) |
| 11 | + |
| 12 | +Here's a walk-through for a simple example. |
| 13 | + |
| 14 | +Let's say you have an existing `SBT` project that you're working with for a while now. You have imported it in IntelliJ |
| 15 | +and the integration works nicely. |
| 16 | +The project's structure looks roughly like this: |
| 17 | + |
| 18 | +<ChainedSnippets> |
| 19 | + |
| 20 | +```bash |
| 21 | +tree -a |
| 22 | +``` |
| 23 | + |
| 24 | +```text |
| 25 | +. |
| 26 | +├── .bsp |
| 27 | +│ └── sbt.json |
| 28 | +├── .idea |
| 29 | +│ ├── .gitignore |
| 30 | +│ ├── codeStyles |
| 31 | +│ │ ├── Project.xml |
| 32 | +│ │ └── codeStyleConfig.xml |
| 33 | +│ ├── libraries |
| 34 | +│ │ ├── sbt__junit_junit_4_13_2_jar.xml |
| 35 | +│ │ ├── sbt__org_hamcrest_hamcrest_core_1_3_jar.xml |
| 36 | +│ │ ├── sbt__org_scala_lang_scala3_library_3_3_1_3_jar.xml |
| 37 | +│ │ ├── sbt__org_scala_lang_scala_library_2_13_8_jar.xml |
| 38 | +│ │ ├── sbt__org_scala_sbt_test_interface_1_0_jar.xml |
| 39 | +│ │ ├── sbt__org_scalameta_junit_interface_1_0_0_M6_jar.xml |
| 40 | +│ │ └── sbt__org_scalameta_munit_3_1_0_0_M6_jar.xml |
| 41 | +│ ├── misc.xml |
| 42 | +│ ├── modules |
| 43 | +│ │ ├── intellij-sbt-with-scala-cli-bsp-build.iml |
| 44 | +│ │ └── intellij-sbt-with-scala-cli-bsp.iml |
| 45 | +│ ├── modules.xml |
| 46 | +│ ├── sbt.xml |
| 47 | +│ ├── scala_compiler.xml |
| 48 | +│ ├── vcs.xml |
| 49 | +│ └── workspace.xml |
| 50 | +├── build.sbt |
| 51 | +├── project |
| 52 | +│ └── build.properties |
| 53 | +├── scripts |
| 54 | +│ ├── AnotherScript.sc |
| 55 | +│ └── SomeScript.sc |
| 56 | +├── src |
| 57 | +│ ├── main |
| 58 | +│ │ └── scala |
| 59 | +│ │ └── main.scala |
| 60 | +│ └── test |
| 61 | +│ └── scala |
| 62 | +│ └── MyTests.test.scala |
| 63 | +└── target |
| 64 | + └── scala-3.1.3 |
| 65 | + ├── classes |
| 66 | + │ ├── main$package$.class |
| 67 | + │ ├── main$package.class |
| 68 | + │ ├── main$package.tasty |
| 69 | + │ ├── main.class |
| 70 | + │ └── main.tasty |
| 71 | + └── test-classes |
| 72 | + ├── MyTests.class |
| 73 | + └── MyTests.tasty |
| 74 | +
|
| 75 | +16 directories, 32 files |
| 76 | +``` |
| 77 | + |
| 78 | +</ChainedSnippets> |
| 79 | + |
| 80 | +Now, let's say that at some point you decide you need to occasionally run some scripts relevant to this project. You run |
| 81 | +those scripts with Scala CLI and decide it'd be convenient to keep them in the same repository. |
| 82 | + |
| 83 | +<ChainedSnippets> |
| 84 | +```bash |
| 85 | +tree scripts |
| 86 | +``` |
| 87 | + |
| 88 | +```text |
| 89 | +scripts |
| 90 | +├── AnotherScript.sc |
| 91 | +└── SomeScript.sc |
| 92 | +
|
| 93 | +0 directories, 2 files |
| 94 | +``` |
| 95 | +</ChainedSnippets> |
| 96 | + |
| 97 | +However, you already import this repo as an `SBT` project, so what can you do? |
| 98 | +Well, you can import the Scala CLI scripts as a `BSP` module **alongside** your `SBT` project. |
| 99 | + |
| 100 | +Make sure you setup the `BSP` configuration for the `scripts` directory first: |
| 101 | + |
| 102 | +```bash ignore |
| 103 | +scala-cli setup-ide scripts |
| 104 | +``` |
| 105 | + |
| 106 | +As a result, a `scripts/.bsp` directory should be created. |
| 107 | +Now, right-click on your project root directory in `IntelliJ` and go into `Module Settings` |
| 108 | + |
| 109 | + |
| 110 | + |
| 111 | +Then, under `Project Structure` -> `Modules` press the `+` button and then `Import Module`. |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | +Navigate to the `scripts` directory from there and add it as a `BSP` module (`BSP` should be an available choice, |
| 116 | +if the `setup-ide` command was run correctly). |
| 117 | + |
| 118 | + |
| 119 | + |
| 120 | +Now the `scripts` `BSP` module should be imported and you should be able to run the scripts from your IDE. |
| 121 | +The end result should look like this: |
| 122 | + |
| 123 | + |
0 commit comments