Dagger 2没有生成类

时间:2015-01-13 13:02:36

标签: java maven dagger dagger-2

我想尝试新的东西并使用Dagger 2作为我的DI框架。所以我有以下pom和"你好世界"咖啡机类(http://google.github.io/dagger/)在我的项目中。

但是当我进行mvn clean安装时,不会生成任何类。据我所知,应该有一个" Dagger_CoffeeShop"类生成。嗯......我错过了什么?

<modelVersion>4.0.0</modelVersion>

<groupId>kic</groupId>
<artifactId>xfoo</artifactId>
<version>0.1-SNAPSHOT</version>

<packaging>jar</packaging>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
    <dependency>
        <groupId>com.google.dagger</groupId>
        <artifactId>dagger</artifactId>
        <version>2.0-SNAPSHOT</version>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
    </dependency>
</dependencies>


<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <dependencies>
                <dependency>
                    <groupId>com.google.dagger</groupId>
                    <artifactId>dagger-compiler</artifactId>
                    <version>2.0-SNAPSHOT</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

3 个答案:

答案 0 :(得分:9)

对于那些使用Gradle的人:确保引用Dagger依赖关系如下:

compile 'com.google.dagger:dagger:2.0.2'
apt 'com.google.dagger:dagger-compiler:2.0.2'

如果您遇到apt不支持,请添加

1)进入模块app\build.gradle

apply plugin: 'com.neenbedankt.android-apt'

2)进入项目\build.gradle

buildscript {
    dependencies {
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
    }
}

答案 1 :(得分:5)

我遇到了同样的问题,只有发布版本2.0。

在我的情况下,以下两个步骤解决了这个问题:

  • target/generated-sources/annotations添加到我的构建路径
  • <forceJavacCompilerUse>true</forceJavacCompilerUse>添加到maven编译器插件

    <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <dependencies>
            <dependency>
                <groupId>com.google.dagger</groupId>
                <artifactId>dagger-compiler</artifactId>
                <version>2.0</version>
            </dependency>
        </dependencies>
        <configuration>
            <source>1.8</source>
            <target>1.8</target>
            <!-- workaround for http://jira.codehaus.org/browse/MCOMPILER-202 -->
            <forceJavacCompilerUse>true</forceJavacCompilerUse>
        </configuration>
    </plugin>
    

另见:

答案 2 :(得分:1)

这不是最好的答案。当我使用下载的JAR文件(在下面的POM文件中注释掉)时,我也没有生成Dagger_文件。一旦我将存储库添加到POM,一切正常。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.hello</groupId>
  <artifactId>hellodagger</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>hellodagger</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <repositories>
    <repository>
      <id>sonatype</id>
      <name>sonatype-repo</name>
      <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
    </repository>
  </repositories>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
      <dependency>
        <groupId>com.google.dagger</groupId>
        <artifactId>dagger</artifactId>
        <version>2.0-SNAPSHOT</version>
        <!--<scope>system</scope>-->
        <!--<systemPath>${project.basedir}/dagger-2.0-20141216.223138-12.jar</systemPath>-->
      </dependency>
      <dependency>
        <groupId>com.google.dagger</groupId>
        <artifactId>dagger-compiler</artifactId>
        <version>2.0-SNAPSHOT</version>
        <optional>true</optional>
        <!--<scope>system</scope>-->
        <!--<systemPath>${project.basedir}/dagger-compiler-2.0-20141216.223201-12-jar-with-dependencies.jar</systemPath>-->
      </dependency>
  </dependencies>
</project>