This is an automated email from the ASF dual-hosted git repository. desruisseaux pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/maven-compiler-plugin.git
commit 338d53268cf8dec85c154d629ff1c7d3d1e50c2f Author: Martin Desruisseaux <[email protected]> AuthorDate: Sun Sep 21 12:38:02 2025 +0200 Fix the use of `--patch-module` option when a forked compiler is used. Includes the case of toolchains, which are implicitly forked processes. --- src/it/default-fork_modular/pom.xml | 46 +++++++++++++++++ .../src/main/java/foo/MyClass.java | 21 ++++++++ .../src/main/java/module-info.java | 21 ++++++++ .../src/test/java/foo/MyTest.java | 21 ++++++++ src/it/default-fork_modular/verify.groovy | 26 ++++++++++ .../maven/plugin/compiler/ForkedToolSources.java | 59 ++++++++++++++-------- .../apache/maven/plugin/compiler/ToolExecutor.java | 2 +- 7 files changed, 174 insertions(+), 22 deletions(-) diff --git a/src/it/default-fork_modular/pom.xml b/src/it/default-fork_modular/pom.xml new file mode 100644 index 0000000..6ec9e15 --- /dev/null +++ b/src/it/default-fork_modular/pom.xml @@ -0,0 +1,46 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + ~ Licensed to the Apache Software Foundation (ASF) under one + ~ or more contributor license agreements. See the NOTICE file + ~ distributed with this work for additional information + ~ regarding copyright ownership. The ASF licenses this file + ~ to you under the Apache License, Version 2.0 (the + ~ "License"); you may not use this file except in compliance + ~ with the License. You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, + ~ software distributed under the License is distributed on an + ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + ~ KIND, either express or implied. See the License for the + ~ specific language governing permissions and limitations + ~ under the License. + --> +<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>org.apache.maven.plugins.compiler.it</groupId> + <artifactId>default-fork-modular</artifactId> + <version>1.0-SNAPSHOT</version> + + <name>Test for default configuration in a modular project</name> + + <properties> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + </properties> + + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + <version>@pom.version@</version> + <configuration> + <fork>true</fork> + </configuration> + </plugin> + </plugins> + </build> + +</project> diff --git a/src/it/default-fork_modular/src/main/java/foo/MyClass.java b/src/it/default-fork_modular/src/main/java/foo/MyClass.java new file mode 100644 index 0000000..d4132bb --- /dev/null +++ b/src/it/default-fork_modular/src/main/java/foo/MyClass.java @@ -0,0 +1,21 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package foo; + +public class MyClass {} diff --git a/src/it/default-fork_modular/src/main/java/module-info.java b/src/it/default-fork_modular/src/main/java/module-info.java new file mode 100644 index 0000000..980a29d --- /dev/null +++ b/src/it/default-fork_modular/src/main/java/module-info.java @@ -0,0 +1,21 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +module com.foo { + exports foo; +} diff --git a/src/it/default-fork_modular/src/test/java/foo/MyTest.java b/src/it/default-fork_modular/src/test/java/foo/MyTest.java new file mode 100644 index 0000000..08064cd --- /dev/null +++ b/src/it/default-fork_modular/src/test/java/foo/MyTest.java @@ -0,0 +1,21 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package foo; + +public class MyTest {} diff --git a/src/it/default-fork_modular/verify.groovy b/src/it/default-fork_modular/verify.groovy new file mode 100644 index 0000000..016f409 --- /dev/null +++ b/src/it/default-fork_modular/verify.groovy @@ -0,0 +1,26 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +assert new File( basedir, 'target/classes/foo/MyClass.class').exists() +assert new File( basedir, 'target/test-classes/foo/MyTest.class').exists() +assert new File( basedir, 'target/classes/module-info.class').exists() + +// For each line, exactly one of the two files should exist. +assert new File( basedir, 'target/javac.sh').exists() != new File( basedir, 'target/javac.bat').exists() +assert new File( basedir, 'target/javac-test.sh').exists() != new File( basedir, 'target/javac-test.bat').exists() diff --git a/src/main/java/org/apache/maven/plugin/compiler/ForkedToolSources.java b/src/main/java/org/apache/maven/plugin/compiler/ForkedToolSources.java index c34bd35..fac6b5d 100644 --- a/src/main/java/org/apache/maven/plugin/compiler/ForkedToolSources.java +++ b/src/main/java/org/apache/maven/plugin/compiler/ForkedToolSources.java @@ -63,48 +63,45 @@ final class ForkedToolSources implements StandardJavaFileManager { * Option for source files. These options are not declared in * {@link JavaPathType} because they are not about dependencies. */ - private enum OtherPathType implements PathType { + private record OtherPathType(String name, String optionString, String moduleName) implements PathType { /** * The option for the directory of source files. */ - SOURCES("--source-path"), + static final OtherPathType SOURCES = new OtherPathType("SOURCES", "--source-path", null); /** * The option for the directory of generated sources. */ - GENERATED_SOURCES("-s"), + static final OtherPathType GENERATED_SOURCES = new OtherPathType("GENERATED_SOURCES", "-s", null); /** * The option for the directory of compiled class files. */ - OUTPUT("-d"); - - /** - * The Java option for this enumeration value. - */ - private final String option; - - OtherPathType(String option) { - this.option = option; - } + static final OtherPathType OUTPUT = new OtherPathType("OUTPUT", "-d", null); @Override public String id() { - return name(); + return name; } @Override public Optional<String> option() { - return Optional.of(option); + return Optional.of(optionString); } + /** + * Formats the option with the paths, <em>without quotes</em>. + * The quotes are omitted because the paths will be given to + * {@link java.lang.ProcessBuilder}, not to a command-line. + */ @Override public String[] option(Iterable<? extends Path> paths) { - var builder = new StringJoiner(File.pathSeparator); + String prefix = (moduleName == null) ? "" : (moduleName + '='); + var builder = new StringJoiner(File.pathSeparator, prefix, ""); paths.forEach((path) -> builder.add(path.toString())); - return new String[] {option, builder.toString()}; + return new String[] {optionString, builder.toString()}; } - }; + } /** * Search paths associated to locations. @@ -427,6 +424,28 @@ final class ForkedToolSources implements StandardJavaFileManager { } } + /** + * Associates the given search paths for a module with the given location. + * Any previous value will be discarded. + */ + @Override + public void setLocationForModule(Location location, String moduleName, Collection<? extends Path> paths) + throws IOException { + PathType type; + if (location == StandardLocation.PATCH_MODULE_PATH) { + type = JavaPathType.patchModule(moduleName); + } else if (location == StandardLocation.MODULE_SOURCE_PATH) { + type = new OtherPathType("MODULE_SOURCE_PATH", "--module-source-path", moduleName); + } else { + throw new IllegalArgumentException("Unsupported location: " + location); + } + if (paths == null || paths.isEmpty()) { + locations.remove(type); + } else { + locations.put(type, paths); + } + } + /** * Returns the search path associated with the given location, or {@code null} if none. */ @@ -451,9 +470,7 @@ final class ForkedToolSources implements StandardJavaFileManager { */ void addAllLocations(List<String> command) { for (Map.Entry<PathType, Collection<? extends Path>> entry : locations.entrySet()) { - for (String element : entry.getKey().option(entry.getValue())) { - command.add(element); - } + command.addAll(Arrays.asList(entry.getKey().option(entry.getValue()))); } } diff --git a/src/main/java/org/apache/maven/plugin/compiler/ToolExecutor.java b/src/main/java/org/apache/maven/plugin/compiler/ToolExecutor.java index 1b9a20c..ec3c521 100644 --- a/src/main/java/org/apache/maven/plugin/compiler/ToolExecutor.java +++ b/src/main/java/org/apache/maven/plugin/compiler/ToolExecutor.java @@ -521,7 +521,7 @@ public class ToolExecutor { */ private StandardJavaFileManager createFileManager(JavaCompiler compiler, boolean workaround) { StandardJavaFileManager fileManager = compiler.getStandardFileManager(listener, LOCALE, encoding); - if (workaround && WorkaroundForPatchModule.ENABLED) { + if (WorkaroundForPatchModule.ENABLED && workaround && !(compiler instanceof ForkedTool)) { fileManager = new WorkaroundForPatchModule(fileManager); } return fileManager;
