首页 > 代码库 > 使用lombok 注解Java类

使用lombok 注解Java类

环境信息:

IDEA  2016.2.4

Maven 3

JDK 1.8

Maven工程配置:

<properties>   
    <lombok.version>1.16.16</lombok.version>
    <lombok.plugin.version>1.16.16.0</lombok.plugin.version>
</properties>
    <dependencies>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.8.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.8.2</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
 <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.projectlombok</groupId>
                    <artifactId>lombok-maven-plugin</artifactId>
                    <version>${lombok.plugin.version}</version>
                    <executions>
                        <execution>
                            <phase>generate-sources</phase>
                            <goals>
                                <goal>delombok</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                        <encoding>UTF-8</encoding>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

 

安装lombok插件: 

1. 下载 https://plugins.jetbrains.com/plugin/6317-lombok-plugin

2.  Setting -> plugins ->install plugin from disk 

3.  Setting->Build,Exe... -> Compiler->Annotation Processors -> 勾选enable annotation processing

遇到的问题:

1.  The parameters ‘encoding‘ for goal org.projectlombok:lombok-maven-plugin:1.16.16.0:delombok are missing or invalid

根据 , 缺少encoding参数 在pom中添加如下属性:

        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

2. 添加@Data注解后,根据注解生成的方法没有在outline中显示, 以及无法在代码编辑是resolve注解生成的方法

解决: 添加lombok-plugin

 

参考: https://stackoverflow.com/questions/41161076/adding-lombok-plugin-to-intellij-project 

使用lombok 注解Java类