下面的代码使用Spring Tool Suite(版本3.9.2)和Java JDK 1.8正常工作(编译和运行)。这是一个常规的Java Web应用程序,其中Spring Framework与Maven处于项目/子项目结构中。
以下课程属于"核心"模块(子项目):
package my.core.util;
import java.util.*;
import java.util.function.*;
public interface StreamUtils {
/**
* To filter unique by a key
*/
static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
final Set<Object> seen = new HashSet<>();
return t -> seen.add(keyExtractor.apply(t));
}
}
使用上述方法的代码(FileServiceImpl.java)属于&#34; web&#34;模块:
List<DcType> dcTypes = Lists.newArrayList(quote.getDCTypes());
List<DcType> distinct = dcTypes.stream().filter(StreamUtils.distinctByKey(DcType::getId)).collect(Collectors.toList());
但是,如果我运行Maven构建,则会失败并显示以下错误消息:
[INFO] my ................................................. SUCCESS [ 2.161 s]
[INFO] my-core ............................................ SUCCESS [01:45 min]
[INFO] my-web ............................................. FAILURE [ 3.007 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.7.0:compile (default-compile) on project my-web: Compilation failure
[ERROR] /C:/dev/workspace/my/my-web/src/main/java/my/web/service/FileServiceImpl.java:[43,35] cannot access my.core.util.StreamUtils
[ERROR] bad class file: C:\dev\workspace\my\my-core\target\my-core-1.16.4-SNAPSHOT.jar(my/core/util/StreamUtils.class)
[ERROR] undeclared type variable: T
[ERROR] Please remove or make sure it appears in the correct subdirectory of the classpath.
这是主(root)pom.xml中的Maven插件pom文件配置片段。
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<encoding>UTF-8</encoding>
<source>1.8</source>
<target>1.8</target>
<compilerArgs>
<arg>-proc:none</arg>
<arg>-Xlint</arg>
</compilerArgs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<skip>true</skip>
<additionalparam>-Xdoclint:none</additionalparam>
</configuration>
</execution>
</executions>
</plugin>
有趣的是,代码(StreamUtil)通常用于不同的项目中。