Global libraries in Intellij IDE - intellij-idea

I have added a Java library in the Intellij IDE, but every time I start a new Java project I can't import this library, and have to set it up over again.
This is a screenshot from Intellij Project Structure:
The library is there, but when I try to import it I get an error:

Setting up a Global Library ensures that the library is defined, however it is not automatically added as a dependency to new projects.
You must go into Project Structure -> Modules, click the Dependencies tab and press the green + to add it to your project.
Or, instead, (and if IntelliJ is smart enough) after hitting F2 to navigate to one of the compile errors in your source code, Alt-enter might allow a quick fix to add the dependency (you'd have to try it...).

Related

How to import a Kotlin Library in IntelliJ

So i want to use the Serialization Library in my kotlin project, and from what i can gather from this page, i must use this: "org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.2", to do so, now i use "IntelliJ" as my build system, so i went to the project structure, and in libraries, clicked the + sign, clicked on "From Maven" and put this as the link to the library, and it downloaded it and i set it as a dependency of my project, it even shows as a external library, like this:
But when i try to import the lib, it gives error:
Apparently you can indeed use IntelliJ IDEA as a build system without maven or gradle, but I don't think this is a supported way to use kotlinx-serialization.
I'd advise to pick either maven or gradle as your build system for your project (this can be done through IntelliJ, just create a new project and pick either one).
Then you can just follow the relevant paragraph at https://github.com/Kotlin/kotlinx.serialization#setup for either maven or gradle.

IntelliJ (2020.2) - How disable 'Build project automatically' for a project based on Gradle?

I am an Eclipse/STS user/developer, now trying to use IntelliJ Idea (CE)
2020.2.(1,2,3)
For a project based on Gradle, how spring-integration, when I open the IDE it happens the following
Ok, let the IDE load the project ... but
From above, that is the problem, I don't want that the IDE starts automatically to build/rebuild the project. I just need, open the project and that's all.
Observation: for example in Eclipse/STS exists the option to disable Build Automatically
I did do a research in the Web and I read the following posts and questions:
How to disable automatic gradle builds?
IntelliJ IDEA “Build project automatically” apparently not working
Intellij IDEA Java classes not auto compiling on save
Sadly the dialog options were changed but ...
Therefore:
From above, seems nothing to do.
Observation: from above observe the Build project automatically option is disabled
Even with that disabled and after to restart the IDE, I must always stop manually the build process
So what is missing? or Do I need a special extra plugin to accomplish my goal?
The images that you show indicate that you are building with Gradle, but the Compiler option that you disable is relevant for building projects with Idea not with Gradle.
For the 2020.2 version, you need to do the following:
Open the Setting > Build Tools page.
Disable the "Reload changes in build scripts" option.
This way you can manually control the reload. When you change the build script, you will see a small gradle icon in the right side of the editor.
For more info, refer to the IntelliJ IDEA help > Gradle section.
https://www.jetbrains.com/help/idea/work-with-gradle-projects.html#auto_reload
There are two different things in IntelliJ's Gradle support that sometimes confused: sync and build. Your pictures demonstrate sync process (note caption on the toolwindow). Word build is kind of misleading here.
What is sync? In gradle we use Groovy to define the build procedure. Groovy is an imperative programming language, so it's hard to predict resulting dependencies graph without actually executing the script. During the sync Idea executes configuration phase of gradle build (one that builds dependency graph), and obtains configured objects from the Gradle daemon. This information is used to setup project in the IDE: modules, libraries, dependencies, which sources are test, which are prod, etc.
Actual build is not happening during sync. You can convince yourself by adding syntax error to any source file, and observe that the sync succeeds. But build will fail if you invoke it.
In answer to the original question: you can't disable automatic build, because it is not enabled.
Is it possible to disable sync in Gradle project? Short answer - no. If you need a code browser, which is not required to understand all the cross-references in the source code, IDEA is not the best choice probably.
TL;DR;
Without sync IDE does not know which files are sources, and which are not. IDEA cannot open folders. It only can open projects. Good thing is that module can contain folder. So you can do the following: File | New | Project. Select Empty project, Next, select some random folder outside the source folder you want to open, Finish.
Then add new module:
Select Java in the left panel, everything else keep default, Next, Finish. Then in new module remove existing content root, and add folder with sources as new content root
Resulting project is mostly useless. Tons of red code (at least, unresolved symbols from external libraries), no inspections, no navigation, no sense. But it might be useful in some rare situations indeed.

How to make IntelliJ reference source code in another module?

I have a typical project structure:
- root-dir (not a project)
\- core-module (a gradle project)
\- application (a gradle project)
Both core-module and application are imported into IntelliJ and kept on Auto Import. In application's build.gradle, core-module is referenced as:
compile('my-group-id:core-module:0.2.0-SNAPSHOT')
where 0.2.0-SNAPSHOT is the current version as declared in core-module's gradle.properties.
In application, when I try to view a class from core-module, I'm brought to the source code in core-module-0.2.0-SNAPSHOT-sources.jar, instead of the corresponding source code in the core-module module.
I know I can manually add core-module as a module dependency in application, but next time anything in application's build.gradle changes, auto import will overwrite that dependency.
Is there any way to make IntelliJ recognize automatically that I'm trying to view a class from another module and go there instead of the downloaded sources jar?
Furthermore, is there any way to make IntelliJ always prefer the core-module module over the dependency jar, not only for code viewing, but for building/running/debugging/etc.?
All the source files are in Kotlin, FWIW.
Use Gradle's composite build. Gradle has composite builds https://docs.gradle.org/4.4/userguide/composite_builds.html that allow one to 'include' a build directly, rather than from a repository.
IntelliJ also supports this functionality. This was added in 2016.3.
Here's a webcast:https://blog.jetbrains.com/idea/2017/03/webinar-recording-composite-builds-with-gradle/
In the Gradle tab, right-click on your application module. The menu will have a 'composite builds' option. On the dialog that appears, check the 'core-module' module and close.
Now, right-click on application module and do a 'refresh Gradle project'. I've found if I don't do this, the dependency doesn't get updated correctly.
To verify, look at the dependencies under the sourceSets. Instead of a version #, it will now look like a module dependency.
This provides many benefits. One is the navigation you were looking for. In addition, any changes made in core-module are immediately available, and used for the application.
Refactor a method in core-module that is used by application, and IntelliJ will refactor all usages.
Enjoy!

SBT compilation for play framework 2.x disabled by default

I created a new Play Framework project using the activator, activator new my-project-name and then I selected the play-java template and the project is created with no errors.
Then I import the project into my IDE, IntelliJ, as a SBT project. When the project is imported a popup is shown saying: SBT compilation for play framework 2.x disabled by default.
On IntelliJ project's panel it doesn't show all (mostly the files with the code) files. Just a handful of them.
The build.sbt file looks like full of compilation error (pretty much all the lines are red underlined).
Here's the build.sbt file
name := """my-project-name"""
version := "1.0-SNAPSHOT"
lazy val root = (project in file(".")).enablePlugins(PlayJava)
scalaVersion := "2.11.6"
libraryDependencies ++= Seq(
javaJdbc,
cache,
javaWs
)
// Play provides two styles of routers, one expects its actions to be injected, the
// other, legacy style, accesses its actions statically.
routesGenerator := InjectedRoutesGenerator
fork in run := true
I can compile and run the code with activator ui with no errors. So, why isn't IntelliJ fully importing the project and it shows me SBT compilation for play framework 2.x disabled by default message?
I googled around and couldn't find any hint related to this so I'm kinda lost here.
EDIT: -------------------------------------
I closed the project on IntelliJ and deleted the project folder. Closed IntelliJ and using activator I created again the same project. Using activator ui I compiled the project. Then I fired up IntelliJ and was prompted to either open a project, create a new one or import a project.
Selected import a project and did the normal "import a project steps" and IntelliJ imported the project with no hassle. But if I try to import the project while with an open project on IntelliJ I get the same problem that lead me to make this question. Maybe this is a bug on IntelliJ?
EDIT 2 ------------------------------------------
The solution to these two problems (the "SBT compilation for play framework 2.x disabled by default" and the files not showing on IntelliJ) can be fixed with user3384225 and DarkShadow answers.
Preferences > Languages & Frameworks > Play2
Under 'Compiler' enable 'Use Play 2 compiler for this project'
I Stumbled upon this problem myself when configuring play framework for IntelliJ IDEA IDE (version: 2017.3). I overcame this problem by following the steps below.
Navigate to:
File --> Settings --> Languages and Frameworks --> Play2 --> Compiler tab
and inside the compiler tab:
put a tick to the Use Play2 compiler for this project
Hope this helped.
Similar answer to user3384225's answer was posted here:
https://devnet.jetbrains.com/message/5560914
They say:
Settings -> Langauges & Frameworks -> Play2 -> Compiler -> User Play 2 compiler for this project
I am also a newbie and was facing the same problem till yesterday. Probably you are trying to open the project through File->open.
Try the steps below:-
File->New->Project from existing source->select the build.sbt from the project you created using the activator->Import project from external model and select SBT from the options->OK.
It will take some time to import files and to create the settings required for intellij idea project.
I am using intellij idea 14.1.5. It worked for me hope it will for you also.
For current projects, you can fix this by using File->Invalidate Caches/Restart..
Most probably you have solved you problem, but just to help others
What you just have to do is -> check the option "Use Play 2 compiler for this project" given under Preferences -> Languages & Frameworks -> Play2 -> Compiler
Close the project and then import the project as sbt project.
Intellij will automatically dump and index the project dependancies.
Stumbled upon the same problem today. Solved by wiping the ~/.IntelliJIdea14 directory. Unfortunately any other things I've tried didn't help.
Just use: File->New->Project from existing source->select the build.sbt from the project you created using the activator->ok.
and it's done.

Can't replicate simple akka project in intelliJ

I am a new scala user and am having problems getting a development environment functional.
I downloaded the typesafe activator which launches an editor in a web browser and was able to run the akka actor tutorial script fine.
However, I have set up my intelliJ scala/akka environment, and when copying the same sample code to intelliJ, I am getting compile errors. I have added akka 2.2-M1 via maven to my project.
Initially when I copy
import akka.actor.{ActorSystem, Props, Actor, Inbox}
the last "Inbox" library is highlighted in red, and on compile I get "Error: object Inbox is not a member of package akka.actor. It suggests that I add akka.actor.dsl.Inbox. When I do that the import is greyed out because it is "not implimented", and get a compile error further down in the script when I go to implement inbox. It says "
not found: value Inbox
val inbox = Inbox.create(system)
"
What am I doing wrong? Thanks!
I would recommend you use sbt and then use the gen-idea plugin to create your intellij project.
You'll need to re-run gen-idea every time your dependencies change - intelliJ currently won't automatically discover and resolve managed dependencies for you.
here is the plugin with directions.
https://github.com/mpeltonen/sbt-idea
also, as a professional I would recommend you use the sbt console for most of your tasks.
Ctrl-tab 0 will let you hop over there without touching your mouse.
Escape brings you back to the code editor window.
You can run sbt ~test to test on change to your files etc.
Note you may need to change focus off of intellij for it to save the file in memory if you're use ~test
I managed to resolve the same issue by adding the following library from the Maven repository
com.typesafe.akka:akka-actor_2.10:2.2-M3
to File -- Project Structure -- Libraries