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.
Related
I would like to add Groovy support to my project in Intellij Idea 2018.1. But when I open "Add Framework Support..." from the project context menu, I don't see "Groovy" in the list of frameworks:
I have Groovy installed on my machine and configured it as a global library in in IntelliJ:
Not sure whether this should be possible, but I also cannot add Groovy as an SDK:
I added Groovy as a module dependency:
My underlying problem is that IntelliJ does not properly recognize Groovy code, e.g. I get
Cannot resolve symbol `String`
when I use the String class:
Any hints on how I can make Groovy available as a Framework and an SDK?
Solution
Thanks to CrazyCoder pointing me to the broken JDK. Fixing the JDK and adding Groovy as a module dependency fixed the problem. See https://www.jetbrains.com/help/idea/creating-and-managing-modules.html
Make sure your JDK configuration is correct. As #crazycoder commented, OP's is red indicating the path might be incorrect.
Groovy can be added as a module or library in Intellij or dependency in its dependency file (pom.xml, build.gradle etc)
1. Module (in Intellij):
File > Project Structure Ctrl+Alt+Shift+S > click Modules > Dependencies.
2. Library (in Intellij):
Go to File > Project Structure... > Libraries > Click on the plus sign on the leftmost side > Add the Groovy jar or maven source at your choice
3. Dependency (build.gradle):
group 'com.test'
version '1.0.0'
apply plugin: 'groovy'
repositories {
mavenCentral()
}
dependencies {
implementation "org.codehaus.groovy:groovy:2.4.5"
}
I am currently developing a plugin for IntelliJ and trying to use another built-in IntelliJ Plugin as dependency (git4idea). As described in the IntelliJ Plugin Development documentation, I added the required JARs to my class Path in Project Structure:
Project Structure Dialog Screenshot
I also added <depends>Git4Idea</depends> to my plugin.xml file.
IntelliJ finds these jars now and Code Completion works well, no errors found...
But when I try to build the Plugin with gradle I get ClassNotFound errors or errors like this:
TkGitflowBaseImpl.java:15: error: package git4idea.commands does not exist
import git4idea.commands.Git;
^
Obviously, Gradle does not find these jars. Since they are part of the IntelliJ installation, I can't just add them to a lib folder and add them as local jars in the build.gradle file. As Gradle JVM, I chose the exact same JVM I chose as JVM behind the IDEA Platform SDK, so the jars should be available to Gradle.
Do you know how I can help Gradle find these jars or add them as "provided" dependencies without adding them to a lib folder?
I am using IntelliJ IDEA 2017.2.5 and Gradle 4.2.1
After reading through the documentation of the IntelliJ Gradle Plugin (https://github.com/JetBrains/gradle-intellij-plugin), I saw that that a "=" was missing in the build.gradle file like:
intellij {
version '2017.2.5'
pluginName 'pluginname'
plugins = ['Git4Idea']
}
instead of
intellij {
version '2017.2.5'
pluginName 'pluginname'
plugins ['Git4Idea']
}
https://plugins.jetbrains.com/docs/intellij/plugin-dependencies.html#optional-plugin-dependencies
build.gradle
intellij {
version = '2021.2'
plugins = ['Git4Idea']
}
src\main\resources\META-INF\plugin.xml
<depends>Git4Idea</depends>
tl;dr: I cannot configure IntelliJ to generate the java files in the same directory as gradle
I have a small project which uses the immutables annotation processor.
It works as expected in the gradle command line build, but I cannot get IntelliJ to output the generated files to the same directory.
The full project is available on GitLab
Gradle config:
I use the folowing gradle plugins:
gradle-idea plugin which handles the idea configuration
gradle-apt-plugin which provides the apt configuration and handles the compile-class path and idea config related to annotation processing (if also the idea plugin is applied)
relevant parts of the build-script (link to the full listing):
apply plugin: 'java'
apply plugin: "net.ltgt.apt"
apply plugin: 'idea'
dependencies {
def immutablesVersion = '2.3.9'
compileOnly "org.immutables:value:$immutablesVersion:annotations"
compileOnly "org.immutables:encode:$immutablesVersion"
apt "org.immutables:value:$immutablesVersion"
}
when I start ./gradlew build everything is as expected:
The source file DataEncoding.java is processed an the generated java-file DataEncodingEnabled.java ends up in
/build/generated/source/apt/main under the expected package com.tmtron.immutables.data
and the generated file is also compiled to a .class file
In IntelliJ I activate the annotation processing as suggested by the gradle-apt-plugin docs:
Then I execute ./gradlew clean to make sure, that the previous files are gone and then I click Build - Build Project in IntelliJ.
The annotation processor is executed, but the problem is that the generated java file ends up in the wrong location:
It is in: /build/generated/source/apt/main/build/generated/source/apt/main/com.tmtron.immutables.data
the bold part is redundant.
What am I doing wrong and how can I set it up correctly, so that IntelliJ and gradle generate the files in the same directory?
Notes:
I have of course already tried to just leave the "Production sources dir" in the IntelliJ annotation configuration empty, but his does not work: then it automatically uses "generated" and I also end up with a wrong path.
IntelliJ version 2016.3.4
Now https://github.com/tbroyer/gradle-apt-plugin states:
The goal of this plugin was to eventually no longer be needed, being superseded by built-in features. This is becoming a reality with Gradle 5.2 and IntelliJ IDEA 2019.1.
So:
dependencies {
compile("com.google.dagger:dagger:2.18")
annotationProcessor("com.google.dagger:dagger-compiler:2.18")
compileOnly("com.google.auto.factory:auto-factory:1.0-beta6")
annotationProcessor("com.google.auto.factory:auto-factory:1.0-beta6")
compileOnly("org.immutables:value-annotations:2.7.1")
annotationProcessor("org.immutables:value:2.7.1")
}
compileOnly is necessary if you use annotations, compile if you use classes, annotationProcessor introduced in Gradle 4.6.
To enable processing specific compile task:
compileJava {
options.annotationProcessorPath = configurations.annotationProcessor
}
To disable:
compileTestJava {
options.compilerArgs += '-proc:none'
}
UPDATE 2.2019
since Gradle 5.2 there is an easy way to do it - see gavenkoas answer
UPDATE 5.2018
The easiest way, I know of is to use the apt-idea plugin
Just activate the plugin in the build.gradle file:
plugins {
id 'java'
id 'net.ltgt.apt-idea' version "0.15"
}
and then add the annotation processors to the annotationProcessor configuration:
final DAGGER_VER = '2.16'
dependencies {
implementation "com.google.dagger:dagger:${DAGGER_VER}"
annotationProcessor"com.google.dagger:dagger-compiler:${DAGGER_VER}"
}
Test-project on GitHub: ex.dagger
(using IntelliJ 2018.1.4, Gradle 4.7)
ORIG ANSWER
There's a simple workaround using the parent-dir which works fine in IntelliJ 2016.3.4
Production sources directory: ../main
Test sources directory: ../test
Now gradle and IntelliJ will generate the code to the same directories.
Fixed in GitLab project V0.0.2
see also: apt-gradle-plugin issue#35
Hey there everyone I had the same issue and found a clean way of solving this issue.
I am using two libraries that require annotation processing (Lombok and MapStruct).
Also my IntelliJ is 2019.1 (update yours in case it's older) and Gradle 5.2.1.
First let's configure IntelliJ:
Disable Annotaion Processing in Settings, since we're going to delegate everything to Gradle:
Delegeate IDE actions to Gradle:
Last step is to configure your dependencies correctly in Gradle.
Dependencies section in Gradle:
Now you can execute the Build and Run from both command line and IDE.
Cheers!
2019.2.x
Disable annotation processor of intellij
add, build directory in your gradle build.gradle file
then run your gradle task to generate build file classes, example gradle compileJava
File -> project structure -> Modules -> Main Folder || remove exclude and add as source
And project should find all annotation and generated source file. Hope it helps.
I am developing a plugin for IntelliJ IDEA. The way I am going about this is by creating a plugin project in IDEA, then packaging this into a jar with appropriate META-INF/plugin.xml, and installing the plugin from the jar.
The problem is that I would like to add a dependency on org.scala-lang:scala-library:2.11.0. I have this specified as a library dependency in the IDEA project, but this information never seems to get passed along to the generated JAR.
How can I include this information in such a way that IntelliJ IDEA will recognize it?
As far as I understand, you want to bundle some library (e.g. scala library) with your plugin.
This is pretty simple.
Go to Project Settings, select module and go to Dependencies tab. Set scope for the library you want to bundle to 'Compile'. In this example it is 'checker-framework' library. 'groovy-2.3.6' library will not be bundled due to its scope set to 'Provided'. Save changes.
Prepare plugin for deployment
Then you got plugin, zipped, ready for deployment (uploading to repo or installing locally) in the root of project. It will contain lib folder with all necessary jars.
The officially supported plugin dependency management solution is to use Gradle with the gradle-intellij-plugin, via Gradle's dependencies or the intellij.plugins entry points. If you want to add a dependency on an artifact (ex. hosted on Maven Central), then configure dependencies just as you normally would in a Gradle based project:
buildscript {
repositories {
mavenCentral()
}
}
dependencies {
compile("org.scala-lang:scala-library:2.11.0")
}
The intellij.plugins entry point will add an artifact in the current project, as well as a <depends> tag to your plugin.xml file. To install an external plugin alongside your own, for example if you are using the Plugin Extensions feature (suppose the plugin is hosted on the JetBrains Plugin Repository), use the following snippet:
plugins {
id "org.jetbrains.intellij" version "0.2.13"
}
intellij {
//...
plugins "org.intellij.scala:2017.2.638"
}
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.