Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lombok/Mapstruct problem: Cannot find symbol #1270

Closed
Hervian opened this issue Aug 15, 2017 · 10 comments
Closed

Lombok/Mapstruct problem: Cannot find symbol #1270

Hervian opened this issue Aug 15, 2017 · 10 comments

Comments

@Hervian
Copy link

Hervian commented Aug 15, 2017

Hi, I love MapStruct and Lombok, but I get a "cannot find symbol" error when compiling with Maven from command-line using Javac.

In some setups, it is the @Mapper annotation that cannot be found, in other setups it is the calls to the Lombok-generated getters/setters etc.

I have created a minimal test-project that demonstrates the error. Code, pom file, stacktrace, Java-version, Maven-version, dependencies etc are all described in the zip-folder files.

I am using maven-compiler-plugin version 3.6.1, mapstruct-jdk8 version 1.2.0.CR1 and lombok version 1.16.16. And the project is a spring-boot application.

Note that this issue resembles #841, which recommends to exlude the transitive mapstruct dependency pulled in from the dependency to springfox-swagger2. This, however, did not solve the problem.

This is the output from mvn clean verify:

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building lombok-mapstruct-spring 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.6.1:clean (default-clean) @ lombok-mapstruct-spring ---
[INFO] Deleting C:\Users\agh\Desktop\tmp\lombok-mapstruct-spring\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ lombok-mapstruct-spring ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.6.1:compile (default-compile) @ lombok-mapstruct-spring ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 3 source files to C:\Users\agh\Desktop\tmp\lombok-mapstruct-spring\target\classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /C:/Users/agh/Desktop/tmp/lombok-mapstruct-spring/src/main/java/MyLombokAnnotatedClass.java:[11,9] cannot find symbol
  symbol:   variable log
  location: class MyLombokAnnotatedClass
[INFO] 1 error
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.791 s
[INFO] Finished at: 2017-08-15T17:50:38+02:00
[INFO] Final Memory: 44M/618M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.6.1:compile (default-compile) on project lombok-mapstruct-spring: Compilation failure
[ERROR] /C:/Users/agh/Desktop/tmp/lombok-mapstruct-spring/src/main/java/MyLombokAnnotatedClass.java:[11,9] cannot find symbol
[ERROR] symbol:   variable log
[ERROR] location: class MyLombokAnnotatedClass
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

Lombok-Mapstruct-SpringBoot-Problem.zip

@Hervian
Copy link
Author

Hervian commented Aug 15, 2017

By downgrading the maven-compiler-plugin from 3.6.1 to 3.1 I got it to work.
I trimmed the attached pom further to pindown the problem, and this is the new pom (with the downgraded plugin version that makes it compile):

<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>org.test</groupId>
  <artifactId>lombok-mapstruct-spring</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.4.RELEASE</version>
    <relativePath/>
  </parent>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <m2e.apt.activation>jdt_apt</m2e.apt.activation>
    <maven.compiler.source>${java.version}</maven.compiler.source>
    <maven.compiler.target>${java.version}</maven.compiler.target>
    <java.version>1.8</java.version>
    <maven.compiler.plugin.version>3.1</maven.compiler.plugin.version>
    <org.mapstruct.version>1.2.0.CR1</org.mapstruct.version>
  </properties>


  <dependencies>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>

 
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.mapstruct</groupId>
      <artifactId>mapstruct-jdk8</artifactId>
      <version>${org.mapstruct.version}</version>
    </dependency>
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
    </dependency> 
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>${maven.compiler.plugin.version}</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
          <annotationProcessorPaths>
            <path>
              <groupId>org.mapstruct</groupId>
              <artifactId>mapstruct-processor</artifactId>
              <version>${org.mapstruct.version}</version>
            </path>
          </annotationProcessorPaths>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>

</project>

I will leave the issue open in case this is a bug - it is possible that I have missed this requirement in some documentation.

@filiphr
Copy link
Member

filiphr commented Aug 15, 2017

Thanks for the nice words. We are glad that you like MapStruct 😄 .

The problem is that you have only mapstruct-processor in the annotationProcessorPaths and only that dependency is passed to the javac compiler.

The annotationProcessorPaths is there only since 3.5 so it is really strange how the mapstruct processor is invoked. In any case in order to make it work correctly you have 2 options:

  1. Add the lombok dependency to the annotationProcessorPaths as well. Then it will be picked up by the compiler and it should work.

  2. Ad the mapstruct-processor jar to your pom as a provided dependency. You will have to remove the annotationProcessorPaths in that case and the compiler will pick up the processors from your dependency. The reason why we are suggesting to use the annotationProcessorPaths is because by doing that you won't be able to use something from the mapstruct-processor module by accident. An additional reason to put it in the pom dependencies is this issue from IntelliJ (in case you are using InteliiJ 😄

You can also have a look at the mapstruct-lombok example from our repository.

I am going to close this issue as it is a problem in the configuration 😄

@lucashos
Copy link

lucashos commented Sep 22, 2017

I had the same problem here.

The mapping bean wasnt being implemented with the corresponding attributes, after a long time, I've discovered that the processorsPath order was interfering in the lombok getters/setters implementation and the mappings made by mapstruct.

The way I've solved was reordering the processors, here is a example of my pom:

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                            <version>${lombok.version}</version>
                        </path>
                        <path>
                            <groupId>org.mapstruct</groupId>
                            <artifactId>mapstruct-processor</artifactId>
                            <version>${mapstruct.version}</version>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>

@filiphr
Copy link
Member

filiphr commented Sep 23, 2017

@lucashos just curious to know which versions of Lombok and MapStruct are you using? Because if you are using the ones that should be compatible, the order should not matter.

@lucashos
Copy link

lucashos commented Oct 2, 2017

@filiphr Intresting... The solution worked for a whole day, but at the other day it just doesnt worked anymore.
Now I've upgraded the version to the latest version, 1.2.0.CR2 and its working perfectly.

@filiphr
Copy link
Member

filiphr commented Oct 3, 2017

@lucashos this is really strange. I really have no idea why it is doing this

@deycall911
Copy link

deycall911 commented Dec 29, 2017

I got "cannot find symbol" error when there is a syntax error in my "expression" code, maybe this will be useful.
@Mapping(target = "ABC", expression = "java(c.getName != null? c.getName() : c.getSurname())")
So maybe this is not configuration problem. (Was using mvn clean install)

@iorran
Copy link

iorran commented Sep 2, 2018

First of all, I'm glad to see that you tried to use that.

So, I had this problem too and my solution was that:

<!-- Mapstruct -->
		<dependency>
			<groupId>org.mapstruct</groupId>
			<artifactId>mapstruct-jdk8</artifactId> <!-- use mapstruct-jdk8 for Java 8 or higher -->
			<version>1.2.0.Final</version>
		</dependency>
<build>
		<finalName>sgec-core</finalName>
		<pluginManagement>
			<plugins>
				<plugin>
					<groupId>org.apache.maven.plugins</groupId>
					<artifactId>maven-compiler-plugin</artifactId>
					<version>3.6.2</version>
					<configuration>
						<source>${java.version}</source>
						<target>${java.version}</target>
						<annotationProcessorPaths>
							<!-- For Mapstruct generation -->
							<path>
								<groupId>org.mapstruct</groupId>
								<artifactId>mapstruct-processor</artifactId>
								<version>1.2.0.Final</version>
							</path>
							<!-- For JPA static metamodel generation 
							<path>
								<groupId>org.hibernate</groupId>
								<artifactId>hibernate-jpamodelgen</artifactId>
								<version>5.0.12.Final</version>
							</path>
-->
							<!-- For Lombok generation -->
							<path>
								<groupId>org.projectlombok</groupId>
								<artifactId>lombok</artifactId>
								<version>1.16.18</version>
							</path>
						</annotationProcessorPaths>
					</configuration>
				</plugin>
			</plugins>
		</pluginManagement>
	</build>

I recommend you build the project without IDE using mvn install on prompt. If the problem persist, just try to re-install lombok jar into your IDE (if you are using eclipse), and restart...

@andresmerida
Copy link

@lucashos Thanks man, I solved with your suggestion.

@nekperu15739
Copy link

nekperu15739 commented Apr 4, 2019

Hi guys i have the same problem, I try to use all solutions in comments without result, the main difference with other post is that im using java 11, this is my config:
<java.version>11</java.version>
<maven-compiler-plugin.version>3.8.0</maven-compiler-plugin.version>
<org.mapstruct.version>1.3.0.Final</org.mapstruct.version>
<immutables.version>2.7.5</immutables.version>

<plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-compiler-plugin</artifactId>
              <version>3.8.0</version>
              <configuration>
                  <source>${java.version}</source>
                  <target>${java.version}</target>
                  <annotationProcessorPaths>
                      <path>
                          <groupId>org.mapstruct</groupId>
                          <artifactId>mapstruct-processor</artifactId>
                          <version>${org.mapstruct.version}</version>
                      </path>
                      <path>
                          <groupId>org.projectlombok</groupId>
                          <artifactId>lombok</artifactId>
                          <version>${lombok.version}</version>
                      </path>
                      <path>
                          <groupId>org.immutables</groupId>
                          <artifactId>value</artifactId>
                          <version>${immutables.version}</version>
                      </path>
                  </annotationProcessorPaths>
              </configuration>
          </plugin>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants