在 Gradle 版本 4.6 中应用 annotationProcessor

Applying annotationProcessor in Gradle version 4.6

我很难构建我的项目。我正在使用匕首 2,因此我需要使用注释处理器。我将 gradle 包装器版本 4.6 与以下构建一起使用:

plugins {
  id "java"
  id "idea"
}

group 'xyz.blackmonster'
version '0.0.1-SNAPSHOT'

sourceCompatibility = 1.8

ext {
  dropwizardVersion = "1.3.0"
  mysqlConnectorVersion = "8.0.9-rc"
  daggerVersion = "2.15"
  mockitoVersion = "2.16.0"
}

repositories {
  mavenCentral()
}

dependencies {
  compile group: 'io.dropwizard', name: 'dropwizard-core', version: dropwizardVersion
  compile group: 'io.dropwizard', name: 'dropwizard-jdbi3', version: dropwizardVersion
  compile group: 'io.dropwizard', name: 'dropwizard-auth', version: dropwizardVersion
  compile group: 'io.dropwizard', name: 'dropwizard-metrics-graphite', version: dropwizardVersion
  compile group: 'mysql', name: 'mysql-connector-java', version: mysqlConnectorVersion
  annotationProcessor group: 'com.google.dagger', name: 'dagger-compiler', version: daggerVersion
  implementation group: 'com.google.dagger', name: 'dagger', version: daggerVersion
  testCompile group: 'io.dropwizard', name: 'dropwizard-testing', version: dropwizardVersion
  testCompile group: 'org.mockito', name: 'mockito-core', version: mockitoVersion
}

正在查看 documentation, they have added annotation processor support out of the box in 4.6 (no need to use any plugins). I am trying to use the same approach. Now, since I do not set the annotationProcessorPath, it should automatically use the build path, since the value by default is null - annotationProcessorPath documentation。但似乎并非如此。我的匕首 类 正在 build/classes/java/main 中正确生成,但没有被拾取。为了构建 类,我在本地安装了 Java 8(控制台输出):

java version "1.8.0_162"
Java(TM) SE Runtime Environment (build 1.8.0_162-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.162-b12, mixed mode)

我 运行 gradle 使用以下命令:./gradlew clean build 为了证明我使用的是正确的版本,我 运行 命令:./gradlew -version 并得到了这个控制台输出:

------------------------------------------------------------
Gradle 4.6
------------------------------------------------------------

Build time:   2018-02-28 13:36:36 UTC
Revision:     8fa6ce7945b640e6168488e4417f9bb96e4ab46c

Groovy:       2.4.12
Ant:          Apache Ant(TM) version 1.9.9 compiled on February 2 2017
JVM:          1.8.0_162 (Oracle Corporation 25.162-b12)
OS:           Mac OS X 10.13.3 x86_64

因此,当 运行 构建 ./gradlew build 时,我得到以下输出:

> Task :compileJava FAILED
.../src/main/java/xyz/blackmonster/resume/app/ResumeApplication.java:17: error: cannot find symbol
import xyz.blackmonster.resume.config.DaggerBeanComponent;
                                     ^
  symbol:   class DaggerBeanComponent
  location: package xyz.blackmonster.resume.config
1 error

这是应用程序文件:

package xyz.blackmonster.resume.app;

import org.glassfish.jersey.server.filter.RolesAllowedDynamicFeature;
import org.jdbi.v3.core.Jdbi;

import com.google.common.cache.CacheBuilderSpec;
import io.dropwizard.Application;
import io.dropwizard.auth.AuthDynamicFeature;
import io.dropwizard.auth.AuthValueFactoryProvider;
import io.dropwizard.auth.CachingAuthenticator;
import io.dropwizard.auth.basic.BasicCredentialAuthFilter;
import io.dropwizard.auth.basic.BasicCredentials;
import io.dropwizard.jdbi3.JdbiFactory;
import io.dropwizard.setup.Environment;
import xyz.blackmonster.resume.config.bean.BeanComponent;
import xyz.blackmonster.resume.config.bean.BeanModule;
import xyz.blackmonster.resume.config.DaggerBeanComponent;
import xyz.blackmonster.resume.config.ResumeConfiguration;
import xyz.blackmonster.resume.security.auth.ResumeAuthorizer;
import xyz.blackmonster.resume.security.model.User;

public class ResumeApplication extends Application<ResumeConfiguration> {

    private static final String REALM = "resume";
    private static final String MYSQL = "mysql";

    private BeanComponent beanComponent;

    public static void main(String[] args) throws Exception {
        new ResumeApplication().run(args);
    }

    @Override
    public void run(ResumeConfiguration configuration, Environment environment) throws Exception {
        final JdbiFactory factory = new JdbiFactory();
        final Jdbi jdbi = factory.build(environment, configuration.getDataSourceFactory(), MYSQL);

        beanComponent = DaggerBeanComponent.builder().beanModule(new BeanModule(jdbi)).build();

        environment.jersey().register(beanComponent.getAchievementControler());

        CachingAuthenticator<BasicCredentials, User> cachingAuthenticator =
            new CachingAuthenticator<>(
                environment.metrics(), 
                beanComponent.getResumeAuthenticator(), 
                CacheBuilderSpec.parse(configuration.getAuthenticationCachePolicy()));

        environment.jersey().register(new AuthDynamicFeature(
            new BasicCredentialAuthFilter.Builder<User>()
                .setAuthenticator(cachingAuthenticator)
                .setAuthorizer(new ResumeAuthorizer())
                .setRealm(REALM)
                .buildAuthFilter()));
        environment.jersey().register(RolesAllowedDynamicFeature.class);
        environment.jersey().register(new AuthValueFactoryProvider.Binder<>(User.class));
    }
}

知道我做错了什么吗?我可以看到在 xyz.blackmonster.resume.config 中生成了匕首文件。 Gradle 需要使用那些已编译的文件,但由于某些原因 "find" 不需要使用那些文件。我将不胜感激任何宝贵的意见。

有人指出了我的问题。关于gradle,一切都很完美。问题与 java 相关。我将 Dagger-related classes 移到了另一个包中。因此,我将错误的(旧的)导入添加到应用程序 class。所以很自然地,当我在构建项目时,我输入错误,找不到class。

新的应用程序文件现在看起来像这样:

package xyz.blackmonster.resume.app;

import org.glassfish.jersey.server.filter.RolesAllowedDynamicFeature;
import org.jdbi.v3.core.Jdbi;

import com.google.common.cache.CacheBuilderSpec;
import io.dropwizard.Application;
import io.dropwizard.auth.AuthDynamicFeature;
import io.dropwizard.auth.AuthValueFactoryProvider;
import io.dropwizard.auth.CachingAuthenticator;
import io.dropwizard.auth.basic.BasicCredentialAuthFilter;
import io.dropwizard.auth.basic.BasicCredentials;
import io.dropwizard.jdbi3.JdbiFactory;
import io.dropwizard.setup.Environment;
import xyz.blackmonster.resume.config.bean.BeanComponent;
import xyz.blackmonster.resume.config.bean.BeanModule;
// wrong import was used here
// before I had import xyz.blackmonster.resume.config.DaggerBeanComponent
import xyz.blackmonster.resume.config.bean.DaggerBeanComponent;
import xyz.blackmonster.resume.config.ResumeConfiguration;
import xyz.blackmonster.resume.security.auth.ResumeAuthorizer;
import xyz.blackmonster.resume.security.model.User;

public class ResumeApplication extends Application<ResumeConfiguration> {

    private static final String REALM = "resume";
    private static final String MYSQL = "mysql";

    private BeanComponent beanComponent;

    public static void main(String[] args) throws Exception {
        new ResumeApplication().run(args);
    }

    @Override
    public void run(ResumeConfiguration configuration, Environment environment) throws Exception {
        final JdbiFactory factory = new JdbiFactory();
        final Jdbi jdbi = factory.build(environment, configuration.getDataSourceFactory(), MYSQL);

        beanComponent = DaggerBeanComponent.builder().beanModule(new BeanModule(jdbi)).build();

        environment.jersey().register(beanComponent.getAchievementControler());

        CachingAuthenticator<BasicCredentials, User> cachingAuthenticator =
            new CachingAuthenticator<>(
                environment.metrics(), 
                beanComponent.getResumeAuthenticator(), 
                CacheBuilderSpec.parse(configuration.getAuthenticationCachePolicy()));

        environment.jersey().register(new AuthDynamicFeature(
            new BasicCredentialAuthFilter.Builder<User>()
                .setAuthenticator(cachingAuthenticator)
                .setAuthorizer(new ResumeAuthorizer())
                .setRealm(REALM)
                .buildAuthFilter()));
        environment.jersey().register(RolesAllowedDynamicFeature.class);
        environment.jersey().register(new AuthValueFactoryProvider.Binder<>(User.class));
    }
}