diff --git a/pom.xml b/pom.xml index 5c44776..b6bca9b 100644 --- a/pom.xml +++ b/pom.xml @@ -157,6 +157,12 @@ test + + org.slf4j + jul-to-slf4j + 1.7.36 + + diff --git a/src/main/java/io/leonard/maven/plugins/jspc/JspCContextAccessor.java b/src/main/java/io/leonard/maven/plugins/jspc/JspCContextAccessor.java index a661883..d531a63 100644 --- a/src/main/java/io/leonard/maven/plugins/jspc/JspCContextAccessor.java +++ b/src/main/java/io/leonard/maven/plugins/jspc/JspCContextAccessor.java @@ -2,10 +2,14 @@ import java.io.IOException; import java.util.Map; - -import org.apache.jasper.*; -import org.apache.jasper.compiler.*; +import org.apache.jasper.JasperException; +import org.apache.jasper.JspC; +import org.apache.jasper.compiler.JspConfig; +import org.apache.jasper.compiler.TldCache; import org.apache.jasper.servlet.JspCServletContext; +import org.apache.tomcat.JarScanner; +import org.apache.tomcat.util.scan.StandardJarScanFilter; +import org.apache.tomcat.util.scan.StandardJarScanner; import org.eclipse.jdt.internal.compiler.env.NameEnvironmentAnswer; public class JspCContextAccessor extends JspC { @@ -13,6 +17,12 @@ public class JspCContextAccessor extends JspC { private Map resourcesCache; private String compilerClass; + private String tldSkip; + + private String tldScan; + + private Boolean defaultTldScan; + public JspCContextAccessor() { super(); } @@ -50,6 +60,18 @@ public void setcompilerClass(String compilerClass) { this.compilerClass = compilerClass; } + public void setTldSkip(String tldSkip) { + this.tldSkip = tldSkip; + } + + public void setTldScan(String tldScan) { + this.tldScan = tldScan; + } + + public void setDefaultTldScan(Boolean defaultTldScan) { + this.defaultTldScan = defaultTldScan; + } + protected void initContext(JspCContextAccessor topJspC) { this.context = topJspC.context; scanner = topJspC.scanner; @@ -65,4 +87,25 @@ protected void initContext(JspCContextAccessor topJspC) { public String getCompilerClassName() { return getcompilerClass(); } + + @Override + protected void initTldScanner(JspCServletContext context, ClassLoader classLoader) { + if (tldSkip != null || tldScan != null || defaultTldScan != null) { + JarScanner scanner = new StandardJarScanner(); + StandardJarScanFilter filter = new StandardJarScanFilter(); + if (tldSkip != null) { + filter.setTldSkip(tldSkip); + } + if (tldScan != null) { + filter.setTldScan(tldScan); + } + if (defaultTldScan != null) { + filter.setDefaultTldScan(defaultTldScan); + } + scanner.setJarScanFilter(filter); + // As seen in org.apache.jasper.compiler.JarScannerFactory.getJarScanner + context.setAttribute(JarScanner.class.getName(), scanner); + } + super.initTldScanner(context, classLoader); + } } diff --git a/src/main/java/io/leonard/maven/plugins/jspc/JspcMojo.java b/src/main/java/io/leonard/maven/plugins/jspc/JspcMojo.java index 7a44b01..972102c 100755 --- a/src/main/java/io/leonard/maven/plugins/jspc/JspcMojo.java +++ b/src/main/java/io/leonard/maven/plugins/jspc/JspcMojo.java @@ -39,6 +39,9 @@ import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; +import java.util.logging.Handler; +import java.util.logging.LogManager; +import java.util.logging.Logger; import javax.xml.XMLConstants; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; @@ -63,6 +66,7 @@ import org.codehaus.plexus.util.DirectoryScanner; import org.codehaus.plexus.util.StringUtils; import org.eclipse.jdt.internal.compiler.env.NameEnvironmentAnswer; +import org.slf4j.bridge.SLF4JBridgeHandler; import org.xml.sax.SAXException; /** @@ -283,7 +287,7 @@ public class JspcMojo extends AbstractMojo { * trimmed? */ @Parameter(defaultValue = "false") - private boolean trimSpaces; + private String trimSpaces; /** * Should text strings be generated as char arrays, to improve performance in @@ -319,8 +323,40 @@ public class JspcMojo extends AbstractMojo { @Parameter(defaultValue = "false", property = "jspc.skip") private boolean skip; + /** + * The comma separated list of JAR file name patterns to skip when scanning for tag libraries (TLDs). + * See {@link org.apache.tomcat.util.scan.StandardJarScanFilter StandardJarScanFilter}. + * + * @see Standard implementation + * + */ + @Parameter + private String tldSkip; + + /** + * The comma separated list of JAR file name patterns to scan when scanning for tag libraries (TLDs). + * See {@link org.apache.tomcat.util.scan.StandardJarScanFilter StandardJarScanFilter}. + * + * @see Standard implementation + * + */ + @Parameter + private String tldScan; + + /** + * Controls if JARs are scanned or skipped by default when scanning for TLDs. + * See {@link org.apache.tomcat.util.scan.StandardJarScanFilter StandardJarScanFilter}. + * + * @see Standard implementation + * + */ + @Parameter + private Boolean defaultTldScan; + private Map resourcesCache = new ConcurrentHashMap<>(); + private Handler[] handlers; + @Override public void execute() throws MojoExecutionException, MojoFailureException { if (getLog().isDebugEnabled()) { @@ -356,6 +392,7 @@ public void execute() throws MojoExecutionException, MojoFailureException { try { long start = System.currentTimeMillis(); + installLogHandler(); prepare(); compile(); cleanupSrcs(); @@ -370,6 +407,8 @@ public void execute() throws MojoExecutionException, MojoFailureException { getLog().info("Compilation completed in " + time); } catch (Exception e) { throw new MojoExecutionException("Failure processing jsps", e); + } finally { + uninstallLogHandler(); } } @@ -457,13 +496,16 @@ private JspCContextAccessor initJspc(StringBuilder classpathStr, int threadIndex jspc.setJavaEncoding(javaEncoding); jspc.setFailOnError(stopAtFirstError); jspc.setPoolingEnabled(enableJspTagPooling); - jspc.setTrimSpaces(trimSpaces ? TrimSpacesOption.TRUE : TrimSpacesOption.FALSE); + jspc.setTrimSpaces(TrimSpacesOption.valueOf(trimSpaces.toUpperCase())); jspc.setGenStringAsCharArray(genStringAsCharArray); jspc.setCompilerSourceVM(compilerVersion); jspc.setCompilerTargetVM(compilerVersion); jspc.setcompilerClass(compilerClass); jspc.setResourcesCache(resourcesCache); jspc.setStrictQuoteEscaping(strictQuoteEscaping); + jspc.setTldSkip(tldSkip); + jspc.setTldScan(tldScan); + jspc.setDefaultTldScan(defaultTldScan); if (topJspC == null) { jspc.initClassLoader(); jspc.initServletContext(); @@ -699,6 +741,20 @@ private void writeXmlFragments(Path mergedWebXmlPath) throws IOException { } } + private void installLogHandler() { + handlers = LogManager.getLogManager().getLogger("").getHandlers(); + SLF4JBridgeHandler.removeHandlersForRootLogger(); + SLF4JBridgeHandler.install(); + } + + private void uninstallLogHandler() { + SLF4JBridgeHandler.uninstall(); + Logger rootLogger = LogManager.getLogManager().getLogger(""); + for (Handler handler : handlers) { + rootLogger.addHandler(handler); + } + } + private void prepare() { // For some reason JspC doesn't like it if the dir doesn't // already exist and refuses to create the web.xml fragment @@ -770,7 +826,7 @@ private File getWebXmlFile() throws IOException { * don't add suffix to maintain the same behavior as in the mode without * multithreading. * - * @param threadNumber the index of current thread + * @param threadIndex the index of current thread * @return web xml fragment filename with thread index */ private String getwebXmlFragmentFilename(int threadIndex) {