我成功地使用了Maven,而不需要module-info.java
文件。 我的POM和您的POM之间的主要区别似乎在于我如何设置编译器参数。 而不是你的:
<configuration>
<compilerArgs>--source 19</compilerArgs>
<compilerArgs>--enable-preview</compilerArgs>
<compilerArgs>--add-modules jdk.incubator.concurrent</compilerArgs>
<compilerVersion>19</compilerVersion>
<source>19</source>
<target>19</target>
</configuration>
我拥有:
<configuration>
<compilerArgs>
<arg>--add-modules=jdk.incubator.concurrent</arg>
</compilerArgs>
</configuration>
注意我在中嵌套了元素。虽然我也在--add-modules参数中使用了等号=,但我不知道那是否是必须的。看来--enable-preview只需要在运行时而不是编译时使用。你拥有的其他参数我以另一种方式进行设置(例如,元素)。
示例
以下是一个示例,其中设置为JDK 19安装。
源代码/构建文件
App.java:
package sample;
import jdk.incubator.concurrent.StructuredTaskScope;
public class App {
public static void main( String[] args ) throws Exception {
try (var scope = new StructuredTaskScope<Void>()) {
scope.fork(() -> delayPrint(1000, "Hello,"));
scope.fork(() -> delayPrint(2000, "World!"));
scope.join();
}
System.out.println("Done!");
}
private static Void delayPrint(long delay, String message) throws Exception {
Thread.sleep(delay);
System.out.println(message);
return null;
}
}
pom.xml:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>sample</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>sample</name>
<url>http://maven.apache.org</url>
<properties>
<maven.compiler.source>19</maven.compiler.source>
<maven.compiler.target>19</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<compilerArgs>
<arg>--add-modules=jdk.incubator.concurrent</arg>
</compilerArgs>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<executable>${java.home}/bin/java</executable>
<arguments>
<argument>--add-modules=jdk.incubator.concurrent</argument>
<argument>--enable-preview</argument>
<argument>--class-path</argument>
<classpath/>
<argument>sample.App</argument>
</arguments>
</configuration>
</plugin>
</plugins>
</build>
</project>
输出
编译:
> mvn compile
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------------< com.example:sample >-------------------------
[INFO] Building sample 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ sample ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\***\Desktop\structured_concurrency\sample\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.10.1:compile (default-compile) @ sample ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to C:\Users\***\Desktop\structured_concurrency\sample\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.439 s
[INFO] Finished at: 2022-08-08T01:01:26-06:00
[INFO] ------------------------------------------------------------------------
执行:
> mvn exec:exec
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------------< com.example:sample >-------------------------
[INFO] Building sample 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- exec-maven-plugin:3.1.0:exec (default-cli) @ sample ---
WARNING: Using incubator modules: jdk.incubator.concurrent
Hello,
World!
Done!
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.610 s
[INFO] Finished at: 2022-08-08T01:02:42-06:00
[INFO] ------------------------------------------------------------------------
module-info.java
文件,如此处所讨论的那样(https://dev59.com/_cTra4cB1Zd3GeqP20rd#72072054)。 - Basil Bourque--add-modules
参数或具有requires
指令,但不能同时使用。您可以在命令行上(即使用javac
和java
)使其正常工作吗?也许Maven没有按照您的期望执行。 - Slaw--add-modules=jdk.incubator.concurrent
(带有=
),会怎样呢? - Slaw