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