📄 abstractjettyrunmojo.java
字号:
//allow a webapp with no classes in it (just jsps/html) if (getClassesDirectory() != null) { if (!getClassesDirectory().exists()) getLog().info( "Classes directory "+ getClassesDirectory().getCanonicalPath()+ " does not exist"); else getLog().info("Classes = " + getClassesDirectory().getCanonicalPath()); } else getLog().info("Classes directory not set"); } catch (IOException e) { throw new MojoExecutionException("Location of classesDirectory does not exist"); } setExtraScanTargets(new ArrayList()); if (scanTargets != null) { for (int i=0; i< scanTargets.length; i++) { getLog().info("Added extra scan target:"+ scanTargets[i]); getExtraScanTargets().add(scanTargets[i]); } } if (scanTargetPatterns!=null) { for (int i=0;i<scanTargetPatterns.length; i++) { Iterator itor = scanTargetPatterns[i].getIncludes().iterator(); StringBuffer strbuff = new StringBuffer(); while (itor.hasNext()) { strbuff.append((String)itor.next()); if (itor.hasNext()) strbuff.append(","); } String includes = strbuff.toString(); itor = scanTargetPatterns[i].getExcludes().iterator(); strbuff= new StringBuffer(); while (itor.hasNext()) { strbuff.append((String)itor.next()); if (itor.hasNext()) strbuff.append(","); } String excludes = strbuff.toString(); try { List files = FileUtils.getFiles(scanTargetPatterns[i].getDirectory(), includes, excludes); itor = files.iterator(); while (itor.hasNext()) getLog().info("Adding extra scan target from pattern: "+itor.next()); List currentTargets = getExtraScanTargets(); if(currentTargets!=null && !currentTargets.isEmpty()) currentTargets.addAll(files); else setExtraScanTargets(files); } catch (IOException e) { throw new MojoExecutionException(e.getMessage()); } } } } public void configureWebApplication() throws Exception { super.configureWebApplication(); setClassPathFiles(setUpClassPath()); if(webAppConfig.getWebXmlFile()==null) webAppConfig.setWebXmlFile(getWebXmlFile()); if(webAppConfig.getJettyEnvXmlFile()==null) webAppConfig.setJettyEnvXmlFile(getJettyEnvXmlFile()); if(webAppConfig.getClassPathFiles()==null) webAppConfig.setClassPathFiles(getClassPathFiles()); if(webAppConfig.getWar()==null) webAppConfig.setWar(getWebAppSourceDirectory().getCanonicalPath()); getLog().info("Webapp directory = " + getWebAppSourceDirectory().getCanonicalPath()); webAppConfig.configure(); } public void configureScanner () { // start the scanner thread (if necessary) on the main webapp final ArrayList scanList = new ArrayList(); scanList.add(getWebXmlFile()); if (getJettyEnvXmlFile() != null) scanList.add(getJettyEnvXmlFile()); File jettyWebXmlFile = findJettyWebXmlFile(new File(getWebAppSourceDirectory(),"WEB-INF")); if (jettyWebXmlFile != null) scanList.add(jettyWebXmlFile); scanList.addAll(getExtraScanTargets()); scanList.add(getProject().getFile()); scanList.addAll(getClassPathFiles()); setScanList(scanList); ArrayList listeners = new ArrayList(); listeners.add(new Scanner.BulkListener() { public void filesChanged (List changes) { try { boolean reconfigure = changes.contains(getProject().getFile().getCanonicalPath()); restartWebApp(reconfigure); } catch (Exception e) { getLog().error("Error reconfiguring/restarting webapp after change in watched files",e); } } }); setScannerListeners(listeners); } public void restartWebApp(boolean reconfigureScanner) throws Exception { getLog().info("restarting "+webAppConfig); getLog().debug("Stopping webapp ..."); webAppConfig.stop(); getLog().debug("Reconfiguring webapp ..."); checkPomConfiguration(); configureWebApplication(); // check if we need to reconfigure the scanner, // which is if the pom changes if (reconfigureScanner) { getLog().info("Reconfiguring scanner after change to pom.xml ..."); scanList.clear(); scanList.add(getWebXmlFile()); if (getJettyEnvXmlFile() != null) scanList.add(getJettyEnvXmlFile()); scanList.addAll(getExtraScanTargets()); scanList.add(getProject().getFile()); scanList.addAll(getClassPathFiles()); getScanner().setScanDirs(scanList); } getLog().debug("Restarting webapp ..."); webAppConfig.start(); getLog().info("Restart completed at "+new Date().toString()); } private List getDependencyFiles () { List dependencyFiles = new ArrayList(); List overlays = new ArrayList(); for ( Iterator iter = getProject().getArtifacts().iterator(); iter.hasNext(); ) { Artifact artifact = (Artifact) iter.next(); // Include runtime and compile time libraries, and possibly test libs too if(artifact.getType().equals("war")) { try { Resource r = Resource.newResource("jar:" + artifact.getFile().toURL().toString() + "!/"); overlays.add(r); getExtraScanTargets().add(artifact.getFile()); } catch(Exception e) { throw new RuntimeException(e); } continue; } if (((!Artifact.SCOPE_PROVIDED.equals(artifact.getScope())) && (!Artifact.SCOPE_TEST.equals( artifact.getScope()))) || (useTestClasspath && Artifact.SCOPE_TEST.equals( artifact.getScope()))) { dependencyFiles.add(artifact.getFile()); getLog().debug( "Adding artifact " + artifact.getFile().getName() + " for WEB-INF/lib " ); } } if(!overlays.isEmpty()) { try { Resource resource = webAppConfig.getBaseResource(); ResourceCollection rc = new ResourceCollection(); if(resource==null) { // nothing configured, so we automagically enable the overlays int size = overlays.size()+1; Resource[] resources = new Resource[size]; resources[0] = Resource.newResource(getWebAppSourceDirectory().toURL()); for(int i=1; i<size; i++) { resources[i] = (Resource)overlays.get(i-1); getLog().info("Adding overlay: " + resources[i]); } rc.setResources(resources); } else { if(resource instanceof ResourceCollection) { // there was a preconfigured ResourceCollection ... append the artifact wars Resource[] old = ((ResourceCollection)resource).getResources(); int size = old.length + overlays.size(); Resource[] resources = new Resource[size]; System.arraycopy(old, 0, resources, 0, old.length); for(int i=old.length,j=0; i<size; i++,j++) { resources[i] = (Resource)overlays.get(j); getLog().info("Adding overlay: " + resources[i]); } rc.setResources(resources); } else { // baseResource was already configured w/c could be src/main/webapp if(!resource.isDirectory() && String.valueOf(resource.getFile()).endsWith(".war")) { // its a war resource = Resource.newResource("jar:" + resource.getURL().toString() + "!/"); } int size = overlays.size()+1; Resource[] resources = new Resource[size]; resources[0] = resource; for(int i=1; i<size; i++) { resources[i] = (Resource)overlays.get(i-1); getLog().info("Adding overlay: " + resources[i]); } rc.setResources(resources); } } webAppConfig.setBaseResource(rc); } catch(Exception e) { throw new RuntimeException(e); } } return dependencyFiles; } private List setUpClassPath() { List classPathFiles = new ArrayList(); //if using the test classes, make sure they are first //on the list if (useTestClasspath && (testClassesDirectory != null)) classPathFiles.add(testClassesDirectory); if (getClassesDirectory() != null) classPathFiles.add(getClassesDirectory()); //now add all of the dependencies classPathFiles.addAll(getDependencyFiles()); if (getLog().isDebugEnabled()) { for (int i = 0; i < classPathFiles.size(); i++) { getLog().debug("classpath element: "+ ((File) classPathFiles.get(i)).getName()); } } return classPathFiles; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -