IntelliJ and Maven Source folder issue
Asked Answered
C

4

17

I'm trying to create a simple Java webapp using IntelliJ (v11.1.3) and integrate it with a simple maven pom file to download the Vaadin jar.

Here is my pom file:

<project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                  http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.a.maven.project</groupId>
<artifactId>MavenProject</artifactId>
<version>0.1</version>
<packaging>war</packaging>

<dependencies>
<dependency>
  <groupId>com.vaadin</groupId>
    <artifactId>vaadin</artifactId>
  <version>6.8.2</version>
</dependency>

</project>

I am checking this project out from version control in order to set it up in IntelliJ. However, IntelliJ does not seem to be able to recognize that the "src" folder is the sources folder. I can manually set this to the sources folder in the project settings, which is a minor annoyance, but not a big deal. However, EVERY time i make a change to the pom file, IntelliJ "forgets" that this is the sources folder, and then all my Java files get red circles around them. Does anyone have any idea as to what could be causing this? I've tried just about everything to fix this, including specifying the source folder in my pom file, but nothing seems to work.

Thanks in advance,

Eric

Coy answered 14/8, 2012 at 5:44 Comment(3)
What's the full path to the source directory from the project root? It should be /src/main/java according to the Maven conventions. IDEA will automatically configure this directory as Sources, see also wiki.jetbrains.net/intellij/Maven_FAQ.Softa
Thank you for that link! According to the wiki, the incorrect way of doing it is this: <build> <sourceDirectory>src</sourceDirectory> </build> --- however, i added that and now it works perfectly. I'm guessing that I created my repository in a way that IntelliJ does not like.Coy
@user1597121: Read more carefully. The wiki does not say that your code is incorrect, but the contrary.Melodize
S
19

IntelliJ IDEA follows Maven conventions and will automatically configure /src/main/java as Sources root.

In case you are using non-standard directories for the sources, check IDEA Maven FAQ for the workaround:

<sourceDirectory>...</sourceDirectory> will be added as Source folder
Softa answered 14/8, 2012 at 19:12 Comment(2)
Yes, for some reason when I created a project in IntelliJ it did not configure it this way by default. I have it all straightened out now. Thank you!Coy
bah you are a lifesaver - I inherited a project that was doing this and it was driving me batty...turns out there was a sourcedirectory tag in the pom >.<Woodprint
M
6

It happened to me on Idea 11 that everything was ok, but sources were not recognized. I opened that project as maven - from parrent pom - and selected delete existing idea files and generate new. It worked then.

Moline answered 21/6, 2013 at 8:19 Comment(1)
This happened on Idea 12 as well, 'mvn clean install' worked perfectly, but intellij thought the source dir started at 'src' and not 'src/main/java'. Just do 'File>Open' and pick your root pom.xml, then choose 'Delete Existing Project and Import' => fixedBeekeeper
O
3

If you are not able to make java dir -> 'src/main/java' as your source root:

  • create a new folder "java1" inside src/main
  • move all the folders and files inside java folder to java1
  • right click on java1 > mark directory as > source root
  • delete java folder
  • rename java1 -> java
Obstruent answered 21/2, 2016 at 14:8 Comment(1)
Sadly "'src/main/java' as your source root" this is probably what starts the monolith development cycle. see spring.io/guides/gs/multi-module for NOT starting with a monolith but upvote because it shows the issue.Beachlamar
E
3

In IntelliJ IDEA 2019.2.4, from the project selector window, either open the folder containing a pom.xml or the pom.xml file directly.

Assuming the pom.xml is properly structured, in case the source folders are still not there after the project is created, right click on the project and then go Maven > Generate Sources and Update folders to generate it, like so:

enter image description here

Extortion answered 16/11, 2019 at 19:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.