lombok with maven


disclaimer

I don’t use lombok. And I won’t use it as long as I don’t have to.

references

sources

https://github.com/jinahya/lombok-with-maven-example

lombok

lombok을 provided scope 로 추가하자.

<dependency>
  <groupId>org.projectlombok</groupId>
  <artifactId>lombok</artifactId>
  <version>1.16.8</version>
  <scope>provided</scope>
</dependency>

클래스 파일 하나를 다음과 같이 추가하였다.

public class Person {

    @lombok.Getter
    @lombok.Setter
    private String name;

    @lombok.Getter
    @lombok.Setter
    private int age;
}

컴파일 후 클래스파일을 살펴보면 getter와 setter가 생성되어 있음을 확인할 수 있다.

$ mvn clean compile
...
$ javap target/classes/com/github/jinahya/example/Person.class
Compiled from “Person.java”
public class com.github.jinahya.example.Person {
  public com.github.jinahya.example.Person();
  public java.lang.String getName();
  public void setName(java.lang.String);
  public int getAge();
  public void setAge(int);
}
$

delombok

pom.xml에 다음과 같이 plugin을 추가하자.

<plugin>
  <groupId>org.projectlombok</groupId>
  <artifactId>lombok-maven-plugin</artifactId>
  <version>1.16.8.0</version>
  <executions>
    <execution>
      <phase>generate-sources</phase>
      <goals>
        <goal>delombok</goal>
      </goals>
    </execution>
  </executions>
</plugin>

src/main/lombok폴더에 다음과 같은 파일을 추가하였다.

public class Group {

    @lombok.Getter(lazy = true)
    private final List<Person> people = new ArrayList<>();
}

빌드 후 Group.class 파일을 de-compile 해 보면 다음과 같은 내용을 확인할 수 있다.

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

public class Group
{
  private final AtomicReference<Object> people = new AtomicReference();

  public List<Person> getPeople()
  {
    Object value = this.people.get();
    if (value == null) {
      synchronized (this.people)
      {
        value = this.people.get();
        if (value == null)
        {
          List<Person> actualValue = new ArrayList();
          value = actualValue == null ? this.people : actualValue;
          this.people.set(value);
        }
      }
    }
    return (List)(value == this.people ? null : value);
  }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s