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