0

我很难建立我的项目。我正在使用匕首 2,因此我需要使用注释处理器。我使用带有以下版本的 gradle wrapper 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
}

查看文档,他们在 4.6 中添加了开箱即用的注释处理器支持(无需使用任何插件)。我正在尝试使用相同的方法。现在,由于我没有设置 annotationProcessorPath,它应该自动使用构建路径,因为默认值是null - annotationProcessorPath 文档。但似乎情况并非如此。我的 Dagger 类正在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

因此,在运行 build 时./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 将需要使用那些已编译的文件,但由于某种原因不会“找到”这些文件。我将不胜感激任何有价值的意见。

4

1 回答 1

0

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

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

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));
    }
}
于 2018-03-20T17:41:34.560 回答