Tuesday Sep 02, 2014

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.

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
« September 2014 »
SunMonTueWedThuFriSat
 
7
12
13
14
26
27
28
29
30
    
       
Today