@Override public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException { EnvVars env = build.getEnvironment(listener); env.overrideAll(build.getBuildVariables()); EnvVariableResolver helper = new EnvVariableResolver(build, listener); boolean success = true; System.out.println("Loading Blueprint !"); int counter = 1; for (BlueprintParam param : params) { System.out.println("Creating package from directory : " + param.getBlueprintPath()); // Resolve any environment variables in the parameters BlueprintParam fparam = new BlueprintParam( helper.replaceBuildParamWithValue(param.getServerUrl()), helper.replaceBuildParamWithValue(param.getUserName()), helper.replaceBuildParamWithValue(param.getPassword()), helper.replaceBuildParamWithValue(param.getTenant()), param.getPackageBlueprint(), env.get("WORKSPACE") + "/" + param.getBlueprintPath(), param.getOverWrite(), param.getPublishBlueprint(), param.getServiceCategory()); final Blueprint blueprint = newBlueprint(listener.getLogger(), fparam); try { if (blueprint.Create()) { this.blueprintList.add(blueprint); } else { build.setResult(Result.FAILURE); success = false; break; } } catch (ArchiveException e) { e.printStackTrace(); } } return success; }
public boolean accept(Path file) { try { FileSystem fs = file.getFileSystem(conf); boolean unpack = conf.getBoolean(unpackParamName, true); if (defaultIgnores.accept(file) && fs.getFileStatus(file).isDir() == false) { String URI = file.toUri().toString(); // detect whether a file is likely to be an archive // TODO extend to other known types if (unpack && URI.toLowerCase().endsWith(".zip")) { FSDataInputStream fis = null; try { fis = fs.open(file); ArchiveInputStream input = new ArchiveStreamFactory().createArchiveInputStream(new BufferedInputStream(fis)); ArchiveEntry entry = null; while ((entry = input.getNextEntry()) != null) { String name = entry.getName(); long size = entry.getSize(); byte[] content = new byte[(int) size]; input.read(content); key.set(name); // fill the values for the content object value.setUrl(name); value.setContent(content); writer.append(key, value); counter++; if (reporter != null) { reporter.incrCounter(Counters.DOC_COUNT, 1); } } } catch (ArchiveException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { fis.close(); } } else { // Hmm, kind of dangerous to do this byte[] fileBArray = new byte[(int) fs.getFileStatus(file).getLen()]; FSDataInputStream fis = null; try { fis = fs.open(file); fis.readFully(0, fileBArray); fis.close(); key.set(URI); // fill the values for the content object value.setUrl(URI); value.setContent(fileBArray); writer.append(key, value); counter++; if (reporter != null) { reporter.incrCounter(Counters.DOC_COUNT, 1); } } catch (FileNotFoundException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } } } // if it is a directory, accept it so we can possibly recurse on // it, // otherwise we don't care about actually accepting the file, // since // all the work is done in the accept method here. return fs.getFileStatus(file).isDir(); } catch (IOException e) { log.error("Exception", e); } return false; }
public void execute() throws MojoExecutionException, MojoFailureException { if (this.skip) { getLog().info("skip execution"); return; } // project.addAttachedArtifact( ); File warExecFile = new File(buildDirectory, finalName); if (warExecFile.exists()) { warExecFile.delete(); } File execWarJar = new File(buildDirectory, finalName); FileOutputStream execWarJarOutputStream = null; ArchiveOutputStream os = null; File tmpPropertiesFile = null; File tmpManifestFile = null; FileOutputStream tmpPropertiesFileOutputStream = null; PrintWriter tmpManifestWriter = null; try { tmpPropertiesFile = new File(buildDirectory, "war-exec.properties"); if (tmpPropertiesFile.exists()) { tmpPropertiesFile.delete(); } tmpPropertiesFile.getParentFile().mkdirs(); tmpManifestFile = new File(buildDirectory, "war-exec.manifest"); if (tmpManifestFile.exists()) { tmpManifestFile.delete(); } tmpPropertiesFileOutputStream = new FileOutputStream(tmpPropertiesFile); execWarJar.getParentFile().mkdirs(); execWarJar.createNewFile(); execWarJarOutputStream = new FileOutputStream(execWarJar); tmpManifestWriter = new PrintWriter(tmpManifestFile); // store : // * wars in the root: foo.war // * tomcat jars // * file tomcat.standalone.properties with possible values : // * useServerXml=true/false to use directly the one provided // * enableNaming=true/false // * wars=foo.war|contextpath;bar.war ( |contextpath is optionnal if empty use the war name // ) // * accessLogValveFormat= // * connectorhttpProtocol: HTTP/1.1 or org.apache.coyote.http11.Http11NioProtocol // * optionnal: conf/ with usual tomcat configuration files // * MANIFEST with Main-Class Properties properties = new Properties(); properties.put( Tomcat7Runner.ARCHIVE_GENERATION_TIMESTAMP_KEY, Long.toString(System.currentTimeMillis())); properties.put(Tomcat7Runner.ENABLE_NAMING_KEY, Boolean.toString(enableNaming)); properties.put(Tomcat7Runner.ACCESS_LOG_VALVE_FORMAT_KEY, accessLogValveFormat); properties.put(Tomcat7Runner.HTTP_PROTOCOL_KEY, connectorHttpProtocol); os = new ArchiveStreamFactory() .createArchiveOutputStream(ArchiveStreamFactory.JAR, execWarJarOutputStream); if ("war".equals(project.getPackaging())) { os.putArchiveEntry(new JarArchiveEntry(StringUtils.removeStart(path, "/") + ".war")); IOUtils.copy(new FileInputStream(projectArtifact.getFile()), os); os.closeArchiveEntry(); properties.put(Tomcat7Runner.WARS_KEY, StringUtils.removeStart(path, "/") + ".war|" + path); } else if (warRunDependencies != null && !warRunDependencies.isEmpty()) { for (WarRunDependency warRunDependency : warRunDependencies) { if (warRunDependency.dependency != null) { Dependency dependency = warRunDependency.dependency; Artifact artifact = artifactFactory.createArtifactWithClassifier( dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion(), dependency.getType(), dependency.getClassifier()); artifactResolver.resolve(artifact, this.remoteRepos, this.local); File warFileToBundle = new File(resolvePluginWorkDir(), artifact.getFile().getName()); FileUtils.copyFile(artifact.getFile(), warFileToBundle); if (warRunDependency.contextXml != null) { warFileToBundle = addContextXmlToWar(warRunDependency.contextXml, warFileToBundle); } final String warFileName = artifact.getFile().getName(); os.putArchiveEntry(new JarArchiveEntry(warFileName)); IOUtils.copy(new FileInputStream(warFileToBundle), os); os.closeArchiveEntry(); String propertyWarValue = properties.getProperty(Tomcat7Runner.WARS_KEY); String contextPath = StringUtils.isEmpty(warRunDependency.contextPath) ? "/" : warRunDependency.contextPath; if (propertyWarValue != null) { properties.put( Tomcat7Runner.WARS_KEY, propertyWarValue + ";" + warFileName + "|" + contextPath); } else { properties.put(Tomcat7Runner.WARS_KEY, warFileName + "|" + contextPath); } } } } if (serverXml != null && serverXml.exists()) { os.putArchiveEntry(new JarArchiveEntry("conf/server.xml")); IOUtils.copy(new FileInputStream(serverXml), os); os.closeArchiveEntry(); properties.put(Tomcat7Runner.USE_SERVER_XML_KEY, Boolean.TRUE.toString()); } else { properties.put(Tomcat7Runner.USE_SERVER_XML_KEY, Boolean.FALSE.toString()); } os.putArchiveEntry(new JarArchiveEntry("conf/web.xml")); IOUtils.copy(getClass().getResourceAsStream("/conf/web.xml"), os); os.closeArchiveEntry(); properties.store(tmpPropertiesFileOutputStream, "created by Apache Tomcat Maven plugin"); tmpPropertiesFileOutputStream.flush(); tmpPropertiesFileOutputStream.close(); os.putArchiveEntry(new JarArchiveEntry(Tomcat7RunnerCli.STAND_ALONE_PROPERTIES_FILENAME)); IOUtils.copy(new FileInputStream(tmpPropertiesFile), os); os.closeArchiveEntry(); // add tomcat classes for (Artifact pluginArtifact : pluginArtifacts) { if (StringUtils.equals("org.apache.tomcat", pluginArtifact.getGroupId()) || StringUtils.equals("org.apache.tomcat.embed", pluginArtifact.getGroupId()) || StringUtils.equals("org.eclipse.jdt.core.compiler", pluginArtifact.getGroupId()) || StringUtils.equals("commons-cli", pluginArtifact.getArtifactId()) || StringUtils.equals("tomcat7-war-runner", pluginArtifact.getArtifactId())) { JarFile jarFile = new JarFile(pluginArtifact.getFile()); extractJarToArchive(jarFile, os); } } // add extra dependencies if (extraDependencies != null && !extraDependencies.isEmpty()) { for (Dependency dependency : extraDependencies) { // String groupId, String artifactId, String version, String scope, String type Artifact artifact = artifactFactory.createArtifact( dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion(), dependency.getScope(), dependency.getType()); artifactResolver.resolve(artifact, this.remoteRepos, this.local); JarFile jarFile = new JarFile(artifact.getFile()); extractJarToArchive(jarFile, os); } } Manifest manifest = new Manifest(); Manifest.Attribute mainClassAtt = new Manifest.Attribute(); mainClassAtt.setName("Main-Class"); mainClassAtt.setValue(mainClass); manifest.addConfiguredAttribute(mainClassAtt); manifest.write(tmpManifestWriter); tmpManifestWriter.flush(); tmpManifestWriter.close(); os.putArchiveEntry(new JarArchiveEntry("META-INF/MANIFEST.MF")); IOUtils.copy(new FileInputStream(tmpManifestFile), os); os.closeArchiveEntry(); if (attachArtifact) { // MavenProject project, String artifactType, String artifactClassifier, File artifactFile projectHelper.attachArtifact( project, attachArtifactClassifierType, attachArtifactClassifier, execWarJar); } if (extraResources != null) { for (ExtraResource extraResource : extraResources) { DirectoryScanner directoryScanner = new DirectoryScanner(); directoryScanner.setBasedir(extraResource.getDirectory()); directoryScanner.addDefaultExcludes(); directoryScanner.setExcludes(toStringArray(extraResource.getExcludes())); directoryScanner.setIncludes(toStringArray(extraResource.getIncludes())); directoryScanner.scan(); for (String includeFile : directoryScanner.getIncludedFiles()) { getLog().debug("include file:" + includeFile); os.putArchiveEntry(new JarArchiveEntry(includeFile)); IOUtils.copy( new FileInputStream(new File(extraResource.getDirectory(), includeFile)), os); os.closeArchiveEntry(); } } } if (tomcatConfigurationFilesDirectory != null && tomcatConfigurationFilesDirectory.exists()) { // Because its the tomcat default dir for configs String aConfigOutputDir = "conf/"; copyDirectoryContentIntoArchive(tomcatConfigurationFilesDirectory, aConfigOutputDir, os); } } catch (ManifestException e) { throw new MojoExecutionException(e.getMessage(), e); } catch (IOException e) { throw new MojoExecutionException(e.getMessage(), e); } catch (ArchiveException e) { throw new MojoExecutionException(e.getMessage(), e); } catch (ArtifactNotFoundException e) { throw new MojoExecutionException(e.getMessage(), e); } catch (ArtifactResolutionException e) { throw new MojoExecutionException(e.getMessage(), e); } finally { IOUtils.closeQuietly(os); IOUtils.closeQuietly(tmpManifestWriter); IOUtils.closeQuietly(execWarJarOutputStream); IOUtils.closeQuietly(tmpPropertiesFileOutputStream); } }