6. 在 Eclipse 中使用 Gradle 的注释处理 (apt)

本文介绍如何在Eclipse中使用Gradle插件进行注释处理(apt),包括安装配置过程及使用Auto-value自动生成构建器的具体步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

6. 在 Eclipse 中使用 Gradle 的注释处理 (apt)

6.1. 概览

gradle 插件支持 gradle 的注释处理。它还支持在 Eclipse 和 IntelliJ 中使用此方法。

6.2. Eclipse IDE 中的安装程序

下面的示例描述使用 com.google.auto.value 为数据模型自动生成构建器的 it 使用情况。

调整你的 build.gradle 启用 gradle 插件。

buildscript {
  repositories {
    maven {
      url "https://plugins.gradle.org/m2/"
    }
  }
  dependencies {
    classpath "net.ltgt.gradle:gradle-apt-plugin:0.13"
  }
}

apply plugin: "net.ltgt.apt-eclipse"
apply plugin: 'java-library'

repositories {
    // Use jcenter for resolving your dependencies.
    // You can declare any Maven/Ivy/file repository here.
    jcenter()
}

dependencies {
    // Use JUnit test framework
    testImplementation 'junit:junit:4.12'
}
6.3. 示例: 使用自动值(Auto-value)与 apt 处理(apt-processing)

下面演示了在 Java 项目中 Auto value apt 处理器的用法。

dependencies {
  // more stuff

  // auto-factory contains both annotations and their processor, neither is needed at runtime
  compileOnly "com.google.auto.value:auto-value:1.5"
  apt         "com.google.auto.value:auto-value:1.5"
}

运行 eclipse 任务之后。在 Eclipse IDE 中配置 apt 处理。

import com.google.auto.value.AutoValue;

@AutoValue
abstract class Task {
    static Task create(String summary, String description, int duration) {
        // See "How do I...?" below for nested classes.
        return new AutoValue_Task(summary, description, duration); // generated by the apt processor
      }

      abstract String summary();
      abstract String description();
      abstract int duration();

}

使用:

import org.junit.Test;
import static org.junit.Assert.*;

public class TaskTest {
    @Test public void testSomeLibraryMethod() {
        Task create = Task.create("Learn apt", "Now", 10);
        System.out.println(create);

    }
}

原文地址:http://www.vogella.com/tutorials/EclipseGradle/article.html#annotation-processing-apt-with-gradle-in-eclipse

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值