java - 使用 kotlin-maven-plugin 进行注解处理

标签 java maven kotlin

所以我有一个包含 2 个模块的 maven 项目。

根 pom.xml 如下所示:

<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>kashier</groupId>
<artifactId>Kashier</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>

<modules>
    <module>AnnotationProcessing</module>
    <module>TestProject</module>
</modules>

AnnotationProcessing 模块的 pom.xml:

<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>


<parent>
    <groupId>kashier</groupId>
    <artifactId>Kashier</artifactId>
    <version>1.0-SNAPSHOT</version>
</parent>

<artifactId>AnnotationProcessing</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<properties>
    <kotlin.version>1.0.3</kotlin.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.jetbrains.kotlin</groupId>
        <artifactId>kotlin-stdlib</artifactId>
        <version>${kotlin.version}</version>
    </dependency>
    <dependency>
        <groupId>org.jetbrains.kotlin</groupId>
        <artifactId>kotlin-test</artifactId>
        <version>${kotlin.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.google.auto.service</groupId>
        <artifactId>auto-service</artifactId>
        <version>1.0-rc2</version>
        <optional>true</optional>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-maven-plugin</artifactId>
            <version>${kotlin.version}</version>
            <executions>
                <execution>
                    <id>compile</id>
                    <phase>compile</phase>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
                <execution>
                    <id>test-compile</id>
                    <phase>test-compile</phase>
                    <goals>
                        <goal>test-compile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

现在,只有当我在资源目录中手动创建 javax.annotation.processing.Processor 文件时,一切才能正常工作。 这意味着自动服务不工作,问题是:我如何让它工作?

我还尝试用 java 制作相同的项目并且它有效。所以我认为问题出在 kotlin-maven-plugin 中。

可以找到项目的源代码here

最佳答案

auto-service 也使用注解处理器,因此您必须将此添加到您的 kotlin-maven-plugin(在编译部分之前):

<execution>
    <id>kapt</id>
    <goals>
        <goal>kapt</goal>
    </goals>
    <configuration>
        <sourceDirs>
            <sourceDir>src/main/kotlin</sourceDir>
            <sourceDir>src/main/java</sourceDir>
        </sourceDirs>
        <annotationProcessorPaths>
            <!-- Specify your annotation processors here. -->
            <annotationProcessorPath>
                <groupId>com.google.auto.service</groupId>
                <artifactId>auto-service</artifactId>
                <version>1.0-rc4</version>
            </annotationProcessorPath>
        </annotationProcessorPaths>
    </configuration>
</execution>

关于java - 使用 kotlin-maven-plugin 进行注解处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39552403/

相关文章:

java - Sun 的统一日志记录格式是否已在任何地方发布过?

java - 有没有办法从 .dex 文件中获取所有类的列表?

android - 从 Google Maven 手动下载 JAR

android - 如何在上下文菜单中创建弹出菜单?

kotlin.jvm.KotlinReflectionNotSupportedError : Kotlin reflection implementation is not found at runtime. 确保你有 kotlin-reflect.jar

Java - 使用文本区域将 JTable 设置为不可编辑

java - 如何为 Kerberos 身份验证配置 JBoss EAP 6.3 WebApp

java - 使用自定义存储库中的依赖项构建 JAR

java - 需要了解 WAR 描述符和结构 - WAR 中的 Flex 项目

java - 如何创建模块化的java项目?