Search code examples
mapstructmaven-compiler-pluginimmutables-library

How to compile project with only `mvn clean install` without having to subsequently run `mvn install`?


I am trying to compile project which uses both Mapstruct and Immutables. The only solution which solves my problem is to run:

  1. mvn clean compile -> fails with compilation failure; cannot find generated classes from Immutables
  2. mvn compile -> succeeds

Which is not acceptable for me.

I've tried recommended solution which you can see in the code section. I've also looked at:

...

<mapstruct.version>1.3.0.Beta2</mapstruct.version>
<immutables.version>2.7.3</immutables.version>

...

<dependency>
    <groupId>org.mapstruct</groupId>
    <artifactId>mapstruct-jdk8</artifactId>
    <version>${mapstruct.version}</version>
</dependency>
<dependency>
    <groupId>org.mapstruct</groupId>
    <artifactId>mapstruct-processor</artifactId>
    <version>${mapstruct.version}</version>
</dependency>

...

<build>
    <plugins>
        <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>${mapstruct.version}</version>
                    </path>
                    <path>
                        <groupId>org.immutables</groupId>
                        <artifactId>value</artifactId>
                        <version>${immutables.version}</version>
                    </path>
                </annotationProcessorPaths>
            </configuration>
        </plugin>
    </plugins>
</build>

I would like to be able to only run mvn clean compile in order to get project compiled.


Solution

  • After several hours of building a minimal example of the issue I noticed this line which happened to be the cause of the failing build:

    @Mapper(imports = {ImmutableFirstType.class, ImmutableSecondType.class})     // this one
    public interface FirstSecondTypeMapper {
    

    I thought imports are necessary in order to make Immutables with mapstruct work. Just used @Mapper and everything went fine.