From 394d38c586b77bb56eefebcfbd596912882b1f20 Mon Sep 17 00:00:00 2001 From: andrii serkes Date: Fri, 12 Mar 2021 09:58:26 +0100 Subject: [PATCH 1/2] Fix "module not found" error when using -J--release=11 option Signed-off-by: andrii serkes --- .../com/sun/tools/ws/wscompile/WsgenTool.java | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/jaxws-ri/tools/wscompile/src/main/java/com/sun/tools/ws/wscompile/WsgenTool.java b/jaxws-ri/tools/wscompile/src/main/java/com/sun/tools/ws/wscompile/WsgenTool.java index b30e89922..9e54e3b1e 100644 --- a/jaxws-ri/tools/wscompile/src/main/java/com/sun/tools/ws/wscompile/WsgenTool.java +++ b/jaxws-ri/tools/wscompile/src/main/java/com/sun/tools/ws/wscompile/WsgenTool.java @@ -55,6 +55,8 @@ import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import javax.tools.Diagnostic; import javax.tools.DiagnosticListener; import org.xml.sax.ext.Locator2Impl; @@ -69,6 +71,11 @@ public class WsgenTool { private final PrintStream out; private final WsgenOptions options = new WsgenOptions(); + private static final String RELEASE_MARK = "releaseMark"; + private static final String JAVA_VERSION_MARK = "javaVersion"; + private static final Pattern RELEASE_VERSION_PATTERN = + Pattern.compile("(?<" + RELEASE_MARK + ">--release=)(?<" + JAVA_VERSION_MARK + ">[0-9]+)"); + private static final int JAVA_MODULE_VERSION = 9; public WsgenTool(OutputStream out, Container container) { @@ -138,8 +145,24 @@ public boolean buildModel(String endpoint, Listener listener) throws BadCommandL args.add("-d"); args.add(options.destDir.getAbsolutePath()); - args.add("-classpath"); + + int javaVersion = 0; + if (options.javacOptions != null){ + for (String option: options.javacOptions){ + final Matcher matcher = RELEASE_VERSION_PATTERN.matcher(option); + if (matcher.find()){ + javaVersion = Integer.parseInt(matcher.group(JAVA_VERSION_MARK)); + break; + } + } + } + if (javaVersion < JAVA_MODULE_VERSION) { + args.add("-classpath"); + } else { + args.add("--module-path"); + } args.add(options.classpath); + args.add("-s"); args.add(options.sourceDir.getAbsolutePath()); if (options.nocompile) { From 568c62844c1ffce04b0f51286b08d094dca59528 Mon Sep 17 00:00:00 2001 From: aserkes Date: Wed, 7 Apr 2021 12:11:10 +0200 Subject: [PATCH 2/2] add -Xno-modules and -modulepath for wsgen and javac for java version 9 and above Signed-off-by: aserkes --- .../jaxws/AbstractJaxwsMojo.java | 30 +++-- .../jvnet/jax_ws_commons/jaxws/Invoker.java | 8 ++ .../com/sun/tools/ws/wscompile/Options.java | 21 ++++ .../com/sun/tools/ws/wscompile/WsgenTool.java | 107 ++++++++++++++++-- 4 files changed, 149 insertions(+), 17 deletions(-) diff --git a/jaxws-ri/extras/jaxws-maven-plugin/src/main/java/org/jvnet/jax_ws_commons/jaxws/AbstractJaxwsMojo.java b/jaxws-ri/extras/jaxws-maven-plugin/src/main/java/org/jvnet/jax_ws_commons/jaxws/AbstractJaxwsMojo.java index 016eedec7..98a1196a4 100644 --- a/jaxws-ri/extras/jaxws-maven-plugin/src/main/java/org/jvnet/jax_ws_commons/jaxws/AbstractJaxwsMojo.java +++ b/jaxws-ri/extras/jaxws-maven-plugin/src/main/java/org/jvnet/jax_ws_commons/jaxws/AbstractJaxwsMojo.java @@ -23,6 +23,7 @@ import java.net.URI; import java.net.URISyntaxException; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; @@ -32,6 +33,7 @@ import java.util.Set; import java.util.logging.Level; import java.util.logging.Logger; + import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.versioning.ArtifactVersion; import org.apache.maven.artifact.versioning.OverConstrainedVersionException; @@ -44,6 +46,7 @@ import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.project.MavenProject; import org.codehaus.plexus.util.Os; +import org.codehaus.plexus.util.cli.Arg; import org.codehaus.plexus.util.cli.CommandLineException; import org.codehaus.plexus.util.cli.CommandLineUtils; import org.codehaus.plexus.util.cli.Commandline; @@ -59,7 +62,6 @@ import org.eclipse.aether.util.graph.visitor.PreorderNodeListGenerator; /** - * * @author dantran * @version $Id: AbstractJaxwsMojo.java 3240 2007-02-04 07:13:21Z dantran $ * */ @@ -246,7 +248,7 @@ protected List getCommonArgs() throws MojoExecutionException { commonArgs.add("-extension"); } - if(getXnocompile()){ + if (getXnocompile()) { commonArgs.add("-Xnocompile"); } @@ -319,7 +321,7 @@ protected void exec(List args) throws MojoExecutionException { if (executable != null) { if (executable.isFile() && executable.canExecute()) { cmd.setExecutable(executable.getAbsolutePath()); - if (getExtraClasspath() != null) { + if (getExtraClasspath() != null) { cmd.createArg().setLine("-cp"); cmd.createArg().setValue(getExtraClasspath()); } @@ -343,7 +345,13 @@ protected void exec(List args) throws MojoExecutionException { String cp = extraCp != null ? extraCp + File.pathSeparator : ""; cp += classpath[0]; try { - File pathFile = createPathFile(cp); + Map cpMap = new HashMap<>(); + cpMap.put("cp", cp); + cpMap.put("wsgen.classpath", classpath[0]); + if (extraCp != null) { + cpMap.put("wsgen.extra.classpath", extraCp); + } + File pathFile = createPathFile(cpMap); cmd.createArg().setLine("-pathfile " + pathFile.getAbsolutePath()); } catch (IOException ioe) { //creation of temporary file can fail, in such case just put everything on cp @@ -445,7 +453,7 @@ private String getJavaExec() { return isWindows() ? "java.exe" : "java"; } - private File createPathFile(String cp) throws IOException { + private File createPathFile(Map cpMap) throws IOException { File f = File.createTempFile("jax-ws-mvn-plugin-cp", ".txt"); if (f.exists() && f.isFile()) { if (!f.delete()) { @@ -454,8 +462,14 @@ private File createPathFile(String cp) throws IOException { } } Properties p = new Properties(); - p.put("cp", cp.replace(File.separatorChar, '/')); - getLog().debug("stored classpath: " + cp.replace(File.separatorChar, '/')); + cpMap.forEach((key, value) -> { + p.put(key, value.replace(File.separatorChar, '/')); + getLog().debug(String.format( + "stored classpath: %s - %s\n", + key, + value.replace(File.separatorChar, '/') + )); + }); FileOutputStream fos = null; try { fos = new FileOutputStream(f); @@ -497,7 +511,7 @@ private void sortArtifacts(DependencyResult result, Map cp) { - return cp.contains("com.sun.xml.ws:jaxws-tools") + return cp.contains("com.sun.xml.ws:jaxws-tools") || cp.contains("org.glassfish.metro:webservices-tools") || cp.contains("com.oracle.weblogic:weblogic-server-pom"); } diff --git a/jaxws-ri/extras/jaxws-maven-plugin/src/main/java/org/jvnet/jax_ws_commons/jaxws/Invoker.java b/jaxws-ri/extras/jaxws-maven-plugin/src/main/java/org/jvnet/jax_ws_commons/jaxws/Invoker.java index 2eda224d0..d84d8a872 100644 --- a/jaxws-ri/extras/jaxws-maven-plugin/src/main/java/org/jvnet/jax_ws_commons/jaxws/Invoker.java +++ b/jaxws-ri/extras/jaxws-maven-plugin/src/main/java/org/jvnet/jax_ws_commons/jaxws/Invoker.java @@ -42,12 +42,16 @@ public final class Invoker { public static void main(String... args) throws Exception { int idx = 1; String c = args[2]; + String wsgenClasspath = null; + String wsgenExtraClasspath = null; if ("-pathfile".equals(args[1])) { Properties p = new Properties(); File pathFile = new File(args[2]); pathFile.deleteOnExit(); p.load(new FileInputStream(pathFile)); c = p.getProperty("cp"); + wsgenClasspath = p.getProperty("wsgen.classpath"); + wsgenExtraClasspath = p.getProperty("wsgen.extra.classpath"); idx = 3; } List cp = new ArrayList(); @@ -66,6 +70,10 @@ public static void main(String... args) throws Exception { String origJcp = System.getProperty("java.class.path"); Thread.currentThread().setContextClassLoader(cl); System.setProperty("java.class.path", c); + System.setProperty("wsgen.classpath", wsgenClasspath); + if (wsgenExtraClasspath != null) { + System.setProperty("wsgen.extra.classpath", wsgenExtraClasspath); + } try { Class compileTool = cl.loadClass(args[0]); Constructor ctor = compileTool.getConstructor(OutputStream.class); diff --git a/jaxws-ri/tools/wscompile/src/main/java/com/sun/tools/ws/wscompile/Options.java b/jaxws-ri/tools/wscompile/src/main/java/com/sun/tools/ws/wscompile/Options.java index 9bdc72766..e0633cce1 100644 --- a/jaxws-ri/tools/wscompile/src/main/java/com/sun/tools/ws/wscompile/Options.java +++ b/jaxws-ri/tools/wscompile/src/main/java/com/sun/tools/ws/wscompile/Options.java @@ -81,6 +81,10 @@ public class Options { public String classpath = System.getProperty("java.class.path"); + public String wsgenExtraClasspath = System.getProperty("wsgen.extra.classpath"); + + public String wsgenClasspath = System.getProperty("wsgen.classpath"); + /** * -javacOptions * @@ -102,6 +106,17 @@ public class Options { */ public boolean disableXmlSecurity; + /** + * -Xno-modules + * If it is true, java compiler will not use modules during the compilation. All dependencies will be put to the classpath. + */ + public boolean noModules; + + /** + * -modulepath + */ + public String modulepath; + public enum Target { V2_0("2.0"), V2_1("2.1"), V2_2("2.2"), V2_3("2.3"), V3_0("3.0"); @@ -368,6 +383,12 @@ protected int parseArguments(String[] args, int i) throws BadCommandLineExceptio } javacOptions.add(args[i].substring(2)); return 1; + } else if (args[i].equals("-Xno-modules")) { + noModules = true; + return 1; + } else if (args[i].equals("-modulepath")) { + modulepath = requireArgument("-modulepath", args, ++i); + return 2; } return 0; } diff --git a/jaxws-ri/tools/wscompile/src/main/java/com/sun/tools/ws/wscompile/WsgenTool.java b/jaxws-ri/tools/wscompile/src/main/java/com/sun/tools/ws/wscompile/WsgenTool.java index 9e54e3b1e..bdf37f9d9 100644 --- a/jaxws-ri/tools/wscompile/src/main/java/com/sun/tools/ws/wscompile/WsgenTool.java +++ b/jaxws-ri/tools/wscompile/src/main/java/com/sun/tools/ws/wscompile/WsgenTool.java @@ -40,7 +40,9 @@ import javax.xml.namespace.QName; import javax.xml.transform.Result; import javax.xml.transform.stream.StreamResult; + import jakarta.xml.ws.Holder; + import java.io.BufferedOutputStream; import java.io.File; import java.io.FileNotFoundException; @@ -50,15 +52,20 @@ import java.io.PrintStream; import java.net.URLClassLoader; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.stream.Collectors; import javax.tools.Diagnostic; import javax.tools.DiagnosticListener; + import org.xml.sax.ext.Locator2Impl; /** @@ -76,6 +83,8 @@ public class WsgenTool { private static final Pattern RELEASE_VERSION_PATTERN = Pattern.compile("(?<" + RELEASE_MARK + ">--release=)(?<" + JAVA_VERSION_MARK + ">[0-9]+)"); private static final int JAVA_MODULE_VERSION = 9; + private static final String CLASSPATH_KEY = "--class-path"; + private static final String MODULEPATH_KEY = "--module-path"; public WsgenTool(OutputStream out, Container container) { @@ -130,7 +139,6 @@ public boolean run(String[] args) { private final Container container; /** - * * @param endpoint * @param listener * @return @@ -147,21 +155,21 @@ public boolean buildModel(String endpoint, Listener listener) throws BadCommandL args.add(options.destDir.getAbsolutePath()); int javaVersion = 0; - if (options.javacOptions != null){ - for (String option: options.javacOptions){ + if (options.javacOptions != null) { + for (String option : options.javacOptions) { final Matcher matcher = RELEASE_VERSION_PATTERN.matcher(option); - if (matcher.find()){ + if (matcher.find()) { javaVersion = Integer.parseInt(matcher.group(JAVA_VERSION_MARK)); break; } } } - if (javaVersion < JAVA_MODULE_VERSION) { + if (javaVersion < JAVA_MODULE_VERSION || options.noModules) { args.add("-classpath"); + args.add(options.classpath); } else { - args.add("--module-path"); + args.addAll(generatePathArgs(args)); } - args.add(options.classpath); args.add("-s"); args.add(options.sourceDir.getAbsolutePath()); @@ -335,6 +343,87 @@ public Result getSchemaOutput(String namespace, Holder filename) { return true; } + private List generatePathArgs(List args) { + List result = new ArrayList<>(); + Map separatedDependencies = new HashMap<>(); + Map separatedWsgenClasspath = splitWsgenClasspath(); + separatedWsgenClasspath.computeIfPresent(CLASSPATH_KEY, separatedDependencies::put); + separatedWsgenClasspath.computeIfPresent(MODULEPATH_KEY, separatedDependencies::put); + if (options.modulepath != null) { + Map separatedExtraClasspath = splitExtraClasspath(); + separatedExtraClasspath.computeIfPresent(CLASSPATH_KEY, (key, value) -> + separatedDependencies.merge( + CLASSPATH_KEY, value, + (oldValue, newValue) -> oldValue + File.pathSeparator + value + )); + separatedExtraClasspath.computeIfPresent(MODULEPATH_KEY, (key, value) -> + separatedDependencies.merge( + MODULEPATH_KEY, value, + (oldValue, newValue) -> oldValue + File.pathSeparator + value + )); + } + if (separatedDependencies.containsKey(CLASSPATH_KEY)) { + result.add(CLASSPATH_KEY); + result.add(separatedDependencies.get(CLASSPATH_KEY)); + } + if (separatedDependencies.containsKey(MODULEPATH_KEY)) { + result.add(MODULEPATH_KEY); + result.add(separatedDependencies.get(MODULEPATH_KEY)); + } + return result; + } + + private Map splitExtraClasspath() { + Map result = new HashMap<>(); + String cp = options.wsgenExtraClasspath; + String mp = options.modulepath; + if (cp == null || cp.isEmpty()) { + if (mp == null || mp.isEmpty()){ + return result; + } + result.put(MODULEPATH_KEY, mp); + return result; + } + if (mp == null || mp.isEmpty()){ + result.put(CLASSPATH_KEY, mp); + return result; + } + Set splitCP = new HashSet(Arrays.asList(cp.split(File.pathSeparator))); + Set splitMP = new HashSet(Arrays.asList(mp.split(File.pathSeparator))); + Set onlyCP = new HashSet<>(splitCP); + onlyCP.removeAll(splitMP); + if (!onlyCP.isEmpty()) { + result.put(CLASSPATH_KEY, String.join(File.pathSeparator, onlyCP)); + } + if (!splitMP.isEmpty()) { + result.put(MODULEPATH_KEY, mp); + } + return result; + } + + private Map splitWsgenClasspath() { + Map result = new HashMap<>(); + String mp = options.wsgenClasspath; + if (mp == null || mp.isEmpty()) { + return result; + } + List classpathJarNames = Arrays.asList("ha-api"); + Set splitMP = new HashSet(Arrays.asList(mp.split(File.pathSeparator))); + Set onlyMP = new HashSet<>(splitMP); + Set onlyCP = onlyMP.stream() + .filter(name -> classpathJarNames.stream().anyMatch(name::contains)) + .collect(Collectors.toSet()); + onlyMP.removeAll(onlyCP); + if (!onlyMP.isEmpty()) { + result.put(MODULEPATH_KEY, String.join(File.pathSeparator, onlyMP)); + } + if (!onlyCP.isEmpty()) { + + result.put(CLASSPATH_KEY, String.join(File.pathSeparator, onlyCP)); + } + return result; + } + private String property(String key) { try { String property = System.getProperty(key); @@ -444,8 +533,8 @@ protected void usage(Options options) { options = this.options; if (options instanceof WsgenOptions) { System.out.println(WscompileMessages.WSGEN_HELP("WSGEN", - ((WsgenOptions)options).protocols, - ((WsgenOptions)options).nonstdProtocols.keySet())); + ((WsgenOptions) options).protocols, + ((WsgenOptions) options).nonstdProtocols.keySet())); System.out.println(WscompileMessages.WSGEN_USAGE_EXTENSIONS()); System.out.println(WscompileMessages.WSGEN_USAGE_EXAMPLES()); }