扩展使用Maven构建的项目中的java.base

huangapple go评论65阅读模式
英文:

Extending java.base in a project built with Maven

问题

Maven 3.9.0在运行mvn clean compile时出现无效标志--patch-module的问题,这是因为该项目扩展了java.lang包。当我运行javac --patch-module java.base=src <path-to-source>时,一切正常。

原因是 - Maven使用com.sun.tools.javac.api.JavacTool,当解析<compilerArgs>时,它仅使用BASIC选项组,而--patch-module位于FILEMANAGER组。这会在mvn clean compile期间引发illegal flag异常。

有人知道如何在Maven中使它正常工作吗?我更希望不使用javac命令行工具来设置我的CI流程。

项目结构如下:
src -> main -> java -> java.lang ->

pom.xml如下:

<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.10.1</version>
        <configuration>
            <source>${java.source.sdk}</source>
            <target>${java.source.sdk}</target>
            <compilerArgs>
                <arg>--patch-module java.base=src</arg>
            </compilerArgs>
        </configuration>
    </plugin>
</plugins>

Maven版本:

Apache Maven 3.9.0 (9b58d2bad23a66be161c4664ef21ce219c2c8584)
Maven home: /opt/homebrew/Cellar/maven/3.9.0/libexec
Java version: 18, vendor: Oracle Corporation, runtime: /Users/<username>/Library/Java/JavaVirtualMachines/openjdk-18/Contents/Home
Default locale: en_RS, platform encoding: UTF-8
OS name: "mac os x", version: "12.6.2", arch: "aarch64", family: "mac"

使用JDK 19 - 相同的结果。使用Maven 3.10.1 - 相同的结果。

完整错误如下:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.10.1:compile (default-compile) on project commons-bootstrap: Fatal error compiling: error: invalid flag: --patch-module java.base=src -> [Help 1]

异常堆栈跟踪:

Caused by: java.lang.IllegalArgumentException: error: invalid flag: --patch-module java.base=src
    at com.sun.tools.javac.main.Arguments.reportDiag (Arguments.java:889)
    at com.sun.tools.javac.main.Arguments.doProcessArgs (Arguments.java:395)
    at com.sun.tools.javac.main.Arguments.processArgs (Arguments.java:347)
    at com.sun.tools.javac.main.Arguments.init (Arguments.java:246)
    at com.sun.tools.javac.api.JavacTool.getTask (JavacTool.java:191)
    at com.sun.tools.javac.api.JavacTool.getTask (JavacTool.java:119)
    at com.sun.tools.javac.api.JavacTool.getTask (JavacTool.java:68)
    at org.codehaus.plexus.compiler.javac.JavaxToolsCompiler.compileInProcess (JavaxToolsCompiler.java:135)
    at org.codehaus.plexus.compiler.javac.JavacCompiler.performCompile (JavacCompiler.java:182)
    at org.apache.maven.plugin.compiler.AbstractCompilerMojo.execute (AbstractCompilerMojo.java:1209)
    at org.apache.maven.plugin.compiler.CompilerMojo.execute (CompilerMojo.java:198)
    at org.apache.maven.plugin.DefaultBuildPluginManager executeMojo (DefaultBuildPluginManager.java:126)
    at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute2 (MojoExecutor.java:342)
    at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute (MojoExecutor.java:330)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:213)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:175)
    at org.apache.maven.lifecycle.internal.MojoExecutor.access$000 (MojoExecutor.java:76)
    at org.apache.maven.lifecycle.internal.MojoExecutor$1.run (MojoExecutor.java:163)
    at org.apache.maven.plugin DefaultMojosExecutionStrategy execute (DefaultMojosExecutionStrategy.java:39)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:160)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:105)
    at org.apache.maven.lifecycle.internal.builder singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:53)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:118)
    at org.apache.maven DefaultMaven.doExecute (DefaultMaven.java:260)
    at org.apache.maven DefaultMaven.doExecute (DefaultMaven.java:172)
    at org.apache.maven DefaultMaven.execute (DefaultMaven.java:100)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:821)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:270)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:192)
    at jdk.internal.reflect.DirectMethodHandleAccessor.invoke (DirectMethodHandleAccessor.java:104)
    at java.lang.reflect.Method.invoke (Method.java:577)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
英文:

Maven 3.9.0 invalid flag --patch-module is what I get when I run mvn clean compile on my project that extends java.lang package. When I run javac --patch-module java.base=src &lt;path-to-source&gt; all is good.

The reason is - Maven uses com.sun.tools.javac.api.JavacTool which when parsing &lt;compilerArgs&gt; is using only the BASIC Option group and --patch-module is in the FILEMANAGER group. This raises an illegal flag exception during mvn clean compile.

Does anyone know how I can get it to work with Maven? I'd prefer not to make my CI pipeline using javac command line tool.

Project structure is like:
src -> main -> java -> java.lang -> <classes>

pom.xml looks like:

        &lt;plugins&gt;
            &lt;plugin&gt;
                &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
                &lt;version&gt;3.10.1&lt;/version&gt;
                &lt;configuration&gt;
                    &lt;source&gt;${java.source.sdk}&lt;/source&gt;
                    &lt;target&gt;${java.source.sdk}&lt;/target&gt;
                    &lt;compilerArgs&gt;
                        &lt;arg&gt;--patch-module java.base=src&lt;/arg&gt;
                    &lt;/compilerArgs&gt;
                &lt;/configuration&gt;
            &lt;/plugin&gt;
        &lt;/plugins&gt;

Maven version:

Maven home: /opt/homebrew/Cellar/maven/3.9.0/libexec
Java version: 18, vendor: Oracle Corporation, runtime: /Users/&lt;username&gt;/Library/Java/JavaVirtualMachines/openjdk-18/Contents/Home
Default locale: en_RS, platform encoding: UTF-8
OS name: &quot;mac os x&quot;, version: &quot;12.6.2&quot;, arch: &quot;aarch64&quot;, family: &quot;mac&quot;

Used JDK 19 -> same result. Used Maven 3.10.1 -> same result.

Full error is:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.10.1:compile (default-compile) on project commons-bootstrap: Fatal error compiling: error: invalid flag: --patch-module java.base=src -&gt; [Help 1]

Exception stacktrace:

Caused by: java.lang.IllegalArgumentException: error: invalid flag: --patch-module java.base=src
    at com.sun.tools.javac.main.Arguments.reportDiag (Arguments.java:889)
    at com.sun.tools.javac.main.Arguments.doProcessArgs (Arguments.java:395)
    at com.sun.tools.javac.main.Arguments.processArgs (Arguments.java:347)
    at com.sun.tools.javac.main.Arguments.init (Arguments.java:246)
    at com.sun.tools.javac.api.JavacTool.getTask (JavacTool.java:191)
    at com.sun.tools.javac.api.JavacTool.getTask (JavacTool.java:119)
    at com.sun.tools.javac.api.JavacTool.getTask (JavacTool.java:68)
    at org.codehaus.plexus.compiler.javac.JavaxToolsCompiler.compileInProcess (JavaxToolsCompiler.java:135)
    at org.codehaus.plexus.compiler.javac.JavacCompiler.performCompile (JavacCompiler.java:182)
    at org.apache.maven.plugin.compiler.AbstractCompilerMojo.execute (AbstractCompilerMojo.java:1209)
    at org.apache.maven.plugin.compiler.CompilerMojo.execute (CompilerMojo.java:198)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:126)
    at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute2 (MojoExecutor.java:342)
    at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute (MojoExecutor.java:330)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:213)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:175)
    at org.apache.maven.lifecycle.internal.MojoExecutor.access$000 (MojoExecutor.java:76)
    at org.apache.maven.lifecycle.internal.MojoExecutor$1.run (MojoExecutor.java:163)
    at org.apache.maven.plugin.DefaultMojosExecutionStrategy.execute (DefaultMojosExecutionStrategy.java:39)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:160)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:105)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:73)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:53)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:118)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:260)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:172)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:100)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:821)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:270)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:192)
    at jdk.internal.reflect.DirectMethodHandleAccessor.invoke (DirectMethodHandleAccessor.java:104)
    at java.lang.reflect.Method.invoke (Method.java:577)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)

答案1

得分: 1

Maven将参数解析为一个带有嵌入式空格的长参数。

应该看起来像这样:

&lt;compilerArgs&gt;
    &lt;arg&gt;--patch-module&lt;/arg&gt;
    &lt;arg&gt;java.base=src&lt;/arg&gt;
&lt;/compilerArgs&gt;

请参阅Maven文档以获取示例。

英文:

Maven parses the argument as one long argument with embedded whitespace.

This is what it is supposed to look like:

&lt;compilerArgs&gt;
    &lt;arg&gt;--patch-module&lt;/arg&gt;
    &lt;arg&gt;java.base=src&lt;/arg&gt;
&lt;/compilerArgs&gt;

See the Maven documentation for example.

huangapple
  • 本文由 发表于 2023年2月18日 19:54:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75493155.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定