gradle-lombok插件不能在单个项目中运行

时间:2016-09-21 06:39:22

标签: java gradle lombok

我正在努力使用简单的gradle构建脚本,通过Lombok Project在我的项目中添加gradle-lombok plugin功能(生成访问器方法)。使用gradle我无法通过命令行构建它,它无法编译。我创建this small project作为概念证明,表明它不起作用。请参阅我的脚本以及未编译的类。

之后我重新创建了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();
    }
}

2 个答案:

答案 0 :(得分:1)

您的项目无法使用Lombok编译的原因是您为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永远不会被执行。

答案 1 :(得分:1)

问题来自-proc:nonedocumentation表示“-proc:none表示编译没有注释处理”,lombok需要注释处理来生成所有其他方法。