Maven FAQ

From IntelliJ-Wiki

Jump to: navigation, search

Sources and Resources

In general, IDEA automatically imports sources and resources as Sources folders (or Test Sources for test sources and resources). E.g. in case:

<build>
 <sourceDirectory>...</sourceDirectory> will be added as Source folder
 <testSourceDirectory>...</testSourceDirectory> will be added as Test Source folder
 
 <resources>
  <resource>
   <directory>...</directory> will be added as Source folder
  </resource>
 </resources>
 
 <testResources>
  <testResource>
   <directory>...</directory> will be added as Test Source folder
  </testResource>
 </testResources>
</build>

However, IDEA does not support nested directories; it is also not allowed to use same directory as production sources and test sources or as sources and resources. If you need some common code, consider extracting it into a separate folder.

The following configurations are incorrect:

<build>
 <sourceDirectory>src</sourceDirectory> 
 <testSourceDirectory>src/tests</testSourceDirectory> invalid: cannot be nested into another source dir
</build>
<build>
 <sourceDirectory>src</sourceDirectory>
 <resources>
  <resource>
   <directory>src/res</directory> invalid: cannot be nested into the source dir
  </resource>
 </resources>
</build>
<build>
 <sourceDirectory>src</sourceDirectory> 
 <testSourceDirectory>src</testSourceDirectory> invalid: cannot use same directory as sources and test sources
</build>
<build>
 <sourceDirectory>src</sourceDirectory>
 <resources>
  <resource>
   <directory>src</directory> invalid: cannot use same directory as sources and resources
  </resource>
 </resources>
</build>

Generated Sources

In order to get generated sources automatically imported as source folders configure corresponding plugins so that they put them into target/generated-sources/<subdir>, where subdir is any folder name you prefer. The subdir folder is necessary to distinguish sources from different tools and also to exclude some special generated sources (e.g. groovy stubs).

Please note that even if you manually configure some source folders under target/generated-sources of this folder itself, IDEA will rewrite them according to your pom.xml.

Any time you want to generate sources you simply execute the corresponding goal, bound for generation (usually generate-sources, generate-test-sources). After that IDEA will pick up new folders and set them up.

Generated test sources/resources should be placed in target/generated-test-sources/<subdir>.

Generated Sources in Non-standard location

If it is not possible to configure the plugin you should use Update Folders action available from Maven Projects tool window and for each Maven project in the list. This action takes special steps to discover plugin configuration and configure source folders.

To specify a phase that should be used during this action go to Settings->Maven->Import.

Personal tools