Reputation: 2368
I am struggling with a simple gradle build script to add the Lombok Project features on my project (generate accessors methods) through gradle-lombok plugin. Using gradle I can't build it via command-line, it's failing to compile. I created this small project as a proof of concept to show that it does not working. See bellow my script and the classes not compiling.
After that I recreated the project with sub-projects structure and the most weird thing is just doing this separation made it work, but I am still looking for why the single project does not work.
If someone can give a hint will be very welcomed.
buildscript {
repositories {
jcenter()
mavenLocal()
maven { url "https://repo.spring.io/plugins-release" }
maven { url "https://plugins.gradle.org/m2" }
}
dependencies {
classpath "io.franzbecker:gradle-lombok:1.7"
}
}
plugins {
id "io.franzbecker.gradle-lombok" version "1.7"
}
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: "io.franzbecker.gradle-lombok"
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
group = 'lombok-demo'
version = '1.0-SNAPSHOT'
buildDir = 'target'
// global configurations
configurations {
compile {
exclude(module: 'commons-logging')
exclude(module: 'c3p0')
exclude(module: 'log4j')
exclude(module: 'log4j2')
exclude(module: 'opensaml')
}
}
repositories {
mavenLocal()
mavenCentral()
maven { url "http://jaspersoft.artifactoryonline.com/jaspersoft/third-party-ce-artifacts/" }
maven { url "http://jasperreports.sourceforge.net/maven2/"}
}
dependencies {
compile "com.google.guava:guava:${version_guava}"
compile "org.projectlombok:lombok:1.16.10"
compile "org.slf4j:slf4j-api:${version_slf4j}"
compile group: 'org.hibernate.javax.persistence', name: 'hibernate-jpa-2.1-api', version: '1.0.0.Final'
compile "javax.validation:validation-api:${version_javax_validation}"
compile ("org.hibernate:hibernate-validator:${version_hibernate_validator}") {
exclude(module: "classmate")
}
compile group: 'net.sf.jasperreports', name: 'jasperreports', version: '6.3.0'
compile "org.hsqldb:hsqldb:${version_hsqldb}"
//provided "org.hsqldb:hsqldb:${version_hsqldb}"
}
tasks.withType( JavaCompile.class ).all { task ->
task.options.encoding = "UTF-8"
task.options.compilerArgs += ["-nowarn", "-proc:none", "-encoding", "UTF-8", "-source", sourceCompatibility, "-target", targetCompatibility ]
}
@Data
@Entity
public class Invoice {
private Long id;
private String invoiceNumber;
private LocalDateTime issueDate;
private Customer customer;
private LocalDateTime startDate;
private LocalDateTime endDate;
private Address billingAddress;
private List<InvoiceItem> items;
public Invoice() {
this.issueDate = LocalDateTime.now();
this.billingAddress = customer.getMailingAddress();
this.items = new ArrayList<>();
}
public Invoice(Customer customer) {
this();
this.customer = customer;
}
}
import lombok.Data;
import lombokdemo.domain.Address;
import lombokdemo.domain.Customer;
import lombokdemo.domain.Invoice;
import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
@Data
public class Header {
private final DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
private String companyName;
private Address companyAddress;
private URL companyLogo;
private Invoice invoice;
private Customer customer;
private LocalDateTime issueDate;
private LocalDateTime startDate;
private LocalDateTime endDate;
private Address billingAddress;
public Header(Invoice invoice) {
this.invoice = invoice;
this.issueDate = LocalDateTime.now();
this.companyName = "";
this.companyAddress = new Address();
}
public String getInvoiceNumber() {
return invoice.getInvoiceNumber();
}
public String getIssueDateText() { return dateFormat.format(issueDate); }
public String getPeriod() {
return dateFormat.format(startDate) + " to " + dateFormat.format(endDate);
}
public String getCustomerName() {
return invoice.getCustomer().getName();
}
private String getAddressText(Address address, boolean includingWeb) {
StringBuilder sb = new StringBuilder();
sb.append(address.getStreet1());
if (!address.getStreet2().isEmpty()) {
sb.append("\n").append(address.getStreet2());
}
if (sb.length() > 0) {
sb.append("\n");
}
if (address.getCity() != null && !address.getCity().isEmpty()) {
sb.append(address.getCity());
if (!address.getStateOrProvince().isEmpty()) {
sb.append(", ").append(address.getStateOrProvince());
}
if (!address.getPostalCode().isEmpty()) {
sb.append(" ").append(address.getPostalCode());
}
sb.append("\n");
}
if (address.getCountry() != null && !address.getCountry().isEmpty()) {
sb.append(address.getCountry());
sb.append("\n");
}
if (includingWeb) {
if (!address.getWebsite().isEmpty()) {
sb.append(address.getWebsite());
sb.append("\n");
}
if (!address.getEmail().isEmpty()) {
sb.append(address.getEmail());
sb.append("\n");
}
}
return sb.toString();
}
public String getCompanyAddress() {
final StringBuilder sb = new StringBuilder();
if (!companyAddress.getContactName().isEmpty()) {
sb.append(companyAddress.getContactName());
}
sb.append(companyAddress.getStreet1());
if (!companyAddress.getStreet2().isEmpty()) {
sb.append("\n").append(companyAddress.getStreet2());
}
sb.append("\n");
sb.append(getAddressText(companyAddress, true));
return sb.toString();
}
public String getBillingAddress() {
final Address address = invoice.getCustomer().getMailingAddress();
final StringBuilder sb = new StringBuilder();
if (!address.getContactName().isEmpty()) {
sb.append(address.getContactName());
}
sb.append(getAddressText(address, false));
return sb.toString();
}
}
Upvotes: 1
Views: 4758
Reputation: 257
The issue comes from -proc:none
. The documentation says "-proc:none means that compilation takes place without annotation processing" and lombok needs an annotation processing to generate all additional methods.
Upvotes: 2
Reputation: 705
The reason your project does not compile with Lombok is the customization you introduced for the JavaCompile
tasks:
tasks.withType( JavaCompile.class ).all { task ->
task.options.encoding = "UTF-8"
task.options.compilerArgs += ["-nowarn", "-proc:none", "-encoding", "UTF-8", "-source", sourceCompatibility, "-target", targetCompatibility ]
}
If you comment this out it works just fine. It seems that the compilerArgs
are causing the problem.
The reason it worked in your multi-project setup is that the customization was not applied. The tasks 'compileJava' and 'compileTestJava' are applied to the root project only, not the subprojects. Try the following:
subprojects {
tasks.withType(JavaCompile.class).all { task ->
println("Hello from $task")
}
}
You will see that the println is never executed.
Upvotes: 1