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