niltz
niltz

Reputation: 1128

How to keep test generated sources out of the generated sources target folder

I am using MapStruct to generate some Entity to DTO mappers. I have an abstract mapper defined, and I created some tests for it using a test entity, test dto, and test mapper. These test files live in the src/test/java folder, but when MapStruct generates the implementation for the test mapper it puts the generated source in the target/generated-sources folder instead of the target/generated-test-sources folder. This causes the class to get compiled into the actual jar file which I don't want.

[UPDATE] I have put up an example that recreates the problem here:

https://github.com/niltz/so-51090868-example

Seems like it works fine when I run the build with maven on the command line, but when I import the pom into Spring Tool Suite, I get the issue.

Upvotes: 0

Views: 2161

Answers (2)

niltz
niltz

Reputation: 1128

Seems like it is an issue with the m2e eclipse plugin while using JDT APT, the maven-compiler-plugin, and an older version of eclipse.

https://marketplace.eclipse.org/content/m2e-apt

Apparently I need to ensure that I use Eclipse Photon or higher, or I can use the maven-processor-plugin instead. I chose to upgrade Eclipse and it seems to work now.

https://bsorrentino.github.io/maven-annotation-plugin/

Upvotes: 1

sansari
sansari

Reputation: 558

I made a quick test with following class in src/test/java/ having a class

 @Mapper
 public abstract class AbstactClass {

 }

I have following dependencies and plugins in my maven pom:

      <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.5.1</version>
          <configuration>
            <source>1.8</source>
            <target>1.8</target>
            <annotationProcessorPaths>
              <path>
                <groupId>org.mapstruct</groupId>
                <artifactId>mapstruct-processor</artifactId>
                <version>1.1.0.Final</version>
              </path>
            </annotationProcessorPaths>
          </configuration>
        </plugin>

    <dependency>
      <groupId>org.mapstruct</groupId>
      <artifactId>mapstruct-jdk8</artifactId>
      <version>1.1.0.Final</version>
    </dependency>

and ran mvn clean install. Generated class is in target/generated-test-sources. If you give more information about your class and settings I could help more.

Upvotes: 1

Related Questions