繁体   English   中英

如何配置 moditect-maven-plugin 以在我的模块化应用程序中使用自动值库?

[英]How can I configure the moditect-maven-plugin to use the auto-value library in my modular application?

我正在尝试构建一个模块化应用程序,并且我正在尝试使用 moditect-maven-plugin 来使用自动值库(它不是模块化的)。

根据这里的说明,我想出了以下 pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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>org.example</groupId>
<artifactId>moditect-test</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
    <dependency>
        <groupId>com.google.auto.value</groupId>
        <artifactId>auto-value-annotations</artifactId>
        <version>${auto-value.version}</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <annotationProcessorPaths>
                    <path>
                        <groupId>com.google.auto.value</groupId>
                        <artifactId>auto-value</artifactId>
                        <version>${auto-value.version}</version>
                    </path>
                </annotationProcessorPaths>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.moditect</groupId>
            <artifactId>moditect-maven-plugin</artifactId>
            <version>1.0.0.Beta2</version>
            <executions>
                <execution>
                    <id>generate-module-info</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>generate-module-info</goal>
                    </goals>
                    <configuration>
                        <modules>
                            <module>
                                <artifact>
                                    <groupId>com.google.auto.value</groupId>
                                    <artifactId>auto-value-annotations</artifactId>
                                    <version>${auto-value.version}</version>
                                </artifact>
                                <moduleInfo>
                                    <name>com.google.auto.value.annotations</name>
                                    <exports>
                                        com.google.auto.value;
                                        *;
                                    </exports>
                                </moduleInfo>
                            </module>
                        </modules>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

<properties>
    <maven.compiler.source>13</maven.compiler.source>
    <maven.compiler.target>13</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <auto-value.version>1.7</auto-value.version>
</properties>

重要的部分是 moditect-maven-plugin 配置。 由于我需要com.google.auto.value.AutoValue注释,我导出该包,然后为模块指定相关名称。

仅供参考,这是我在其中使用注释的类:

package com.example.moditect.test;

import com.google.auto.value.AutoValue;

@AutoValue
abstract class Animal {

    abstract String name();
    abstract int numberOfLegs();

    static Builder builder() {
        return new AutoValue_Animal.Builder();
    }

    @AutoValue.Builder
    abstract static class Builder {
        abstract Builder setName(String value);
        abstract Builder setNumberOfLegs(int value);
        abstract Animal build();
    }
}

...这是我的应用程序的module-info.java文件:

module com.example.moditect.test {
    requires com.google.auto.value.annotations;
}

但是,当我运行mvn clean install ,出现module not found错误:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project moditect-test: Compilation failure
[ERROR] /home/someuser/development/workspace/moditect-test/src/main/java/module-info.java:[2,35] module not found: com.google.auto.value.annotations

看起来 moditect-maven-plugin 正在根据这些日志工作,这些日志在我运行mvn clean install时输出:

[INFO] --- moditect-maven-plugin:1.0.0.Beta2:generate-module-info (generate-module-info) @ moditect-test ---
writing to /home/someuser/development/workspace/moditect-test/target/moditect/auto.value.annotations/module-info.java
[INFO] Created module descriptor at /home/someuser/development/workspace/moditect-test/target/generated-sources/modules/com.google.auto.value.annotations/module-info.java

对于它的价值,这里是这个自动值库生成的模块信息(来自上面记录的位置):

module com.google.auto.value.annotations {
    exports com.google.auto.value;
    exports com.google.auto.value.extension.memoized;
}

所以我需要一些帮助来弄清楚我做错了什么。 我假设我只是搞砸了 moditect-maven-plugin 的配置,但我不确定如何。

我犯了使用目标generate-module-info而不是add-module-info的愚蠢错误。 只需用正确的目标切换目标即可解决此问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM