Thomas Vachuska | 8c8b037 | 2015-03-10 11:11:24 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.maven; |
| 17 | |
Thomas Vachuska | 586afd8 | 2015-04-17 11:06:53 -0700 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
Thomas Vachuska | a98ae7f | 2015-04-14 14:00:36 -0700 | [diff] [blame] | 19 | import org.apache.commons.configuration.ConfigurationException; |
| 20 | import org.apache.commons.configuration.XMLConfiguration; |
Brian O'Connor | bf7eca5 | 2015-04-15 16:25:46 -0700 | [diff] [blame] | 21 | import org.apache.maven.artifact.repository.ArtifactRepository; |
Thomas Vachuska | 8c8b037 | 2015-03-10 11:11:24 -0700 | [diff] [blame] | 22 | import org.apache.maven.plugin.AbstractMojo; |
| 23 | import org.apache.maven.plugin.MojoExecutionException; |
Thomas Vachuska | a98ae7f | 2015-04-14 14:00:36 -0700 | [diff] [blame] | 24 | import org.apache.maven.plugins.annotations.Component; |
Thomas Vachuska | 8c8b037 | 2015-03-10 11:11:24 -0700 | [diff] [blame] | 25 | import org.apache.maven.plugins.annotations.LifecyclePhase; |
| 26 | import org.apache.maven.plugins.annotations.Mojo; |
| 27 | import org.apache.maven.plugins.annotations.Parameter; |
Thomas Vachuska | a98ae7f | 2015-04-14 14:00:36 -0700 | [diff] [blame] | 28 | import org.apache.maven.project.MavenProject; |
| 29 | import org.apache.maven.project.MavenProjectHelper; |
Thomas Vachuska | 8c8b037 | 2015-03-10 11:11:24 -0700 | [diff] [blame] | 30 | |
Thomas Vachuska | a98ae7f | 2015-04-14 14:00:36 -0700 | [diff] [blame] | 31 | import java.io.File; |
| 32 | import java.io.FileInputStream; |
| 33 | import java.io.FileNotFoundException; |
| 34 | import java.io.FileOutputStream; |
| 35 | import java.io.IOException; |
| 36 | import java.io.InputStream; |
Thomas Vachuska | 8c8b037 | 2015-03-10 11:11:24 -0700 | [diff] [blame] | 37 | import java.util.List; |
Thomas Vachuska | a98ae7f | 2015-04-14 14:00:36 -0700 | [diff] [blame] | 38 | import java.util.stream.Collectors; |
| 39 | import java.util.zip.ZipEntry; |
| 40 | import java.util.zip.ZipOutputStream; |
| 41 | |
Thomas Vachuska | 586afd8 | 2015-04-17 11:06:53 -0700 | [diff] [blame] | 42 | import static com.google.common.io.ByteStreams.toByteArray; |
Thomas Vachuska | a98ae7f | 2015-04-14 14:00:36 -0700 | [diff] [blame] | 43 | import static org.codehaus.plexus.util.FileUtils.*; |
Thomas Vachuska | 8c8b037 | 2015-03-10 11:11:24 -0700 | [diff] [blame] | 44 | |
| 45 | /** |
Thomas Vachuska | a98ae7f | 2015-04-14 14:00:36 -0700 | [diff] [blame] | 46 | * Produces ONOS application archive using the app.xml file information. |
Thomas Vachuska | 8c8b037 | 2015-03-10 11:11:24 -0700 | [diff] [blame] | 47 | */ |
| 48 | @Mojo(name = "app", defaultPhase = LifecyclePhase.PACKAGE) |
| 49 | public class OnosAppMojo extends AbstractMojo { |
| 50 | |
Thomas Vachuska | a98ae7f | 2015-04-14 14:00:36 -0700 | [diff] [blame] | 51 | private static final String APP = "app"; |
| 52 | private static final String NAME = "[@name]"; |
| 53 | private static final String VERSION = "[@version]"; |
| 54 | private static final String FEATURES_REPO = "[@featuresRepo]"; |
Thomas Vachuska | a98ae7f | 2015-04-14 14:00:36 -0700 | [diff] [blame] | 55 | private static final String ARTIFACT = "artifact"; |
Thomas Vachuska | 8c8b037 | 2015-03-10 11:11:24 -0700 | [diff] [blame] | 56 | |
Thomas Vachuska | a98ae7f | 2015-04-14 14:00:36 -0700 | [diff] [blame] | 57 | private static final String APP_XML = "app.xml"; |
| 58 | private static final String FEATURES_XML = "features.xml"; |
Thomas Vachuska | 8c8b037 | 2015-03-10 11:11:24 -0700 | [diff] [blame] | 59 | |
Thomas Vachuska | a98ae7f | 2015-04-14 14:00:36 -0700 | [diff] [blame] | 60 | private static final String MVN_URL = "mvn:"; |
Thomas Vachuska | a98ae7f | 2015-04-14 14:00:36 -0700 | [diff] [blame] | 61 | private static final String M2_PREFIX = "m2"; |
Thomas Vachuska | 8c8b037 | 2015-03-10 11:11:24 -0700 | [diff] [blame] | 62 | |
Thomas Vachuska | 586afd8 | 2015-04-17 11:06:53 -0700 | [diff] [blame] | 63 | private static final String ONOS_APP_NAME = "onos.app.name"; |
| 64 | private static final String ONOS_APP_ORIGIN = "onos.app.origin"; |
| 65 | |
Thomas Vachuska | a98ae7f | 2015-04-14 14:00:36 -0700 | [diff] [blame] | 66 | private static final String JAR = "jar"; |
| 67 | private static final String XML = "xml"; |
| 68 | private static final String APP_ZIP = "oar"; |
| 69 | private static final String PACKAGE_DIR = "oar"; |
Thomas Vachuska | 8c8b037 | 2015-03-10 11:11:24 -0700 | [diff] [blame] | 70 | |
Thomas Vachuska | 586afd8 | 2015-04-17 11:06:53 -0700 | [diff] [blame] | 71 | private static final String DEFAULT_ORIGIN = "ON.Lab"; |
Thomas Vachuska | 41e8c18 | 2015-04-21 11:09:19 -0700 | [diff] [blame] | 72 | private static final String DEFAULT_VERSION = "${project.version}"; |
Thomas Vachuska | 586afd8 | 2015-04-17 11:06:53 -0700 | [diff] [blame] | 73 | |
| 74 | private static final String DEFAULT_FEATURES_REPO = |
| 75 | "mvn:${project.groupId}/${project.artifactId}/${project.version}/xml/features"; |
| 76 | private static final String DEFAULT_ARTIFACT = |
| 77 | "mvn:${project.groupId}/${project.artifactId}/${project.version}"; |
| 78 | |
Thomas Vachuska | 41e8c18 | 2015-04-21 11:09:19 -0700 | [diff] [blame] | 79 | private static final int BUFFER_SIZE = 8192; |
Thomas Vachuska | 8c8b037 | 2015-03-10 11:11:24 -0700 | [diff] [blame] | 80 | |
Thomas Vachuska | 586afd8 | 2015-04-17 11:06:53 -0700 | [diff] [blame] | 81 | private String name; |
| 82 | private String origin; |
| 83 | private String version = DEFAULT_VERSION; |
Thomas Vachuska | 586afd8 | 2015-04-17 11:06:53 -0700 | [diff] [blame] | 84 | private String featuresRepo = DEFAULT_FEATURES_REPO; |
Thomas Vachuska | 8c8b037 | 2015-03-10 11:11:24 -0700 | [diff] [blame] | 85 | private List<String> artifacts; |
| 86 | |
Thomas Vachuska | a98ae7f | 2015-04-14 14:00:36 -0700 | [diff] [blame] | 87 | /** |
| 88 | * The project base directory. |
| 89 | */ |
| 90 | @Parameter(defaultValue = "${basedir}") |
| 91 | protected File baseDir; |
Thomas Vachuska | 8c8b037 | 2015-03-10 11:11:24 -0700 | [diff] [blame] | 92 | |
Thomas Vachuska | a98ae7f | 2015-04-14 14:00:36 -0700 | [diff] [blame] | 93 | /** |
| 94 | * The directory where the generated catalogue file will be put. |
| 95 | */ |
| 96 | @Parameter(defaultValue = "${project.build.directory}") |
| 97 | protected File dstDirectory; |
| 98 | |
| 99 | /** |
| 100 | * The project group ID. |
| 101 | */ |
| 102 | @Parameter(defaultValue = "${project.groupId}") |
| 103 | protected String projectGroupId; |
| 104 | |
| 105 | /** |
| 106 | * The project artifact ID. |
| 107 | */ |
| 108 | @Parameter(defaultValue = "${project.artifactId}") |
| 109 | protected String projectArtifactId; |
| 110 | |
| 111 | /** |
| 112 | * The project version. |
| 113 | */ |
| 114 | @Parameter(defaultValue = "${project.version}") |
| 115 | protected String projectVersion; |
| 116 | |
| 117 | /** |
| 118 | * The project version. |
| 119 | */ |
| 120 | @Parameter(defaultValue = "${project.description}") |
| 121 | protected String projectDescription; |
| 122 | |
Brian O'Connor | bf7eca5 | 2015-04-15 16:25:46 -0700 | [diff] [blame] | 123 | @Parameter(defaultValue = "${localRepository}") |
| 124 | protected ArtifactRepository localRepository; |
| 125 | |
Thomas Vachuska | a98ae7f | 2015-04-14 14:00:36 -0700 | [diff] [blame] | 126 | /** |
| 127 | * Maven project |
| 128 | */ |
Brian O'Connor | bf7eca5 | 2015-04-15 16:25:46 -0700 | [diff] [blame] | 129 | @Parameter(defaultValue = "${project}") |
| 130 | protected MavenProject project; |
Thomas Vachuska | a98ae7f | 2015-04-14 14:00:36 -0700 | [diff] [blame] | 131 | |
| 132 | /** |
| 133 | * Maven project helper. |
| 134 | */ |
| 135 | @Component |
Brian O'Connor | bf7eca5 | 2015-04-15 16:25:46 -0700 | [diff] [blame] | 136 | protected MavenProjectHelper projectHelper; |
Thomas Vachuska | a98ae7f | 2015-04-14 14:00:36 -0700 | [diff] [blame] | 137 | |
| 138 | |
| 139 | private File m2Directory; |
| 140 | protected File stageDirectory; |
| 141 | protected String projectPath; |
Thomas Vachuska | a98ae7f | 2015-04-14 14:00:36 -0700 | [diff] [blame] | 142 | |
| 143 | @Override |
Thomas Vachuska | 8c8b037 | 2015-03-10 11:11:24 -0700 | [diff] [blame] | 144 | public void execute() throws MojoExecutionException { |
Thomas Vachuska | a98ae7f | 2015-04-14 14:00:36 -0700 | [diff] [blame] | 145 | File appFile = new File(baseDir, APP_XML); |
| 146 | File featuresFile = new File(baseDir, FEATURES_XML); |
Thomas Vachuska | 8c8b037 | 2015-03-10 11:11:24 -0700 | [diff] [blame] | 147 | |
Thomas Vachuska | 586afd8 | 2015-04-17 11:06:53 -0700 | [diff] [blame] | 148 | name = (String) project.getProperties().get(ONOS_APP_NAME); |
| 149 | |
| 150 | // If neither the app.xml file exists, nor the onos.app.name property |
| 151 | // is defined, there is nothing for this Mojo to do, so bail. |
| 152 | if (!appFile.exists() && name == null) { |
Thomas Vachuska | a98ae7f | 2015-04-14 14:00:36 -0700 | [diff] [blame] | 153 | return; |
| 154 | } |
| 155 | |
Brian O'Connor | bf7eca5 | 2015-04-15 16:25:46 -0700 | [diff] [blame] | 156 | m2Directory = new File(localRepository.getBasedir()); |
Thomas Vachuska | a98ae7f | 2015-04-14 14:00:36 -0700 | [diff] [blame] | 157 | stageDirectory = new File(dstDirectory, PACKAGE_DIR); |
Thomas Vachuska | a98ae7f | 2015-04-14 14:00:36 -0700 | [diff] [blame] | 158 | projectPath = M2_PREFIX + "/" + artifactDir(projectGroupId, projectArtifactId, projectVersion); |
| 159 | |
Thomas Vachuska | 586afd8 | 2015-04-17 11:06:53 -0700 | [diff] [blame] | 160 | origin = (String) project.getProperties().get(ONOS_APP_ORIGIN); |
| 161 | origin = origin != null ? origin : DEFAULT_ORIGIN; |
| 162 | |
| 163 | if (appFile.exists()) { |
| 164 | loadAppFile(appFile); |
| 165 | } else { |
| 166 | artifacts = ImmutableList.of(eval(DEFAULT_ARTIFACT)); |
| 167 | } |
Thomas Vachuska | a98ae7f | 2015-04-14 14:00:36 -0700 | [diff] [blame] | 168 | |
| 169 | // If there are any artifacts, stage the |
| 170 | if (!artifacts.isEmpty()) { |
Thomas Vachuska | 586afd8 | 2015-04-17 11:06:53 -0700 | [diff] [blame] | 171 | getLog().info("Building ONOS application package for " + name + " (v" + eval(version) + ")"); |
Thomas Vachuska | a98ae7f | 2015-04-14 14:00:36 -0700 | [diff] [blame] | 172 | artifacts.forEach(a -> getLog().debug("Including artifact: " + a)); |
| 173 | |
Brian O'Connor | bf7eca5 | 2015-04-15 16:25:46 -0700 | [diff] [blame] | 174 | if (stageDirectory.exists() || stageDirectory.mkdirs()) { |
| 175 | processAppXml(appFile); |
| 176 | processFeaturesXml(featuresFile); |
| 177 | processArtifacts(); |
| 178 | generateAppPackage(); |
| 179 | } else { |
| 180 | throw new MojoExecutionException("Unable to create directory: " + stageDirectory); |
| 181 | } |
Thomas Vachuska | a98ae7f | 2015-04-14 14:00:36 -0700 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
| 185 | // Loads the app.xml file. |
| 186 | private void loadAppFile(File appFile) throws MojoExecutionException { |
| 187 | XMLConfiguration xml = new XMLConfiguration(); |
| 188 | xml.setRootElementName(APP); |
| 189 | |
| 190 | try (FileInputStream stream = new FileInputStream(appFile)) { |
| 191 | xml.load(stream); |
| 192 | xml.setAttributeSplittingDisabled(true); |
| 193 | xml.setDelimiterParsingDisabled(true); |
| 194 | |
| 195 | name = xml.getString(NAME); |
| 196 | version = eval(xml.getString(VERSION)); |
Thomas Vachuska | a98ae7f | 2015-04-14 14:00:36 -0700 | [diff] [blame] | 197 | featuresRepo = eval(xml.getString(FEATURES_REPO)); |
| 198 | |
| 199 | artifacts = xml.configurationsAt(ARTIFACT).stream() |
| 200 | .map(cfg -> eval(cfg.getRootNode().getValue().toString())) |
| 201 | .collect(Collectors.toList()); |
| 202 | |
| 203 | } catch (ConfigurationException e) { |
| 204 | throw new MojoExecutionException("Unable to parse app.xml file", e); |
| 205 | } catch (FileNotFoundException e) { |
| 206 | throw new MojoExecutionException("Unable to find app.xml file", e); |
| 207 | } catch (IOException e) { |
| 208 | throw new MojoExecutionException("Unable to read app.xml file", e); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | // Processes and stages the app.xml file. |
| 213 | private void processAppXml(File appFile) throws MojoExecutionException { |
| 214 | try { |
| 215 | File file = new File(stageDirectory, APP_XML); |
| 216 | forceMkdir(stageDirectory); |
Thomas Vachuska | 586afd8 | 2015-04-17 11:06:53 -0700 | [diff] [blame] | 217 | String contents; |
| 218 | |
| 219 | if (appFile.exists()) { |
| 220 | contents = fileRead(appFile); |
| 221 | } else { |
| 222 | byte[] bytes = toByteArray(getClass().getResourceAsStream(APP_XML)); |
| 223 | contents = new String(bytes); |
| 224 | } |
| 225 | fileWrite(file.getAbsolutePath(), eval(contents)); |
Thomas Vachuska | a98ae7f | 2015-04-14 14:00:36 -0700 | [diff] [blame] | 226 | } catch (IOException e) { |
| 227 | throw new MojoExecutionException("Unable to process app.xml", e); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | private void processFeaturesXml(File featuresFile) throws MojoExecutionException { |
| 232 | boolean specified = featuresRepo != null && featuresRepo.length() > 0; |
| 233 | |
| 234 | // If featuresRepo attribute is specified and there is a features.xml |
| 235 | // file present, add the features repo as an artifact |
| 236 | try { |
| 237 | if (specified && featuresFile.exists()) { |
| 238 | processFeaturesXml(new FileInputStream(featuresFile)); |
| 239 | } else if (specified) { |
| 240 | processFeaturesXml(getClass().getResourceAsStream(FEATURES_XML)); |
| 241 | } |
| 242 | } catch (FileNotFoundException e) { |
| 243 | throw new MojoExecutionException("Unable to find features.xml file", e); |
| 244 | } catch (IOException e) { |
| 245 | throw new MojoExecutionException("Unable to process features.xml file", e); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | // Processes and stages the features.xml file. |
| 250 | private void processFeaturesXml(InputStream stream) throws IOException { |
| 251 | String featuresArtifact = |
| 252 | artifactFile(projectArtifactId, projectVersion, XML, "features"); |
| 253 | File dstDir = new File(stageDirectory, projectPath); |
| 254 | forceMkdir(dstDir); |
Thomas Vachuska | 586afd8 | 2015-04-17 11:06:53 -0700 | [diff] [blame] | 255 | String s = eval(new String(toByteArray(stream))); |
Thomas Vachuska | a98ae7f | 2015-04-14 14:00:36 -0700 | [diff] [blame] | 256 | fileWrite(new File(dstDir, featuresArtifact).getAbsolutePath(), s); |
| 257 | } |
| 258 | |
| 259 | // Stages all artifacts. |
Thomas Vachuska | a7a0220 | 2015-04-14 23:13:02 -0700 | [diff] [blame] | 260 | private void processArtifacts() throws MojoExecutionException { |
| 261 | for (String artifact : artifacts) { |
| 262 | processArtifact(artifact); |
| 263 | } |
Thomas Vachuska | a98ae7f | 2015-04-14 14:00:36 -0700 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | // Stages the specified artifact. |
Thomas Vachuska | a7a0220 | 2015-04-14 23:13:02 -0700 | [diff] [blame] | 267 | private void processArtifact(String artifact) throws MojoExecutionException { |
Thomas Vachuska | a98ae7f | 2015-04-14 14:00:36 -0700 | [diff] [blame] | 268 | if (!artifact.startsWith(MVN_URL)) { |
Thomas Vachuska | a7a0220 | 2015-04-14 23:13:02 -0700 | [diff] [blame] | 269 | throw new MojoExecutionException("Unsupported artifact URL:" + artifact); |
Thomas Vachuska | a98ae7f | 2015-04-14 14:00:36 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | String[] fields = artifact.substring(4).split("/"); |
| 273 | if (fields.length < 3) { |
Thomas Vachuska | a7a0220 | 2015-04-14 23:13:02 -0700 | [diff] [blame] | 274 | throw new MojoExecutionException("Illegal artifact URL:" + artifact); |
Thomas Vachuska | a98ae7f | 2015-04-14 14:00:36 -0700 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | try { |
| 278 | String file = artifactFile(fields); |
| 279 | |
| 280 | if (projectGroupId.equals(fields[0]) && projectArtifactId.equals(fields[1])) { |
| 281 | // Local artifact is not installed yet, package it from target directory. |
| 282 | File dstDir = new File(stageDirectory, projectPath); |
| 283 | forceMkdir(dstDir); |
| 284 | copyFile(new File(dstDirectory, file), new File(dstDir, file)); |
| 285 | } else { |
| 286 | // Other artifacts are packaged from ~/.m2/repository directory. |
| 287 | String m2Path = artifactDir(fields); |
| 288 | File srcDir = new File(m2Directory, m2Path); |
Thomas Vachuska | a7a0220 | 2015-04-14 23:13:02 -0700 | [diff] [blame] | 289 | File dstDir = new File(stageDirectory, M2_PREFIX + "/" + m2Path); |
Thomas Vachuska | a98ae7f | 2015-04-14 14:00:36 -0700 | [diff] [blame] | 290 | forceMkdir(dstDir); |
| 291 | copyFile(new File(srcDir, file), new File(dstDir, file)); |
| 292 | } |
| 293 | } catch (IOException e) { |
Thomas Vachuska | a7a0220 | 2015-04-14 23:13:02 -0700 | [diff] [blame] | 294 | throw new MojoExecutionException("Unable to stage artifact " + artifact, e); |
Thomas Vachuska | a98ae7f | 2015-04-14 14:00:36 -0700 | [diff] [blame] | 295 | } |
| 296 | } |
| 297 | |
| 298 | // Generates the ONOS package ZIP file. |
| 299 | private void generateAppPackage() throws MojoExecutionException { |
| 300 | File appZip = new File(dstDirectory, artifactFile(projectArtifactId, projectVersion, |
| 301 | APP_ZIP, null)); |
| 302 | try (FileOutputStream fos = new FileOutputStream(appZip); |
| 303 | ZipOutputStream zos = new ZipOutputStream(fos)) { |
| 304 | zipDirectory("", stageDirectory, zos); |
| 305 | projectHelper.attachArtifact(this.project, APP_ZIP, null, appZip); |
| 306 | } catch (IOException e) { |
| 307 | throw new MojoExecutionException("Unable to compress application package", e); |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | // Generates artifact directory name from the specified fields. |
| 312 | private String artifactDir(String[] fields) { |
| 313 | return artifactDir(fields[0], fields[1], fields[2]); |
| 314 | } |
| 315 | |
| 316 | // Generates artifact directory name from the specified elements. |
| 317 | private String artifactDir(String gid, String aid, String version) { |
| 318 | return gid.replace('.', '/') + "/" + aid.replace('.', '/') + "/" + version; |
| 319 | } |
| 320 | |
| 321 | // Generates artifact file name from the specified fields. |
| 322 | private String artifactFile(String[] fields) { |
| 323 | return fields.length < 5 ? |
| 324 | artifactFile(fields[1], fields[2], |
| 325 | (fields.length < 4 ? JAR : fields[3]), null) : |
| 326 | artifactFile(fields[1], fields[2], fields[3], fields[4]); |
| 327 | } |
| 328 | |
| 329 | // Generates artifact file name from the specified elements. |
| 330 | private String artifactFile(String aid, String version, String type, |
| 331 | String classifier) { |
| 332 | return classifier == null ? aid + "-" + version + "." + type : |
| 333 | aid + "-" + version + "-" + classifier + "." + type; |
| 334 | } |
| 335 | |
| 336 | // Returns the given string with project variable substitutions. |
| 337 | private String eval(String string) { |
| 338 | return string == null ? null : |
Thomas Vachuska | 586afd8 | 2015-04-17 11:06:53 -0700 | [diff] [blame] | 339 | string.replaceAll("\\$\\{onos.app.name\\}", name) |
| 340 | .replaceAll("\\$\\{onos.app.origin\\}", origin) |
| 341 | .replaceAll("\\$\\{project.groupId\\}", projectGroupId) |
Thomas Vachuska | a98ae7f | 2015-04-14 14:00:36 -0700 | [diff] [blame] | 342 | .replaceAll("\\$\\{project.artifactId\\}", projectArtifactId) |
| 343 | .replaceAll("\\$\\{project.version\\}", projectVersion) |
Thomas Vachuska | 586afd8 | 2015-04-17 11:06:53 -0700 | [diff] [blame] | 344 | .replaceAll("\\$\\{project.description\\}", projectDescription); |
Thomas Vachuska | a98ae7f | 2015-04-14 14:00:36 -0700 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | // Recursively archives the specified directory into a given ZIP stream. |
| 348 | private void zipDirectory(String root, File dir, ZipOutputStream zos) |
| 349 | throws IOException { |
Thomas Vachuska | 41e8c18 | 2015-04-21 11:09:19 -0700 | [diff] [blame] | 350 | byte[] buffer = new byte[BUFFER_SIZE]; |
Thomas Vachuska | a98ae7f | 2015-04-14 14:00:36 -0700 | [diff] [blame] | 351 | File[] files = dir.listFiles(); |
| 352 | if (files != null && files.length > 0) { |
| 353 | for (File file : files) { |
| 354 | if (file.isDirectory()) { |
| 355 | String path = root + file.getName() + "/"; |
| 356 | zos.putNextEntry(new ZipEntry(path)); |
| 357 | zipDirectory(path, file, zos); |
| 358 | zos.closeEntry(); |
| 359 | } else { |
| 360 | FileInputStream fin = new FileInputStream(file); |
| 361 | zos.putNextEntry(new ZipEntry(root + file.getName())); |
| 362 | int length; |
| 363 | while ((length = fin.read(buffer)) > 0) { |
| 364 | zos.write(buffer, 0, length); |
| 365 | } |
| 366 | zos.closeEntry(); |
| 367 | fin.close(); |
| 368 | } |
| 369 | } |
| 370 | } |
Thomas Vachuska | 8c8b037 | 2015-03-10 11:11:24 -0700 | [diff] [blame] | 371 | } |
| 372 | } |