不可变对象不使用 java 9 和模块生成代码

Immutables don't generate code with java 9 with modules

使用 immutables-library 与 java 9 一起使用效果很好,直到我将 module-info.java 添加到项目中,将不再生成 Immutables*.java

我按照 IntelliJ 的建议在模块信息中添加 'requires value'。

我错过了什么,是 immutables-library 问题还是我需要设置的其他内容以便 javac 找到注释处理。

我正在使用 Maven,maven-compiler-plugin:3.7.0 配置为 target/source = 9.

您遇到的问题是您没有将 Immutable 部分配置为注解处理器,应该这样做:

<?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>example</groupId>
    <artifactId>jigsaw</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.immutables</groupId>
            <artifactId>value</artifactId>
            <version>2.5.6</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.7.0</version>
          <configuration>
            <source>9</source>
            <target>9</target>
            <annotationProcessorPaths>
              <dependency>
                  <groupId>org.immutables</groupId>
                  <artifactId>value</artifactId>
                  <version>2.5.6</version>
              </dependency>
            </annotationProcessorPaths>
          </configuration>
        </plugin>
      </plugins>
    </build>
</project>

除了关于编码的提示之外,可以通过像这样定义编码来简单地解决:

<?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>example</groupId>
    <artifactId>jigsaw</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.immutables</groupId>
            <artifactId>value</artifactId>
            <version>2.5.6</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.7.0</version>
          <configuration>
            <source>9</source>
            <target>9</target>
            <annotationProcessorPaths>
              <dependency>
                  <groupId>org.immutables</groupId>
                  <artifactId>value</artifactId>
                  <version>2.5.6</version>
              </dependency>
            </annotationProcessorPaths>
          </configuration>
        </plugin>
      </plugins>
    </build>
</project>

如果您通过上述配置构建,您将获得所需的一切:

.
├── pom.xml
├── src
│   └── main
│       └── java
│           ├── example
│           │   └── Some.java
│           └── module-info.java
└── target
    ├── classes
    │   ├── example
    │   │   ├── ImmutableSome.class
    │   │   ├── ImmutableSome$Builder.class
    │   │   ├── ImmutableSome.class
    │   │   └── Some.class
    │   └── module-info.class
    ├── generated-sources
    │   └── annotations
    │       └── example
    │           └── ImmutableSome.java
    ├── jigsaw-1.0-SNAPSHOT.jar
    ├── maven-archiver
    │   └── pom.properties
    └── maven-status
        └── maven-compiler-plugin
            └── compile
                └── default-compile
                    ├── createdFiles.lst
                    └── inputFiles.lst