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