generated test-sources folder recognized as sources folder

Answered

Hi there,

I am using IntelliJ 2019.1 Ultimate and trying to achieve the following:

I have a jar containing a swagger specification and I want to build a REST client for my project using the swagger maven plugin. This works already and I configured it that it will create the following directory structure:

/target/generated-sources/swagger/src/main/java and /target/generated-sources/swagger/src/test/java  so it conforms to the standard maven layout.

Moreover, I configured maven to pick up the test sources directory as test-source directory:

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>add-test-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>${project.build.directory}/generated-sources/swagger/src/test/java</source>
</sources>
</configuration>
</execution>

When I run maven on the command line, everything looks fine:

[INFO] --- swagger-codegen-maven-plugin:2.4.4:generate (default) @ myproject ---
[...][INFO] writing file T:\Repositories\myproject\target\generated-sources\swagger\src\test\java\de\xyz\myotherproject\rest\api\DefaultApiTest.java
[...][INFO] --- build-helper-maven-plugin:3.0.0:add-test-source (add-test-source) @ myproject ---
[INFO] Test Source directory: T:\Repositories\myproject\target\generated-sources\swagger\src\test\java added.
[...]

When I use that project in IntelliJ, the IDE picks up that path as "generated sources root" (and not test-sources) so it flags the class red, since the JUnit dependency has scope test and none of the annotations work. When I manually flag that directory as test-sources root it turns green and the errors vanish. But I don't want to do this every time and tell all my fellow-developers to do that. Instead I want IntelliJ to respect that maven configuration.

There seems to be some magic involved here as the /target/generated-sources/swagger/src/main/java is being recognized as source folder no matter whether I ask maven to add it as source folder or not.

Does anyone have an idea on this one?

Thanks!

Harald.

0
7 comments

Works fine for me:

Might be related to some other Maven plugins you use in project for source code generation, like in case described here: https://youtrack.jetbrains.com/issue/IDEA-159954 Can you provide a sample project where issue is reproducible?

0

Good description of the issue. I have the exact same problem.

0

@Marten Richert please provide a sample project in YouTrack reported issue at https://youtrack.jetbrains.com/issues/IDEA

0

@Andrey Dernov I have sample project at hand right now. Which youtrack-issue do you mean? I am not the original reporter in this thread.

0

I faced the same situation.


Nevertheless, IntelliJ :

  • requires a plugin/executions/execution/id value
  • can use phase[none]
  • bind goal add-test-source => add a new test source
  • bind goal add-source => add a new source directory

IntelliJ limitations:

  • does not bind goal add-test-resource to test resource directory
  • does not bind goal add-resource to resource directory

 

 

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>add-test-sources</id>
<phase>none</phase>
<goals>
<goal>add-test-source</goal>
<goal>add-test-resource</goal>
</goals>
<configuration>
<sources>
<source>src/it/java</source>
</sources>
<resources>
<resource>src/it/resources</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>

 

0

Please sign in to leave a comment.