📄 jspcmojo.java
字号:
.getContextClassLoader(); ArrayList urls = new ArrayList(); setUpClassPath(urls); URLClassLoader ucl = new URLClassLoader((URL[]) urls.toArray(new URL[0]), currentClassLoader); StringBuffer classpathStr = new StringBuffer(); for (int i = 0; i < urls.size(); i++) { if (getLog().isDebugEnabled()) getLog().debug("webappclassloader contains: " + urls.get(i)); classpathStr.append(((URL) urls.get(i)).getFile()); if (getLog().isDebugEnabled()) getLog().debug( "added to classpath: " + ((URL) urls.get(i)).getFile()); classpathStr.append(System.getProperty("path.separator")); } Thread.currentThread().setContextClassLoader(ucl); JspC jspc = new JspC(); jspc.setWebXmlFragment(webXmlFragment); jspc.setUriroot(webAppSourceDirectory); jspc.setPackage(packageRoot); jspc.setOutputDir(generatedClasses); jspc.setValidateXml(validateXml); jspc.setClassPath(classpathStr.toString()); jspc.setCompile(true); jspc.setSmapSuppressed(suppressSmap); jspc.setSmapDumped(!suppressSmap); jspc.setJavaEncoding(javaEncoding); // JspC#setExtensions() does not exist, so // always set concrete list of files that will be processed. String jspFiles = getJspFiles(webAppSourceDirectory); System.err.println("Compiling "+jspFiles); System.err.println("Includes="+includes); System.err.println("Excludes="+excludes); jspc.setJspFiles(jspFiles); if (verbose) { getLog().info("Files selected to precompile: " + jspFiles); } try { jspc.setIgnoreJspFragmentErrors(ignoreJspFragmentErrors); } catch (NoSuchMethodError e) { getLog().debug("Tomcat Jasper does not support configuration option 'ignoreJspFragmentErrors': ignored"); } try { if (schemaResourcePrefix != null) jspc.setSchemaResourcePrefix(schemaResourcePrefix); } catch (NoSuchMethodError e) { getLog().debug("Tomcat Jasper does not support configuration option 'schemaResourcePrefix': ignored"); } if (verbose) jspc.setVerbose(99); else jspc.setVerbose(0); jspc.execute(); Thread.currentThread().setContextClassLoader(currentClassLoader); } private String getJspFiles(String webAppSourceDirectory) throws Exception { List fileNames = FileUtils.getFileNames(new File(webAppSourceDirectory),includes, excludes, false); return StringUtils.join(fileNames.toArray(new String[0]), ","); } /** * Until Jasper supports the option to generate the srcs in a different dir * than the classes, this is the best we can do. * * @throws Exception */ public void cleanupSrcs() throws Exception { // delete the .java files - depending on keepGenerated setting if (!keepSources) { String packageRootDirectory = packageRoot.replace('.', File.separatorChar); File generatedClassesDir = new File(generatedClasses + File.separatorChar + packageRootDirectory); File[] srcFiles = generatedClassesDir .listFiles(new FilenameFilter() { public boolean accept(File dir, String name) { if (name == null) return false; if (name.trim().equals("")) return false; if (name.endsWith(".java")) return true; return false; } }); for (int i = 0; (srcFiles != null) && (i < srcFiles.length); i++) { srcFiles[i].delete(); } } } /** * Take the web fragment and put it inside a copy of the web.xml file from * the webAppSourceDirectory. * * You can specify the insertion point by specifying the string in the * insertionMarker configuration entry. * * If you dont specify the insertionMarker, then the fragment will be * inserted at the end of the file just before the </webapp> * * @throws Exception */ public void mergeWebXml() throws Exception { if (mergeFragment) { // open the src web.xml File webXml = new File(webAppSourceDirectory + "/WEB-INF/web.xml"); if (!webXml.exists()) { getLog() .info( webAppSourceDirectory + "/WEB-INF/web.xml does not exist, cannot merge with generated fragment"); return; } File fragmentWebXml = new File(webXmlFragment); if (!fragmentWebXml.exists()) { getLog().info("No fragment web.xml file generated"); } File mergedWebXml = new File(fragmentWebXml.getParentFile(), "web.xml"); BufferedReader webXmlReader = new BufferedReader(new FileReader( webXml)); PrintWriter mergedWebXmlWriter = new PrintWriter(new FileWriter( mergedWebXml)); // read up to the insertion marker or the </webapp> if there is no // marker boolean atInsertPoint = false; boolean atEOF = false; String marker = (insertionMarker == null || insertionMarker.equals("") ? END_OF_WEBAPP : insertionMarker); while (!atInsertPoint && !atEOF) { String line = webXmlReader.readLine(); if (line == null) atEOF = true; else if (line.indexOf(marker) >= 0) { atInsertPoint = true; } else { mergedWebXmlWriter.println(line); } } // put in the generated fragment BufferedReader fragmentWebXmlReader = new BufferedReader( new FileReader(fragmentWebXml)); IO.copy(fragmentWebXmlReader, mergedWebXmlWriter); // if we inserted just before the </web-app>, put it back in if (marker.equals(END_OF_WEBAPP)) mergedWebXmlWriter.println(END_OF_WEBAPP); // copy in the rest of the original web.xml file IO.copy(webXmlReader, mergedWebXmlWriter); webXmlReader.close(); mergedWebXmlWriter.close(); fragmentWebXmlReader.close(); } } private void prepare() throws Exception { // For some reason JspC doesn't like it if the dir doesn't // already exist and refuses to create the web.xml fragment File generatedSourceDirectoryFile = new File(generatedClasses); if (!generatedSourceDirectoryFile.exists()) generatedSourceDirectoryFile.mkdirs(); } /** * Set up the execution classpath for Jasper. * * Put everything in the classesDirectory and all of the dependencies on the * classpath. * * @param urls a list to which to add the urls of the dependencies * @throws Exception */ private void setUpClassPath(List urls) throws Exception { String classesDir = classesDirectory.getCanonicalPath(); classesDir = classesDir + (classesDir.endsWith(File.pathSeparator) ? "" : File.separator); urls.add(new File(classesDir).toURL()); if (getLog().isDebugEnabled()) getLog().debug("Adding to classpath classes dir: " + classesDir); for (Iterator iter = project.getArtifacts().iterator(); iter.hasNext();) { Artifact artifact = (Artifact) iter.next(); // Include runtime and compile time libraries if (!Artifact.SCOPE_TEST.equals(artifact.getScope())) { String filePath = artifact.getFile().getCanonicalPath(); if (getLog().isDebugEnabled()) getLog().debug( "Adding to classpath dependency file: " + filePath); urls.add(artifact.getFile().toURL()); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -