|
| 1 | +--- |
| 2 | +title: Setup multiple Scala CLI projects in IDEA IntelliJ as separate modules |
| 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, in some cases importing a single project just does not fit the bill. |
| 10 | + |
| 11 | +Here's a walk-through for a slightly more advanced scenario. |
| 12 | + |
| 13 | +Let's say we keep the sources for 2 separate Scala apps in one repository. Each has its own subdirectory, to keep things |
| 14 | +clean. Additionally, you have another one for scripts alongside them. |
| 15 | + |
| 16 | +It looks somewhat similar to this: |
| 17 | + |
| 18 | +<ChainedSnippets> |
| 19 | + |
| 20 | +```bash |
| 21 | +tree -a |
| 22 | +``` |
| 23 | + |
| 24 | +```text |
| 25 | +. |
| 26 | +├── app1 |
| 27 | +│ ├── src |
| 28 | +│ │ └── HelloWorld1.scala |
| 29 | +│ └── test |
| 30 | +│ └── MyTests1.test.scala |
| 31 | +├── app2 |
| 32 | +│ ├── src |
| 33 | +│ │ └── HelloWorld2.scala |
| 34 | +│ └── test |
| 35 | +│ └── MyTests2.test.scala |
| 36 | +└── scripts |
| 37 | + ├── AnotherScript.sc |
| 38 | + └── SomeScript.sc |
| 39 | +
|
| 40 | +7 directories, 6 files |
| 41 | +``` |
| 42 | + |
| 43 | +</ChainedSnippets> |
| 44 | + |
| 45 | +When running these apps, you'd like to run them separately. `app1` and `app2` may have conflicting dependencies, or it |
| 46 | +may just not feel hygienic to share their classpath long term. |
| 47 | + |
| 48 | +However, you keep those in one repository because of business relevance (or whatever other reasons why they are tied |
| 49 | +together), and so, you'd like to see them all at once in your IDE, with all the syntax coloring, completions and |
| 50 | +debugging |
| 51 | +your code straight from the IDE, the whole shebang. |
| 52 | + |
| 53 | +It's tempting to just run: |
| 54 | + |
| 55 | +```bash |
| 56 | +scala-cli setup-ide . |
| 57 | +``` |
| 58 | + |
| 59 | +Unfortunately, in this case that won't really do the trick. Even if you run and package the apps & scripts from the |
| 60 | +terminal separately, when importing everything together to your IDE like this, the single `BSP` project will make them |
| 61 | +share their classpath. This in turn means that things will break. |
| 62 | + |
| 63 | +The only way to solve this is for each to have its own `BSP` configuration, really. |
| 64 | +And so: |
| 65 | + |
| 66 | +```bash ignore |
| 67 | +scala-cli setup-ide app1 |
| 68 | +scala-cli setup-ide app2 |
| 69 | +scala-cli setup-ide scripts |
| 70 | +``` |
| 71 | + |
| 72 | +As a result, a separate `.bsp` directory was created in `app1`, `app2` and `scripts`, respectively. |
| 73 | + |
| 74 | +<ChainedSnippets> |
| 75 | + |
| 76 | +```bash |
| 77 | +tree -a |
| 78 | +``` |
| 79 | + |
| 80 | +```text |
| 81 | +. |
| 82 | +├── app1 |
| 83 | +│ ├── .bsp |
| 84 | +│ │ └── scala-cli.json |
| 85 | +│ ├── .scala-build |
| 86 | +│ │ ├── ide-inputs.json |
| 87 | +│ │ └── ide-options-v2.json |
| 88 | +│ ├── src |
| 89 | +│ │ └── HelloWorld1.scala |
| 90 | +│ └── test |
| 91 | +│ └── MyTests1.test.scala |
| 92 | +├── app2 |
| 93 | +│ ├── .bsp |
| 94 | +│ │ └── scala-cli.json |
| 95 | +│ ├── .scala-build |
| 96 | +│ │ ├── ide-inputs.json |
| 97 | +│ │ └── ide-options-v2.json |
| 98 | +│ ├── src |
| 99 | +│ │ └── HelloWorld2.scala |
| 100 | +│ └── test |
| 101 | +│ └── MyTests2.test.scala |
| 102 | +└── scripts |
| 103 | + ├── .bsp |
| 104 | + │ └── scala-cli.json |
| 105 | + ├── .scala-build |
| 106 | + │ ├── ide-inputs.json |
| 107 | + │ └── ide-options-v2.json |
| 108 | + ├── AnotherScript.sc |
| 109 | + └── SomeScript.sc |
| 110 | +
|
| 111 | +13 directories, 15 files |
| 112 | +
|
| 113 | +
|
| 114 | +
|
| 115 | +``` |
| 116 | + |
| 117 | +</ChainedSnippets> |
| 118 | + |
| 119 | + |
| 120 | +After opening the root directory in `IntelliJ` (`File` -> `Open...`), the 3 `BSP` setups should be successfully |
| 121 | +detected. |
| 122 | + |
| 123 | + |
| 124 | + |
| 125 | +However, since there are 3 different setups, `IntelliJ` doesn't know what to import. And so, we have to set it up |
| 126 | +ourselves. |
| 127 | + |
| 128 | +Right-click on your project root directory in `Intellij` and go into `Module Settings`. |
| 129 | + |
| 130 | + |
| 131 | + |
| 132 | +Then, under `Project Structure` -> `Modules` press the `+` button and then `Import Module`. |
| 133 | + |
| 134 | + |
| 135 | + |
| 136 | +Navigate to each of the subdirectories from there and add them as a `BSP` module (`BSP` should be an available choice, |
| 137 | +if the `setup-ide` was run correctly). |
| 138 | + |
| 139 | + |
| 140 | + |
| 141 | +You have to import each of the subdirectories separately (`app1`, `app2` and `scripts`, in the example). |
| 142 | + |
| 143 | +The end result should look like this: |
| 144 | + |
| 145 | + |
| 146 | + |
| 147 | +Now each of the subdirectories uses its own `BSP` connection, which in turn means a separate classpath. And all of that |
| 148 | +in a single `IntelliJ` project! |
| 149 | + |
| 150 | +Upon closer inspection, you may notice that `IntelliJ` stores this as separate sub-project configurations. Each |
| 151 | +subdirectory gets its own `.idea` folder with the relevant settings. |
| 152 | + |
| 153 | +<ChainedSnippets> |
| 154 | + |
| 155 | +```bash |
| 156 | +tree -a |
| 157 | +``` |
| 158 | + |
| 159 | +```text |
| 160 | +. |
| 161 | +├── .idea |
| 162 | +│ ├── .gitignore |
| 163 | +│ ├── bsp.xml |
| 164 | +│ ├── codeStyles |
| 165 | +│ │ ├── Project.xml |
| 166 | +│ │ └── codeStyleConfig.xml |
| 167 | +│ ├── intellij-multi-bsp.iml |
| 168 | +│ ├── misc.xml |
| 169 | +│ ├── modules.xml |
| 170 | +│ ├── sbt.xml |
| 171 | +│ ├── vcs.xml |
| 172 | +│ └── workspace.xml |
| 173 | +├── app1 |
| 174 | +│ ├── .bsp |
| 175 | +│ │ └── scala-cli.json |
| 176 | +│ ├── .idea |
| 177 | +│ │ └── modules |
| 178 | +│ │ └── app1-root.iml |
| 179 | +│ ├── .scala-build |
| 180 | +│ │ ├── ide-inputs.json |
| 181 | +│ │ └── ide-options-v2.json |
| 182 | +│ ├── src |
| 183 | +│ │ └── HelloWorld1.scala |
| 184 | +│ └── test |
| 185 | +│ └── MyTests1.test.scala |
| 186 | +├── app2 |
| 187 | +│ ├── .bsp |
| 188 | +│ │ └── scala-cli.json |
| 189 | +│ ├── .idea |
| 190 | +│ │ └── modules |
| 191 | +│ │ └── app2-root.iml |
| 192 | +│ ├── .scala-build |
| 193 | +│ │ ├── ide-inputs.json |
| 194 | +│ │ └── ide-options-v2.json |
| 195 | +│ ├── src |
| 196 | +│ │ └── HelloWorld2.scala |
| 197 | +│ └── test |
| 198 | +│ └── MyTests2.test.scala |
| 199 | +└── scripts |
| 200 | + ├── .bsp |
| 201 | + │ └── scala-cli.json |
| 202 | + ├── .idea |
| 203 | + │ └── modules |
| 204 | + │ └── scripts-root.iml |
| 205 | + ├── .scala-build |
| 206 | + │ ├── ide-inputs.json |
| 207 | + │ └── ide-options-v2.json |
| 208 | + ├── AnotherScript.sc |
| 209 | + └── SomeScript.sc |
| 210 | +
|
| 211 | +21 directories, 28 files |
| 212 | +``` |
| 213 | + |
| 214 | +</ChainedSnippets> |
0 commit comments