Kotlin/IntelliJ Modules with Shared Content Root - intellij-idea

I'm working on a project in IntelliJ Idea using Kotlin. I'm trying to create a unit test for a Kotlin class, and I can create the class, except that when I run the test, I get an error that there is no JDK configured. I go into the project structure and change the JDK for the module, but when I click on Apply, I get a message saying that :
Content root
"C:\Users\2rtmar\Documents\xproject\xproject\examples\src\main" is
defined for modules "xproject-examples-utils_main" and
"xproject-examples-java_main". Two modules in a project cannot share
the same content root.
Another team member had said that these modules were faulty, and that they couldn't be used, but I'm not using them and still prevent me from running my code. I tried to unmark this module as a sources root, but did not fix the problem. I went as far as to removing the xproject\examples module entirely, but received the same message when trying to set the JDK of my module.
Any help is appreciated!

In my case I was trying to setup a multi-module Gradle project by adding a new module to my root Gradle project.
Despite adding an include ':my-submodule' to my settings.gradle, it
was always telling me gradle wrapper not found, because the submodule was kind of outside of the context of the root module.
This however is IntelliJ-specific
When I looked into Right-Click on module -> Open Module Settings - > Modules I was seeing the following hierarchy:
my-root
-> my-submodule
my-submodule
For whatever reason, IntelliJ has added the new module as a child and as a standalone module, and this caused the issue Two modules cannot have the same content root, as my-root:my-submodule and my-submodule are now both pointing to the same src/main directory.
This is IntelliJ's out of the box behaviour when adding a new
submodule, which is in my opinion totally wrong and likely a bug! You
cannot even fix it within the module settings
Solution
But I discovered a way to fix it.
Right-click on the module
Load/Unload Modules
Now unload the module that is not below the my-root
After that, there is just one single root pointing at the src directory and the active module is now within the parent's Gradle scope.
I hope Jetbrains fixes these issues. Those problems exist for a while now and are not pleasant to solve.

I got this issue recently.
The very first check should be to check if any other module with the same content root is added or not.[you can check this in Project Structure -> Modules]
If YES, remove the other project by clicking on - (minus) button at the top.

If you're using Intellij and have modified / are modifying the pom.xml files directly:
Go to the affected sources' pom.xml files
Go to the following area:
...
<build>
...
<resources>
<resource>
...
<directory>[DIRECTORY]</directory>
...
<includes>
...
</includes>
...
</resource>
</resources>
...
</build>
...
Change the [DIRECTORY] to something unique. For instance, I was referencing ../plugin-outputs. As such, I changed it to ../plugin-outputs/[specific sub-directory], wherein I named the sub directory as I wished.
Delete any "modules" listing the specific output directory. No, they aren't modules... however, they have the module icon applied to this, preventing the pom.xml files' changes to be updated while editing modules through the standard project structure tab.
I know this thread is 2 years old; however, there are MANY people who still read these artifacts... hopefully this helps someone.
-Dan

Related

How to import a project with modules and java packages in IntelliJ?

We have a java project with several modules. We use git for versioning and want the developers to be able to chose their own IDE. So we don't push .idea and .iml files with git. This means that developers in IntelliJ needs who clones from git needs to set up the project in IntelliJ as well. However we are having some issues with setting it up. In my own project it works fine and looks like:
Proejct Module (Is this a real module?)
⤷ Module A
⤷ src
⤷ main
⤷ java
⤷ Package 1
⤷ Package 2
⤷ test
⤷ Module B
Before we added packages to the project new developers could just pull from git and open Module P in IntelliJ, then go to project structure and import module from Module A and Module B. However if they try the same thing now IntelliJ tries to make the Modules main and test from Module A.
A workaround we temporarily use for this is to open Module A and B directly as separate IntelliJ projects. If we do this IntelliJ does not try to make modules out of main and test. However this is very inconveniant for us.
How can we fix this?
It's a good approach to make your source control system "agnostic" about the IDE and not check in .idea, *.iml and equivalent things from other IDE's.
However you really need to use a build control system like maven or gradle.
In my workplace we use maven for all our java projects. It's very simple to define a pom.xml at the root level which defines your modules, and many other things.
When you import a maven project into intellij, all the relevant IntelliJ modules are automatically created for you. In essence, you only need to import the pom.xml and you're good to go.
Otherwise it will be a nightmare to import and create modules every time you check out source from git. Maven isn't perfect but I highly recommend using either maven or gradle.
To define multi-modules from the root pom.xml, make a section like this:
<modules>
<module>module-A</module>
<module>module-B</module>
</modules>
And then the pom.xml in both module A and B should define a section referring to the root pom via the <parent> tag.

"Class already exists" error in IntelliJ on Groovy class

in IntelliJ (2016.2 and previous) we have our Groovy classes marked red with the error "class already exists".
I think we can exclude that the cause is the stub-generation, as this is deactivated.
Probably it's caused in our constellation: We have included our compiled groovy (and java) classes in a jar that is registered as dependency.
Dependency MyProduct.jar contains com.mycompany.MyGroovyClass
Our source contains com.mycompany.MyGroovyClass
The error disappears if the dependency is registered with Test-Scope, in all other scopes the error appears.
However, in our structure we kinda have to include the compiled classes in a compile scope, as we want to avoid that each developer needs to compile all classes (I know about the compile in background ability, but we have a constellation that prevents this from working).
We have no errors in com.mycompany.MyJavaClass which exists as well in source and in MyProduct.jar.
Any ideas on how we can solve this?
We've been suffering the same issue, it seems to be that IntelliJ registers the Java class, but also the Groovy class, and because of that it is showing that message (BTW, we are using a Maven Project).
So we ended up by going to the target folder -> right click -> Mark Directory As -> Excluded. Then, this setting will be saved on the IML file, and it won't happen again.
Hope it works for your as well!
Cheers
I'm using gmavenplus-plugin:1.5
After marking target/generated-sources/generated-sources/main as "Excluded", The error disappeared. I even did "invalidate cache and restart", It persists the setting. This is great. Intellij 2017.1.5
We have two ways to fix this issue
Exclude Stub Directory
target folder -> generated-sources -> groovy-stubs -> Right click main folder -> Mark Directory As -> Excluded
Remove generateStubs goal from gmaven plugin
Remove <goal>generateStubs</goal> from gmavenplus plugin
Make sure you Mark the src folder as Sources Root and do the same for the test folder
Then delete the target folder (most likely it's marked in yellow) and don't worry it won't delete any code from your project
If the issue persist, proceed to go to File -> Invalidate Cache/Restart

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:)

Dependency Management issue in Intellij idea 12.0.4

I am facing a problem modifying dependencies within Intellij12.04.
Under my project folder, I have two modules, the second of which depends on the first:
a.b.c.somebusiness
---> a.b.c.tests
I want to remove one dependency from a.b.c.tests and add it into a.b.c.somebusiness module, but I don't want to create a circular dependency tree. When I try to remove the dependency from the a.b.c.tests module and add it into the a.b.c.somebusiness module, I am getting the following error:
"Source root "C:\Perforce\depot\Projest\Main\a.b.c.tests\src\test" cannot be defined in module "Main" because it belongs to content of nested module "a.b.c.tests""
I don't know what to do to resolve this issue. Can anyone suggest an approach that would fix the error I'm receiving?
You should look at using apache maven and the project object model (pom.xml file)
When you define your dependencies under <dependencies> in the pom.xml, they are managed automatically by IntelliJ. So you will never have such problems from moving dependencies into file folders manually.
If you configure it similar to the fashion shown below, your dependencies will work automagically.
<dependencies>
<dependency>
<groupId>a.b.c</groupId>
<artifactId>a-b-c-tests</artifactId>
</dependency>
<dependency>
<groupId>some.other.org</groupId>
<artifactId>some.other.dep</artifactId>
</dependency>
</dependencies>
How to get started: install apache maven and add a pom.xml at the root of your project. Right click it to add in as a maven file in IntelliJ.
Good luck!
The best approach to isolate issue faced is to use Apache Maven. It gives following advantages and will be easy for you to manage dependencies in a cleaner and efficient way. Hope this will help a new comer.
The task of downloading Jar files and other dependencies is done automatically.
In Maven, it’s easy to add new dependencies by writing the dependency code in the pom.xml file.
Makes it easy for the developer to build a project in different environments without worrying about the dependencies, processes, etc.
Having said that, please follow these steps to fix the issue.
Go to InteliJ IDEA
Right click the Project and Click Add Framework Support and check
the "Maven" option This will create pom.xml file for the project
Double click the pom.xml file and include dependencies as needed.
(The dependencies could be found by searching via
https://mvnrepository.com/)
When the dependencies are added it will automatically download, if
does not, right click the project and select "Maven" and click
"Reimport".
Now you are project is added with the dependencies you have
specified.
Once above is completed, try to build the project in order to deploy/run.
Go to Run -> Edit Configuration -> Add New Configuration -> Select
Maven -> Fill the name etc. -> Type 'clean install' in the 'Command
Line' field and press OK.
Now you can run the Maven in the Run/Debug Configurations
The output can be seen in the target folder of your project
directory.
Check this out for more information - Official Page for Apache Maven:
https://maven.apache.org/index.html

IntelliJ IDEA and PlayFramework modules

After a lot of good comment about IDEA, I decided to give it a try. I downloaded the Community Edition and would like to use it for PlayFramework development.
I have followed official documentation and some other information gathered around, but I have not succeeded completely. When using a project with differents (play) modules, the different classes are not found.
For example when using Secure module, IDEA keeps complaining about not finding Secure.class. It has to be a classpath issue. I tried to attach Java source & classes ($PLAY/modules/secure/) in module settings (F4), class is still not found. Did I miss something?
BTW, I have done a play dependencies and play idealize, which seems to add another module Secure into project in IDEA.
Thanks,
The answer is to run the following:
play deps
play idealize
This forces the IDEA .iml project file to be refreshed with the updated class path entries for the new module (in this case Secure).
A issue I came across using IntelliJ and Playframework.
The log4j.properties file or log4j.xml file are not in classpath by default. You have to add the conf as a source folder in module settings.
You need to add the Secure module you have created in IDEA as a dependency to the main application module:
Go to File -> Project Structure
Choose the main module
Choose the Dependencies tab
Click Add -> Module Dependency
Choose the IDEA Module you created for the Play Secure module
Also make sure you have the correct source path selected for the Secure module in IDEA.