Reputation: 1267
I have cloned a spring boot project from GitLab having multiple branches in IntelliJ. I checkout to the branch in which I want to work. But I am getting "Java file outside of source root" on all the files. Please suggest what I am doing wrong. I just installed IntelliJ and it's the first project that I am importing.
Upvotes: 111
Views: 341365
Reputation: 827
I fixed it in this way:
File > Project Structure > Modules > Sources
and then marked java files dir as Sources.
Upvotes: 72
Reputation: 1234
A very simple solution: -
Click on "maven" on the right toolbar in IntelliJ and then click on the refresh button.
Update: Adding a screenshot on the New UI, but the location is the same on the old UI.
Upvotes: 97
Reputation: 60
Mark the directory as root solved my problem. Attaching screenshot.
Upvotes: 0
Reputation: 629
My new experience with this problem from September 2023 - the issue was:
I changed system Java from Java 17 to 1.8 and after that, I opened Inteliji and all subfolders could not be recognized as modules, and Java files were outside of the source root as a consequence.
Upvotes: 0
Reputation: 1275
I had the same problem in my multi-module maven project. Doing some steps mentioned under this question solved the issues but it was not permanent. The issue appeared everyday. In-order to fix this I had to go to the project settings as shown below.
After that, I did check the following check boxes.
I don't know whether this would solve the issue for every occurrence. However, for me this was the fix.
I am sharing this thinking this might help someone. Thank you!
Upvotes: 2
Reputation: 11
I tried all of the above. Finally figured out this is because of an issue with incompatible Maven version. Fixing the settings in IntelliJ to refer to a lower version of Maven and then Add as Maven project helped.
Upvotes: 1
Reputation: 562
In my case, I have ignored some projects and hence the pom.xml files also showed disabled. Hence I followed the below steps.
right-clicked those pom.xml files -> Maven -> Unignore Projects
Upvotes: 1
Reputation: 911
However there are many options already given, i tried 2-3 from above but it did not work as expected.Because, Intellij Idea keeps all workspace setting in .idea folder on root level of project and *.iml file under all sub-project to link the modules. To clean all the settings - either close IDEA or left idle for some time and from back using terminal executed below command - assume my root project is "ewfsa" -
cd ewfsa
rm -rf .idea
rm -rf **/*/*.iml
rm -f *.iml
and then select option "reload from disk" and if IDEA closed then open IDEA. every project reloaded and all missing setting restored.
Upvotes: 0
Reputation: 17
This is the solution that worked for me:
Upvotes: 0
Reputation: 515
For one project where build.gradle was configured for various custom build variants, I tried everything mentioned above but still it didn't work. Finally when I chose correct build variant from left panel of Android Studio, it just worked.
Upvotes: 0
Reputation: 527
Right click on pom.xml then click on "maven" and reload project
Upvotes: 6
Reputation: 1
Please do the below 2 steps:
Step-1: Right click on root pom.xml file Maven-> Generate Sources and update folders
Step-2: Right click on root project level pom.xml >> maven >> Reload project
Upvotes: 0
Reputation: 1
I had to change the Maven path in IntelliJ. Preferences-> Build, Execution, Deployment-> Build Tools-> Maven.
Using the bundled Maven 3 path solved this and the Spring Boot application started up just fine.
Upvotes: 0
Reputation: 11
Just for anyone who has the minor mistake that I had:
My simple issue and fix was to cd into the exact file I cloned down. I accidentally shortcut opened the parent directory with idea . instead of the project I needed to work on.
Upvotes: 0
Reputation: 327
pom.xml
> Maven
> Generate Sources and Update Folders
Here's an image of what the menu looks like:
Upvotes: 3
Reputation: 607
The solution that worked for me was to right click on the src folder and select "Mark Directory as" -> "Sources Root" or "Test Sources Root".
This is also mentioned in the comment to the question. But I had missed it so wanted to post it as an answer.
Upvotes: 3
Reputation: 11
Go to Intelij
File > Project Structure > Modules > Add
add module
Upvotes: 1
Reputation: 31
File -> Project structure -> Modules
Remove the whole module, and click the add button then select 'Import Module', choose the root maven pom.xml of your project.
This saved my time.
Upvotes: 3
Reputation: 5954
I'm having a similar issue for an Android project in Android Studio Chipmunk 2021.2.1 Patch 1. It looks like a known bug related to using manifest.srcFile
: https://issuetracker.google.com/issues/218370114?hl=ko
Apparently the options are switching to the canary channel or waiting for the fix to be released on the stable channel. Or getting rid of manifest.srcFile
in your build.gradle
.
Upvotes: 1
Reputation: 95
Resolved !! Go to your project on IntelliJ -> Right click on pom.xml -> Add as Maven Project.
Upvotes: 6
Reputation: 71
File
->
Project Structure
->
Module
select the Sources tab in the third pane from the left
Upvotes: 2
Reputation: 2569
I did a 'Maven reload' (or Gradle reload) from the upper-right 'Maven' ('Gradle') window and problem is solved.
Upvotes: 2
Reputation: 8393
MultiModule Project.
Select root pom.xml --> Click Maven -- Unlink <-- This will remove all sub modules
Select root pom.xml --> Add As Maven project
Then everythings worked..
Upvotes: 10
Reputation: 451
Upvotes: 35
Reputation: 17864
If you do an 'import from git', IntelliJ doesn't import the project structure from maven (or gradle) automatically.
One way to do this afterwards is to right-click on the pom.xml file in the root directory, and select 'Add as maven project'. Then the folders will be marked correctly, and dependent libraries will be imported. There should be no need to mark individual source folders manually.
I prefer to do a git clone outside of IntelliJ, and afterwards open the project in IntelliJ from the local filesystem, then IntelliJ imports the maven project structure automatically.
Upvotes: 157
Reputation: 1275
I faced the same problem in one of the modules in my multi module maven
project. In my InteliJ
idea, I've installed Maven Helper
plugin before. I did right click on the module and within that contextual menu click Run Maven and then Reimport. After doing that, my issue resolved.
Upvotes: 0
Reputation: 2810
IntelliJ may be ignoring files from a multi-module project. Modules that are ignored will make it harder for the IDE to find the root of that file. Hence, java file outside of source root
Another solution:
Preferences -> Build, Execution, Deployment -> Build Tools -> Maven -> Ignored Files
Now, uncheck the modules you do not want IntelliJ to ignore.
Upvotes: 1
Reputation: 69
Hate to add yet another method; but this one worked for me after trying and failing with the above methods. I had a multi-mode project. Unlinked all maven projects in Maven workspace and reloaded as maven project by clicking root poom.xml in project workspace. Not sure whether this will always work and for everyone.
Upvotes: 0
Reputation: 345
I had to open the module I worked on to a new window to solve that.
Upvotes: 0
Reputation: 31
In intellij: click Maven, select the project and right, select Unignore Projects
Upvotes: 3