我正在尝试定义一个 Java 9 模块。我已经定义了类似的东西:
module my.module.name {
}
然后我的许多文件开始给我错误,他们找不到一些包。然后我使用了 IntelliJ 的自动帮助功能,它在我的 module-info.java 中添加了几个“需要”语句。所以它变成了这样:
module my.module.name {
requires spring.web;
requires sshd.core;
requires com.fasterxml.jackson.core;
....
}
现在 IntelliJ 显示我所有的代码没有错误。但是当我在“Maven 项目”窗口中单击“编译”时(我使用 Maven 3.5.3 和 Oracle JDK 10 进行编译),它告诉我以下信息:
src/main/java/module-info.java:[2,20] module not found: spring.web
src/main/java/module-info.java:[11,18] module not found: sshd.core
src/main/java/module-info.java:[13,35] module not found: com.fasterxml.jackson.core
...
same for the other modules I "require".
据我了解,未定义为模块的第 3 方库会生成“自动”模块。所以我找到了我需要的罐子之一:
C:\Users\<my username>\.m2\repository\org\springframework\spring-web\5.0.5.RELEASE\spring-web-5.0.5.RELEASE.jar
并想使用以下命令查看“自动”模块的名称是什么:
jar.exe" --file=spring-web-5.0.5.RELEASE.jar --describe-module
No module descriptor found. Derived automatic module.
spring.web@5.0.5.RELEASE automatic
...
所以根据我的调查,我“需要”的模块名称是正确的!我错过了什么?为什么我会得到
module not found: spring.web
和其他模块一样吗?我错过了一些路径还是什么?
编辑:根据要求,这是我的 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>mygroupid</groupId>
<artifactId>myartifactid</artifactId>
<version>1.0.1-RELEASE</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.9</java.version>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.sshd/apache-sshd -->
<dependency>
<groupId>org.apache.sshd</groupId>
<artifactId>apache-sshd</artifactId>
<version>1.7.0</version>
<type>pom</type>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<!-- https://mvnrepository.com/artifact/net.java.dev.jna/jna -->
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>4.5.1</version>
</dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna-platform</artifactId>
<version>4.5.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<descriptors>
<descriptor>assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.9</source>
<target>1.9</target>
</configuration>
</plugin>
</plugins>
</build>
Edit2: 我尝试将以下内容添加到 maven-compiler-plugin 配置中:
<compilerArgs>
<arg>--add-modules</arg><arg>spring.web</arg>
</compilerArgs>
然后尝试使用“mvn compile -X”对其进行编译以获取完整的调试日志,因此我执行了命令行进行编译:
[INFO] Changes detected - recompiling the module!
[DEBUG] Classpath:
[DEBUG] Source roots:
[DEBUG] D:\Work\gitrepos\mymoduleproject\src\main\java
[DEBUG] D:\Work\gitrepos\mymoduleproject\target\generated-sources\annotations
[DEBUG] Command line options:
[DEBUG] -d D:\Work\gitrepos\mymoduleproject\target\classes -sourcepath D:\Work\gitrepos\mymoduleproject\src\main\java;D:\Work\gitrepos\mymoduleproject\target\generated-sources\annotations; -s D:\Work\
gitrepos\mymoduleproject\target\generated-sources\annotations -g -nowarn -target 1.9 -source 1.9 -encoding UTF-8 --add-modules spring.web
但又一次......我明白了
module-info.java:[2,20] module not found: spring.web
和所有其他模块一样...
编辑3:
我创建了一个新项目,新项目编译良好(但我仍然需要编译原始项目!)。我做了同样的“mvn compile -X”命令,这就是我得到的:
[INFO] Changes detected - recompiling the module!
[DEBUG] Classpath:
[DEBUG] C:\Users\myuser\.m2\repository\org\springframework\spring-web\5.0.4.RELEASE\spring-web-5.0.4.RELEASE.jar
[DEBUG] C:\Users\myuser\.m2\repository\org\springframework\spring-beans\5.0.4.RELEASE\spring-beans-5.0.4.RELEASE.jar
[DEBUG] C:\Users\myuser\.m2\repository\org\springframework\spring-core\5.0.4.RELEASE\spring-core-5.0.4.RELEASE.jar
[DEBUG] C:\Users\myuser\.m2\repository\org\springframework\spring-jcl\5.0.4.RELEASE\spring-jcl-5.0.4.RELEASE.jar
[DEBUG] C:\Users\myuser\.m2\repository\org\springframework\boot\spring-boot-starter\2.0.0.RELEASE\spring-boot-starter-2.0.0.RELEASE.jar
[DEBUG] C:\Users\myuser\.m2\repository\org\springframework\spring-aop\5.0.4.RELEASE\spring-aop-5.0.4.RELEASE.jar
[DEBUG] C:\Users\myuser\.m2\repository\org\springframework\spring-expression\5.0.4.RELEASE\spring-expression-5.0.4.RELEASE.jar
[DEBUG] C:\Users\myuser\.m2\repository\org\springframework\boot\spring-boot-starter-logging\2.0.0.RELEASE\spring-boot-starter-logging-2.0.0.RELEASE.jar
[DEBUG] C:\Users\myuser\.m2\repository\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar
[DEBUG] C:\Users\myuser\.m2\repository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar
[DEBUG] C:\Users\myuser\.m2\repository\org\apache\logging\log4j\log4j-to-slf4j\2.10.0\log4j-to-slf4j-2.10.0.jar
[DEBUG] C:\Users\myuser\.m2\repository\org\apache\logging\log4j\log4j-api\2.10.0\log4j-api-2.10.0.jar
[DEBUG] C:\Users\myuser\.m2\repository\org\slf4j\jul-to-slf4j\1.7.25\jul-to-slf4j-1.7.25.jar
[DEBUG] C:\Users\myuser\.m2\repository\javax\annotation\javax.annotation-api\1.3.2\javax.annotation-api-1.3.2.jar
[DEBUG] C:\Users\myuser\.m2\repository\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar
[DEBUG] Modulepath:
[DEBUG] C:\Users\myuser\.m2\repository\org\springframework\boot\spring-boot-autoconfigure\2.0.0.RELEASE\spring-boot-autoconfigure-2.0.0.RELEASE.jar
[DEBUG] C:\Users\myuser\.m2\repository\org\springframework\boot\spring-boot\2.0.0.RELEASE\spring-boot-2.0.0.RELEASE.jar
[DEBUG] D:\Work\gitrepos\demo\javamodules\target\classes
[DEBUG] C:\Users\myuser\.m2\repository\org\springframework\spring-context\5.0.4.RELEASE\spring-context-5.0.4.RELEASE.jar
[DEBUG] Source roots:
[DEBUG] D:\Work\gitrepos\demo\javamodules\src\main\java
[DEBUG] D:\Work\gitrepos\demo\javamodules\target\generated-sources\annotations
[DEBUG] Command line options:
[DEBUG] -d D:\Work\gitrepos\demo\javamodules\target\classes -classpath C:\Users\myuser\.m2\repository\org\springframework\spring-web\5.0.4.RELEASE\spring-web-5.0.4.RELEASE.jar;C:\Users\myuser\.m2\repository\
org\springframework\spring-beans\5.0.4.RELEASE\spring-beans-5.0.4.RELEASE.jar;C:\Users\myuser\.m2\repository\org\springframework\spring-core\5.0.4.RELEASE\spring-core-5.0.4.RELEASE.jar;C:\Users\myuser\.m2\re
pository\org\springframework\spring-jcl\5.0.4.RELEASE\spring-jcl-5.0.4.RELEASE.jar;C:\Users\myuser\.m2\repository\org\springframework\boot\spring-boot-starter\2.0.0.RELEASE\spring-boot-starter-2.0.0.RELEAS
E.jar;C:\Users\myuser\.m2\repository\org\springframework\spring-aop\5.0.4.RELEASE\spring-aop-5.0.4.RELEASE.jar;C:\Users\myuser\.m2\repository\org\springframework\spring-expression\5.0.4.RELEASE\spring-expres
sion-5.0.4.RELEASE.jar;C:\Users\myuser\.m2\repository\org\springframework\boot\spring-boot-starter-logging\2.0.0.RELEASE\spring-boot-starter-logging-2.0.0.RELEASE.jar;C:\Users\myuser\.m2\repository\ch\qos\lo
gback\logback-classic\1.2.3\logback-classic-1.2.3.jar;C:\Users\myuser\.m2\repository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;C:\Users\myuser\.m2\repository\org\apache\logging\log4j\log4j-to-
slf4j\2.10.0\log4j-to-slf4j-2.10.0.jar;C:\Users\myuser\.m2\repository\org\apache\logging\log4j\log4j-api\2.10.0\log4j-api-2.10.0.jar;C:\Users\myuser\.m2\repository\org\slf4j\jul-to-slf4j\1.7.25\jul-to-slf4j-
1.7.25.jar;C:\Users\myuser\.m2\repository\javax\annotation\javax.annotation-api\1.3.2\javax.annotation-api-1.3.2.jar;C:\Users\myuser\.m2\repository\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar; --module-p
ath C:\Users\myuser\.m2\repository\org\springframework\boot\spring-boot-autoconfigure\2.0.0.RELEASE\spring-boot-autoconfigure-2.0.0.RELEASE.jar;C:\Users\myuser\.m2\repository\org\springframework\boot\spring-
boot\2.0.0.RELEASE\spring-boot-2.0.0.RELEASE.jar;D:\Work\gitrepos\demo\javamodules\target\classes;C:\Users\myuser\.m2\repository\org\springframework\spring-context\5.0.4.RELEASE\spring-context-5.0.4.RELEAS
E.jar; -sourcepath D:\Work\gitrepos\demo\javamodules\src\main\java;D:\Work\gitrepos\demo\javamodules\target\generated-sources\annotations; -s D:\Work\gitrepos\demo\javamodules\target\generated-sources\an
notations -g -parameters -nowarn -target 1.9 -source 1.9 -encoding UTF-8
为什么我在新项目中获得“模块路径”以及 Maven 命令行中的许多其他选项,而我在原始项目中没有获得它们?