Change directory to SBT project in IntelliJ with SBT console - intellij-idea

I've just installed SBT plugin for IntelliJ and successfully imported my project. SBT console in IntelliJ shows up as expected, however I can't use it because of layout of my project. The entire problem is that my SBT Play! project is not a top-level directory. Instead I have maven parent pom with several child modules amongst which my SBT application is placed. This is how it looks like:
MyProject (parent pom)
-> submodule1 (JAR)
-> submodule2 (JAR)
-> webapp (SBT Play! webapp module)
There is no problem to run Play! application from Linux CLI, previously changing directory to MyProject\webapp and executing SBT from there. However, I don't see any option to set root dir for SBT console in IntelliJ. I have entire project imported into workspace, so the default project root directory is MyProject which is obviously not treaded as SBT project.
Is there any way to change "working directory" for IntelliJ SBT plugin?

I had the same issue, here's how I got it going:
Start by adding your root as a new module. In your case it will MyProject. I was careful to add this as a blank module, but if you already have something like a pom file (which begs the question as to why you want to use SBT), then you might be okay letting IntelliJ import it for you.
Next, add a scala file to your root project directory to map the sub-projects. A great explanation on how to set one of these up can be found on the scala wiki
import sbt._
import Keys._
object HelloBuild extends Build {
lazy val root = Project(id = "MyProject",
base = file(".")) aggregate(submodule1, submodule2, webapp)
lazy val submodule1 = Project(id = "submodule1",
base = file("submodule1"))
lazy val submodule2 = Project(id = "submodule2",
base = file("submodule2"))
lazy val webapp = Project(id = "webapp",
base = file("webapp"))
}
Start your SBT and you should now be able to switch between projects. If you have it up and running, make sure you use the reload command to apply the changes.
You can list the projects SBT recognizes as modules with the projects command. Switch projects by using project [projectName]. So to switch to submodule2, just type project submodule2.

Related

intellij 2020.1 sbt mainRunner configuration

I am trying to follow the intructions for setting up intellij scala project to work with sbt. However, I am not finding the run/configuration described in intellij 2020.1. Based on this post I understand that the way this is configured has changed. However, that post describes how to make old project work. What do I do for new projects?
Steps to Reproduce
Create nice sbt project with idea.sbt already configured with mainRunner
sbt new tillrohrmann/flink-project.g8
this includes idea.sbt
lazy val mainRunner = project.in(file("mainRunner")).dependsOn(RootProject(file("."))).settings(
// we set all provided dependencies to none, so that they are included in the classpath of mainRunner
libraryDependencies := (libraryDependencies in RootProject(file("."))).value.map{
module => module.configurations match {
case Some("provided") => module.withConfigurations(None)
case _ => module
}
}
)
It also comes with a README.md that says:
You can also run your application from within IntelliJ: select the classpath of the 'mainRunner' module in the run/debug configurations.
Simply open 'Run -> Edit configurations...' and then select 'mainRunner' from the "Use classpath of module" dropbox.
Import project into intellij 2020.1
Now what? I cannot find a "Use classpath of module" dropbox in intellij 2020.1.
IntelliJ's Use classpath of module corresponds to the classpath of sbt's sub-project. To create a Run Configuration using the classpath of mainRunner project try
Run | Edit Configurations...
Click the plus button + to Add New Configuration
Select Application
Give it a name say WordCount
Under Main Class specify Scala class with main method, say, org.example.WordCount
Working directory should be the root of the project
Set Use classpath of module to mainRunner
JRE should be 1.8 or above
Note as an alternative to using mainRunner project you could also use root project but select the checkbox Include dependencies with "Provided" scope.

How to open pom.xml project using IDEA

I am trying to learn about annotation processing by looking at this sample.
I've cloned it to my local machine and used IDEA to open it.
However IDEA tells me:
Project SDK not set up.
After setting up Java SDK, the project is not opening correctly. So how can I open this project?
The project structure :
annotationprocessing10
|
|------factory
|-----annotation
|------pom.xml
|-----processor
|------pom.xml
|-----pom.xml
|-------factory-sample
I tried opening annotationprocessing10 (root) directly and also tried factory directly but neither worked. Here is a screenshot of the directory structure:
The given repo is not a maven module (no POM.xml in the base directory), the maven modules are the projects inside.
Make sure you import the modules inside the root directory in IDEA to identify it as a maven project.

How to get IntelliJ to associate Gradle sources with build.gradle?

When writing Gradle scripts for my Java project, specifically, when writing build.gradle files, IntelliJ does not recognize the Gradle API.
For instance, Gradle methods calls like apply, dependencies configure appear with a black line under them and it is not possible to navigate to method declarations, there is no auto-completion etc.
I managed to work around this by adding compile gradleApi() to the build's dependencies block. However, I don't want to have this explicit dependency in my code.
I tried editing IntelliJ's project structure and add a dependency on a Gradle library (tried gradle-core and gradle-all) to my modules, but that seems to have no effect.
Is there a way to make IntelliJ associate all build.gradle files with the Gadle sources?
I solved this problem as follows:
As mention in already posted answers, configure gradle
update gradle/wrapper/gradle-wrapper.properties file
change bin to all in distributionUrl i.e.
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-bin.zip
to
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-all.zip
OR
[optional] If you are using old version of gradle wrapper and wanted to upgrade, then execute
./gradlew wrapper --gradle-version 6.8.3 --distribution-type all
Update gradle task (if present in build file)
wrapper {
gradleVersion = '6.8.3'
distributionType = Wrapper.DistributionType.ALL
}
Before importing the project to IntelliJ-Idea IDE, update build.gradle and add java and idea plugin to the plugins list
plugins {
id "java-library"
id "idea"
}
From a terminal, execute ./gradlew clean build idea or simply ./gradlew idea
Import project to IntelliJ idea.
Go to Preferences --> build,Execution,Deployment --> BuildTools --> Gradle
You can see
Restart IntelliJ idea IDE.
So above we have configured both of the options so choose either of them, except the specified location option. That's it.
Before
After
Autocomplete functionality as mentioned in this answer.
I had similar frustrations with Grails 3, which defines and runs a wrapper task when an app is created. Changing to the "all" zip in the wrapper properties file did not work because this kept getting changed back to the "bin" zip.
This was solved when it was understood that the "gradle-wrapper.properties" file simply stores the values from the "wrapper" task, and if this task is run after the properties are changed, they get changed right back.
This is easily fixed by setting some properties on the wrapper task:
wrapper.gradleVersion='3.2.1'
wrapper.distributionType=Wrapper.DistributionType.ALL
Now importing the project into IDEA gives you smart editing of your build.gradle.
when I choose build.gradle in IDEA and open it, IDE prompts
You can configure Gradle wrapper to use distribution with sources. It will provide IDE with Gradle API/DSL documentation.
I choose Ok, apply suggestion!
after project refreshing I am able to use code completion
before you import your project, configure it to use the customizable gradle wrapper as per the instructions here :-
https://docs.gradle.org/current/userguide/gradle_wrapper.html
add a task to your top level project like this:-
task wrapper(type: Wrapper) {
println "Wrapper gradleVersion = '2.12'"
gradleVersion = '2.12'
}
or whatever the latest version is.
make sure you can build the project from the gradle command line before you try importing into intelliJ, using the ./gradlew command, which will download and install a gradle distribution for you the first time you build.
set your java home, intelliJ home and gradle home variables in your machine and in intelliJ (mine look like this, yours may be different depending on your setup and your history of hacking around your machine...:-
(from .bashrc
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home
)
When you do import, choose the customisable gradle wrapper. if all is well, when you open the top level build.gradle for your project, you will be asked to configure sources for the gradle dsl, which will also update your gradle wrapper properties file to this:-
#Thu Mar 31 14:04:00 BST 2016
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.12-all.zip
.. the change being from ... bin.zip to all.zip. and that's it. This had been giving me lots of grief for a long time, but that's the way to do it. (on IntelliJ IDEA 2016.1 CE at least...)
most of this was in
Dimitry's answer too, but I couldn't get it to work using the default wrapper , it had to be the customisable wrapper.

Play Framework 2.4 and IntelliJ Idea

I am trying to open a play 2.4 project in IntelliJ but since things have changed I don't know how to do this.
In previous versions I could just run
activator idea
Or use the activator UI and click on generate intelliJ project, but in 2.4 the idea command doesn't seem to exist
[error] Not a valid command: idea (similar: eval, alias)
[error] Not a valid project ID: idea
[error] Expected ':' (if selecting a configuration)
[error] Not a valid key: idea (similar: clean)
[error] idea
[error] ^
And the UI seems broken, when I click on generate intelliJ project it tries to compile the app and gives this error:
play/Play$
java.lang.NoClassDefFoundError: play/Play$
Use 'last' for the full log.
Failed to load project.
I created the project from scratch using the play java template with:
activator new
I have also tried importing the folder as a project but intelliJ doesn't seem to identify it as a project
I run into the same problem, as I used IDEA to open a project folder, it had an play 2 app in a sub folder, then I import module (play 2 app) to the system.
And it works well.
After that I have changed the module folder name, then when I run the app, it displayed:
Not a valid project ID: "project team"
I re-checked the folder, and found that in the File -> Project Structure option, the name of module is "root", and the "team" is the module for whole project (not the module imported by SBT), so apparently, the module wasn't functional after I changed the module folder name.
So I found and removed all .idea folder, which is IDEA configureation, then re-open/re-import the module, still not work. I thought it's IDEA cache issue, it do have cache for the opened project, so I changed the project folder from team to something else, clean the .idea folders, and re-open/re-import it. It worked.
If the play app is in the project folder as a sub folder, to import the module at File -> Project Structure.
The project name should be "root" when running it in IDEA. So in this case, you should rename "project team" to "root" in the name field.
Solution 1
In my case (IDEA 2018.2), I changed the lazy val variable in build.sbt, it had the name "root" when my project name was "top", changed "root" to "top".
Before:
lazy val root = (project in file(".")).enablePlugins(PlayScala)
After:
lazy val top = (project in file(".")).enablePlugins(PlayScala)
Change project variable
Solution 2
The method proposed by Tom solved my problem partially, because after rebooting the IDEA I returned the project name back, it was necessary to change the variable from "top" to "root" in build.sbt.
Before:
name: = "top"
After:
name := "root"
Intellij IDEA lets you quickly create a Play application without using a command prompt. You don’t need to configure anything outside of the IDE, the SBT build tool takes care of downloading appropriate libraries, resolving dependencies and building the project.
Before you start creating a Play application in IntelliJ IDEA, make sure that the latest Scala Plugin is installed and enabled in IntelliJ IDEA. Even if you don’t develop in Scala, it will help with the template engine and also resolving dependencies.
Basically, install Play Framework, Scala plugins and import project into Intellij as SBT project. Don't use activator to create IDEA project files. More details here.
It seems I had not updated scala/sbt to the latest version in intelliJ
Once I had done this it noticed that it was a valid project, though the docs don't seem to mention you can import it as an SBT project, just how to create it as a new sbt project (which I did not want to do as I wanted to create it via activator)
I also had the project/play-fork-run.sbt file issue
I use Intellij Idea 16.3.3 and this problem appears sometimes. How I fixed it? You just need to open your SBT plugin and under your project get the context menu then you should choose "Ignore SBT project". After a current process is finished you should turn on this option again. This is work for me:)

how to add a jar to my lib/ directory in intelliJ and have the classes available for my web.xml?

I have tried many combinations and every time I try to add the jars from the lib directory, intelliJ puts the path down as the src directory. I don't know why. When I move the jars to the src/ directory, intelliJ then switches the path to the lib/ directory. I am not sure how to get this to work correctly.
I'm using Intellij IDEA 11.1, trying to add the Selenium Standalone Server 2 JAR to my new Java project, which already has a module. I could not add the .jar to the Project settings Module (as suggested in another answer), but I can add it to either the project or global libraries. This appears to be the best place for it in Intellij IDEA 11.1.
To add the JAR to the project library (individual modules):
Ensure that the project to which you want the JAR added is open, then choose: File > Project Structure > Libraries > [+] (New Project Library) > Java > .../selenium-server-standalone-2.24.1.jar > [Select the module to add the JAR to] > OK
To add the JAR to the global library (so it's used for all projects):
File > Project Structure > Global Libraries > [+] New Global Library > Java > .../selenium-server-standalone-2.24.1.jar > OK
My Selenium project now compiles and runs as expected. (-:
Project structure -> Modules -> -> Add... -> Library -> Java -> Attach Jar Directories... (for IDEA 10). Are you doing the same?