Lombok, Maven, and NetBeans

Here's a POM that incorporates Lombok:

<?xml version="1.0" encoding="UTF-8"?>
<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>com.mycompany</groupId>
    <artifactId>BookManager</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.14.4</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</project>

For adding the Lombok dependency above, you're able to add the @Data annotation from Lombok. The Navigator in NetBeans automatically shows all the getters and setters made available by Lombok, which you don't need to code and maintain yourself because they're not in your code. Click to enlarge the image below.

Now you can use all those getters and setters without ever having coded them, for example:

public class Main {

    public static void main(String[] args) {
        MyBooks mb = new MyBooks(1850, "Tale of Two Cities", "Dickens");
        String title = mb.getTitle();
        String author = mb.getAuthor();
        int year = mb.getYear();
        System.out.println("title = " + title);
        System.out.println("author = " + author);
        System.out.println("year = " + year);
    }

}

Use code completion in the version element of the POM to check what the latest Lombok JAR version is:

In the Services window, you can explore the Maven repo:

In "Local" above, after building the project which downloads the dependencies you've declared, you'll find the Lombok JAR declared in the POM:

In "Central Repository", you'll find all the Lombok JARs, which explains the code completion results, i.e., that's where they come from:


Simple, smooth, intuitive integration of Maven, and from Maven to Lombok, in NetBeans IDE.

Comments:

some experimental features aren't work in NetBeans, but anyway good thing

Posted by guest on September 03, 2014 at 02:29 PM PDT #

Which experimental features are you talking about? I don't believe NetBeans has any experimental features, so I'm curious to know what you're talking about.

Posted by Geertjan on September 04, 2014 at 10:49 AM PDT #

I have forgotten what exactly didn't work (compile error in ide editor) cause I have been testing Lombok a year ago, but it's likely was from page
http://projectlombok.org/features/experimental/index.html
some annotations worked well despite documentation awareness, but one or two was really unusable.

Posted by guest on September 04, 2014 at 02:55 PM PDT #

Well, completely impossible to help you with the description you've provided so far. Let me know when you have something that can be reproduced today, i.e., not something random you did last year.

Posted by Geertjan on September 06, 2014 at 06:57 AM PDT #

Thanks.

Posted by Mehul Boricha on September 28, 2014 at 09:48 AM PDT #

I also looked at Project Lombok but deemed it too risky to use (f.x. there are issues with the latest JDK). I also found the customisation features lacking, so I just released my own open source tool for generating java value objects (with getters, setters, constructors, equals, hash, compareTo and more) that used 100% standard java features, integrates with all java tools and is extremely customisable. You can check it out at "http://valjogen.41concepts.com". Let me know what you think?

Posted by Morten Christensen on October 22, 2014 at 12:11 PM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« March 2015
SunMonTueWedThuFriSat
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
    
       
Today