类路径中的拼图测试依赖:为什么另一个JAR中的超类无法访问?

时间:2017-01-05 10:46:45

标签: maven classpath java-9 jigsaw

我目前正在测试使用jdk9-ea + 147和maven-compiler-plugin 3.6.0将现有的Java 8(Maven)项目迁移到Java 9 / Jigsaw。

我已将src目录转换为模块,并使test目录保持非模块化。

testCompile期间,我收到了意外错误:

  

错误:ExtractedArtifactStoreBuilder中的build()是在一个无法访问的类或接口中定义的

我当然检查过ExtractedArtifactStoreBuilder是否公开。 build()也从其(公共)超类和公共继承而来。代码在JDK 8中编译并运行良好。

ExtractArtifactStoreBuilder在不同的第三部分JAR中定义,而不是超类,但是Maven正确地将两者放在类路径中。令人困惑的是,超类具有相同的类名(但位于不同的包中)

据我所知,我应该能够在我的测试代码中访问公共继承的方法。那么这是jdk9早期访问版本中的一个错误吗?

编辑:希望更好地理解,这里是对所涉及的JAR和类的一点抽象,减少了令人困惑的命名和不重要的东西(对于实际的依赖性,见下文):

process.jar

public ProcessStoreBuilder
    public ProcessStoreBuilder download(...) // returns "this"
    public ... build()

mongo.jar

public MongoStoreBuilder extends ProcessStoreBuilder

的src /测试/ JAVA / ExampleTest

mongoStoreBuilder.download(...).build()
//                              ^ breaks at compile time, saying that
//                              ProcessStoreBuilder#build() is not accessible

我能够以最小的设置重现行为:

pom.xml (摘录)

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.6.0</version>
            <configuration>
                <source>9</source>
                <target>9</target>
                <debug>true</debug>
                <optimize>true</optimize>
            </configuration>
        </plugin>
    </plugins>
</build>

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

    <dependency>
        <groupId>de.flapdoodle.embed</groupId>
        <artifactId>de.flapdoodle.embed.mongo</artifactId>
        <version>1.50.0</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>de.flapdoodle.embed</groupId>
        <artifactId>de.flapdoodle.embed.process</artifactId>
        <version>1.50.0</version>
        <scope>test</scope>
    </dependency>
</dependencies>

的src /主/爪哇/ module-info.java

module com.mycompany.example.testdependencyexample {}

src / test / java / ExampleTest.java (摘录)

@Before
public void setUp() throws Exception {

    // both `download` and `build` are inherited from the superclass

    // the following does work
    // de.flapdoodle.embed.mongo.config.ExtractedArtifactStoreBuilder
    ExtractedArtifactStoreBuilder easb =  new ExtractedArtifactStoreBuilder();
    easb.download(new DownloadConfigBuilder().build());
    easb.build();

    // but this does not
    // download returns the same instance but has the superclass as return type
    // de.flapdoodle.embed.process.store.ExtractedArtifactStoreBuilder
    // the compiler can't see the `build` method of the superclass
    new ExtractedArtifactStoreBuilder()
            .download(new DownloadConfigBuilder().build())
            .build();

}

0 个答案:

没有答案