6

Note: This question is specific to Scala projects. I want to have Scala compile and run successfully from inside IntelliJ without any prior configuration.

I have created a test project containing Scala examples using IntelliJ IDEA and published it to GitHub.

The project structure looks like this:

Project Structure

My current .gitignore looks like this:

*.class
*.log
target/
.idea/
project/

This results in a repository that looks like this:

GitHub repository structure

Now for what I am trying to do:

Having Scala example code is cool, but I want to also use this project as a template for Scala projects with IntelliJ.

So, how should I best change my .gitignore file, so whenever I clone the project, I can open it with IntelliJ and have everything in working order? This, of course, excludes target directories, so I need to recompile the project whenever I clone it.

phihag
  • 278,196
  • 72
  • 453
  • 469
Markus Appel
  • 3,138
  • 1
  • 17
  • 46
  • Possible duplicate of [Intellij Idea 9/10, what folders to check into (or not check into) source control?](https://stackoverflow.com/questions/3041154/intellij-idea-9-10-what-folders-to-check-into-or-not-check-into-source-contro) – phd Mar 12 '18 at 16:02

3 Answers3

5

Assuming that you're using SBT to build your project in IntelliJ, you should filter out the following directories:

project/target/
target/

(Actually, just adding target/ filters both out.)

These directories contain SBT output generated from your sources, and should not be placed under version control.

If everyone working on your project is to use IntelliJ while working on the code, then you should add all of the ./.idea directory with the following exceptions:

.idea/.name
.idea/libraries/
.idea/modules/
.idea/modules.xml
.idea/scala_compiler.xml
.idea/tasks.xml
.idea/workspace.xml
.idea_modules/

(You might also want to consider adding .idea/sbt.xml to this list. It has some redundant information from SBT, but some IntelliJ SBT configuration settings as well. Up to you...)

These files and directories either contain information gleaned by IntelliJ from SBT (and are, therefore, redundant) or contain machine- and/or user-specific information which will create problems if checked out on a different machine or by a different user.

If SBT is the primary build tool, and people can use any IDE they like, it would probably be better to ignore the entire .idea directory instead.

Mike Allen
  • 8,139
  • 2
  • 24
  • 46
  • Thank you, great answer. I want to have a template for an IntelliJ Scala project in this case, so I will keep `.idea/` – Markus Appel Mar 12 '18 at 16:43
  • Do I even need SBT if I compile inside IntelliJ IDEA? It tells me `SBT configuration detected. Do you want to import it?` whenever I load the cloned project for the first time, but I don't have to click it to build and run the code from inside IntelliJ. – Markus Appel Mar 12 '18 at 16:48
  • Yes, you definitely need SBT! Even if all of your interaction is through IntelliJ, you'll still need an _SBT_ build for continuous integration, etc. Anything that is related to the build should be in _SBT_; everything else (copyright, dictionaries, code styles, misc. settings) are in _IntelliJ_. Also, I recommend setting up _SBT run configurations_, so that _IntelliJ_ uses _SBT_ to run and debug code. (Alas, the run configurations are in `.idea/workspace.xml` and have to be recreated each time you retrieve from _git_.) – Mike Allen Mar 12 '18 at 16:56
  • You should also enable _auto-import_ of the _SBT_ build into _IntelliJ_, so that the latter updates itself whenever the _SBT_ build is modified. – Mike Allen Mar 12 '18 at 17:01
2

I would recommend using the website https://gitignore.io where it will build you a .gitignore file based on your environment.

Here's one that ignores everything you shouldn't commit for a Scala/SBT/Windows/Vim based project:

https://gitignore.io/api/git,sbt,scala,windows,intellij,vim

Janac Meena
  • 3,203
  • 35
  • 32
0

You must commit the /project directory as it contains important information like build.properties and plugins.sbt, etc..

you should ignore /project/target also you should ignore *.iml

gotch4
  • 13,093
  • 29
  • 107
  • 170
  • What about the `.idea` - folder? – Markus Appel Mar 12 '18 at 13:02
  • 1
    ```.idea/``` is an intellij configuration folder. You're better off ignoring it too. – yarwest Mar 12 '18 at 13:13
  • 1
    Sadly, without having the `.idea`-folder in the repository, I do not get a project in working order, but instead have to go through different setup steps beforehand. – Markus Appel Mar 12 '18 at 13:44
  • @MarkusAppel you shouldn't commit any ide specific files. SBT should define the project, not intellij. The step for working on the project should be, clone the repository, then import sbt project in your ide. – puhlen Mar 12 '18 at 15:12
  • @puhlen Please read my question again. I *want* a template project for IntelliJ IDEA. – Markus Appel Mar 12 '18 at 15:14
  • @MarkusAppel Read the answer, don't commit .idea-folder! Rely on the build.sbt. – Epicurist Aug 24 '18 at 18:11
  • Scala != SBT. IDEA will by default interact directly with scalac and not use SBT. It's because of this that this answer completely misses the point. – Markus Appel Aug 26 '18 at 20:48
  • When creating a new Scala project one can choose between an SBT-based project or an IDEA-based project, because logically speaking SBT is an alternative to IDEA. That's why Scala projects without a build.sbt are possible, utilizing IDEA's dependency management instead. – Markus Appel Aug 26 '18 at 20:51