2016-09-21 27 views
1

我正在使用简单的gradle构建脚本来通过gradle-lombok plugin在我的项目(生成访问器方法)中添加Lombok Project功能。使用gradle我不能通过命令行构建它,它无法编译。我创建了this small project作为概念验证证明它不起作用。请参阅下面的脚本和不编译的类。gradle-lombok插件无法在单个项目中工作

之后,我重新创建了project with sub-projects structure和最奇怪的事情就是做这个分离使它的工作,但我仍然在寻找为什么单个项目不起作用。

如果有人可以给予提示将会非常受欢迎。

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(); 
    } 
} 
+0

“无法编译” - 您是否收到任何错误消息? – mkabanen

+0

错误在于没有getter方法。 lombok插件本来是要测试字节码并自动生成的。 –

+0

不知道,发生了什么事。我在所有项目中都使用lombok,我需要的只是'依赖关系{providedCompile'org.projectlombok:lombok:1.16.8“}'。 +++注意:Lombok不适用于字节码。它在字节码生成发生之前修改AST。 – maaartinus

回答

1

你的项目没有与龙目岛编译的原因是你的JavaCompile任务推出了定制:

tasks.withType(JavaCompile.class).all { task -> 
    task.options.encoding = "UTF-8" 
    task.options.compilerArgs += ["-nowarn", "-proc:none", "-encoding", "UTF-8", "-source", sourceCompatibility, "-target", targetCompatibility ] 
} 

如果您注释掉它只是正常工作。看来compilerArgs正在造成这个问题。

它在您的多项目设置中起作用的原因是没有应用自定义。任务'compileJava'和'compileTestJava'仅适用于根项目,而不适用于子项目。请尝试以下操作:

subprojects { 
    tasks.withType(JavaCompile.class).all { task -> 
     println("Hello from $task") 
    } 
} 

您将看到println从不执行。

相关问题