blob: 24c876bea64f3bf53fc050d09580092b0509d456 [file] [log] [blame]
Thomas Vachuska8c8b0372015-03-10 11:11:24 -07001/*
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 */
16package org.onosproject.maven;
17
Thomas Vachuska586afd82015-04-17 11:06:53 -070018import com.google.common.collect.ImmutableList;
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -070019import org.apache.commons.configuration.ConfigurationException;
20import org.apache.commons.configuration.XMLConfiguration;
Jian Li97d6b2d2016-01-20 10:13:43 -080021import org.apache.commons.io.FileUtils;
Jian Lic35415d2016-01-14 17:22:31 -080022import org.apache.commons.lang.StringUtils;
Brian O'Connorbf7eca52015-04-15 16:25:46 -070023import org.apache.maven.artifact.repository.ArtifactRepository;
Thomas Vachuska8c8b0372015-03-10 11:11:24 -070024import org.apache.maven.plugin.AbstractMojo;
25import org.apache.maven.plugin.MojoExecutionException;
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -070026import org.apache.maven.plugins.annotations.Component;
Thomas Vachuska8c8b0372015-03-10 11:11:24 -070027import org.apache.maven.plugins.annotations.LifecyclePhase;
28import org.apache.maven.plugins.annotations.Mojo;
29import org.apache.maven.plugins.annotations.Parameter;
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -070030import org.apache.maven.project.MavenProject;
31import org.apache.maven.project.MavenProjectHelper;
Thomas Vachuska8c8b0372015-03-10 11:11:24 -070032
Jian Lic35415d2016-01-14 17:22:31 -080033import javax.imageio.ImageIO;
34import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
35import java.awt.image.BufferedImage;
36import java.awt.image.DataBufferByte;
37import java.awt.image.WritableRaster;
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -070038import java.io.File;
39import java.io.FileInputStream;
40import java.io.FileNotFoundException;
41import java.io.FileOutputStream;
42import java.io.IOException;
43import java.io.InputStream;
Thomas Vachuska8c8b0372015-03-10 11:11:24 -070044import java.util.List;
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -070045import java.util.stream.Collectors;
46import java.util.zip.ZipEntry;
47import java.util.zip.ZipOutputStream;
48
Thomas Vachuska586afd82015-04-17 11:06:53 -070049import static com.google.common.io.ByteStreams.toByteArray;
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -070050import static org.codehaus.plexus.util.FileUtils.*;
Thomas Vachuska8c8b0372015-03-10 11:11:24 -070051
52/**
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -070053 * Produces ONOS application archive using the app.xml file information.
Thomas Vachuska8c8b0372015-03-10 11:11:24 -070054 */
55@Mojo(name = "app", defaultPhase = LifecyclePhase.PACKAGE)
56public class OnosAppMojo extends AbstractMojo {
57
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -070058 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 Vachuskaa98ae7f2015-04-14 14:00:36 -070062 private static final String ARTIFACT = "artifact";
Thomas Vachuska8c8b0372015-03-10 11:11:24 -070063
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -070064 private static final String APP_XML = "app.xml";
Jian Li97d6b2d2016-01-20 10:13:43 -080065 private static final String APP_PNG = "app.png";
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -070066 private static final String FEATURES_XML = "features.xml";
Thomas Vachuska8c8b0372015-03-10 11:11:24 -070067
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -070068 private static final String MVN_URL = "mvn:";
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -070069 private static final String M2_PREFIX = "m2";
Thomas Vachuska8c8b0372015-03-10 11:11:24 -070070
Thomas Vachuska586afd82015-04-17 11:06:53 -070071 private static final String ONOS_APP_NAME = "onos.app.name";
72 private static final String ONOS_APP_ORIGIN = "onos.app.origin";
Thomas Vachuska761f0042015-11-11 19:10:17 -080073 private static final String ONOS_APP_REQUIRES = "onos.app.requires";
Thomas Vachuska586afd82015-04-17 11:06:53 -070074
Jian Lic35415d2016-01-14 17:22:31 -080075 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 Vachuskaa98ae7f2015-04-14 14:00:36 -070079 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 Vachuska8c8b0372015-03-10 11:11:24 -070083
Thomas Vachuska586afd82015-04-17 11:06:53 -070084 private static final String DEFAULT_ORIGIN = "ON.Lab";
Thomas Vachuska41e8c182015-04-21 11:09:19 -070085 private static final String DEFAULT_VERSION = "${project.version}";
Thomas Vachuska586afd82015-04-17 11:06:53 -070086
Jian Li97d6b2d2016-01-20 10:13:43 -080087 private static final String DEFAULT_CATEGORY = "default";
Jian Lic35415d2016-01-14 17:22:31 -080088 private static final String DEFAULT_URL = "http://onosproject.org";
89
Thomas Vachuska586afd82015-04-17 11:06:53 -070090 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 Vachuska41e8c182015-04-21 11:09:19 -070095 private static final int BUFFER_SIZE = 8192;
Thomas Vachuska8c8b0372015-03-10 11:11:24 -070096
Thomas Vachuska586afd82015-04-17 11:06:53 -070097 private String name;
98 private String origin;
Thomas Vachuska761f0042015-11-11 19:10:17 -080099 private String requiredApps;
Jian Lic35415d2016-01-14 17:22:31 -0800100 private String category;
101 private String url;
102 private String readme;
Thomas Vachuska586afd82015-04-17 11:06:53 -0700103 private String version = DEFAULT_VERSION;
Thomas Vachuska586afd82015-04-17 11:06:53 -0700104 private String featuresRepo = DEFAULT_FEATURES_REPO;
Thomas Vachuska8c8b0372015-03-10 11:11:24 -0700105 private List<String> artifacts;
106
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700107 /**
108 * The project base directory.
109 */
110 @Parameter(defaultValue = "${basedir}")
111 protected File baseDir;
Thomas Vachuska8c8b0372015-03-10 11:11:24 -0700112
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700113 /**
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'Connorbf7eca52015-04-15 16:25:46 -0700143 @Parameter(defaultValue = "${localRepository}")
144 protected ArtifactRepository localRepository;
145
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700146 /**
147 * Maven project
148 */
Brian O'Connorbf7eca52015-04-15 16:25:46 -0700149 @Parameter(defaultValue = "${project}")
150 protected MavenProject project;
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700151
152 /**
153 * Maven project helper.
154 */
155 @Component
Brian O'Connorbf7eca52015-04-15 16:25:46 -0700156 protected MavenProjectHelper projectHelper;
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700157
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700158 private File m2Directory;
159 protected File stageDirectory;
160 protected String projectPath;
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700161
162 @Override
Thomas Vachuska8c8b0372015-03-10 11:11:24 -0700163 public void execute() throws MojoExecutionException {
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700164 File appFile = new File(baseDir, APP_XML);
Jian Li97d6b2d2016-01-20 10:13:43 -0800165 File iconFile = new File(baseDir, APP_PNG);
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700166 File featuresFile = new File(baseDir, FEATURES_XML);
Thomas Vachuska8c8b0372015-03-10 11:11:24 -0700167
Thomas Vachuska586afd82015-04-17 11:06:53 -0700168 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 Vachuskaa98ae7f2015-04-14 14:00:36 -0700173 return;
174 }
175
Brian O'Connorbf7eca52015-04-15 16:25:46 -0700176 m2Directory = new File(localRepository.getBasedir());
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700177 stageDirectory = new File(dstDirectory, PACKAGE_DIR);
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700178 projectPath = M2_PREFIX + "/" + artifactDir(projectGroupId, projectArtifactId, projectVersion);
179
Thomas Vachuska586afd82015-04-17 11:06:53 -0700180 origin = (String) project.getProperties().get(ONOS_APP_ORIGIN);
181 origin = origin != null ? origin : DEFAULT_ORIGIN;
182
Thomas Vachuska761f0042015-11-11 19:10:17 -0800183 requiredApps = (String) project.getProperties().get(ONOS_APP_REQUIRES);
Thomas Vachuskafa776af2015-11-18 00:57:10 -0800184 requiredApps = requiredApps == null ? "" : requiredApps.replaceAll("[\\s]", "");
Thomas Vachuska761f0042015-11-11 19:10:17 -0800185
Jian Lic35415d2016-01-14 17:22:31 -0800186 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 Vachuska586afd82015-04-17 11:06:53 -0700196 if (appFile.exists()) {
197 loadAppFile(appFile);
198 } else {
199 artifacts = ImmutableList.of(eval(DEFAULT_ARTIFACT));
200 }
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700201
202 // If there are any artifacts, stage the
203 if (!artifacts.isEmpty()) {
Thomas Vachuska586afd82015-04-17 11:06:53 -0700204 getLog().info("Building ONOS application package for " + name + " (v" + eval(version) + ")");
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700205 artifacts.forEach(a -> getLog().debug("Including artifact: " + a));
206
Brian O'Connorbf7eca52015-04-15 16:25:46 -0700207 if (stageDirectory.exists() || stageDirectory.mkdirs()) {
208 processAppXml(appFile);
Jian Li97d6b2d2016-01-20 10:13:43 -0800209 processAppPng(iconFile);
Brian O'Connorbf7eca52015-04-15 16:25:46 -0700210 processFeaturesXml(featuresFile);
211 processArtifacts();
212 generateAppPackage();
213 } else {
214 throw new MojoExecutionException("Unable to create directory: " + stageDirectory);
215 }
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700216 }
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 Vachuskaa98ae7f2015-04-14 14:00:36 -0700231 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 Vachuska586afd82015-04-17 11:06:53 -0700251 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 Vachuskaa98ae7f2015-04-14 14:00:36 -0700260 } catch (IOException e) {
261 throw new MojoExecutionException("Unable to process app.xml", e);
262 }
263 }
264
Jian Li97d6b2d2016-01-20 10:13:43 -0800265 // 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 Vachuskaa98ae7f2015-04-14 14:00:36 -0700278 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 Vachuska586afd82015-04-17 11:06:53 -0700302 String s = eval(new String(toByteArray(stream)));
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700303 fileWrite(new File(dstDir, featuresArtifact).getAbsolutePath(), s);
304 }
305
306 // Stages all artifacts.
Thomas Vachuskaa7a02202015-04-14 23:13:02 -0700307 private void processArtifacts() throws MojoExecutionException {
308 for (String artifact : artifacts) {
309 processArtifact(artifact);
310 }
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700311 }
312
313 // Stages the specified artifact.
Thomas Vachuskaa7a02202015-04-14 23:13:02 -0700314 private void processArtifact(String artifact) throws MojoExecutionException {
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700315 if (!artifact.startsWith(MVN_URL)) {
Thomas Vachuskaa7a02202015-04-14 23:13:02 -0700316 throw new MojoExecutionException("Unsupported artifact URL:" + artifact);
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700317 }
318
319 String[] fields = artifact.substring(4).split("/");
320 if (fields.length < 3) {
Thomas Vachuskaa7a02202015-04-14 23:13:02 -0700321 throw new MojoExecutionException("Illegal artifact URL:" + artifact);
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700322 }
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 Vachuskaa7a02202015-04-14 23:13:02 -0700336 File dstDir = new File(stageDirectory, M2_PREFIX + "/" + m2Path);
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700337 forceMkdir(dstDir);
338 copyFile(new File(srcDir, file), new File(dstDir, file));
339 }
340 } catch (IOException e) {
Thomas Vachuskaa7a02202015-04-14 23:13:02 -0700341 throw new MojoExecutionException("Unable to stage artifact " + artifact, e);
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700342 }
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 Lic35415d2016-01-14 17:22:31 -0800348 APP_ZIP, null));
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700349 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 Lic35415d2016-01-14 17:22:31 -0800372 (fields.length < 4 ? JAR : fields[3]), null) :
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700373 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 Vachuska586afd82015-04-17 11:06:53 -0700386 string.replaceAll("\\$\\{onos.app.name\\}", name)
387 .replaceAll("\\$\\{onos.app.origin\\}", origin)
Thomas Vachuska761f0042015-11-11 19:10:17 -0800388 .replaceAll("\\$\\{onos.app.requires\\}", requiredApps)
Jian Lic35415d2016-01-14 17:22:31 -0800389 .replaceAll("\\$\\{onos.app.category\\}", category)
390 .replaceAll("\\$\\{onos.app.url\\}", url)
Thomas Vachuska586afd82015-04-17 11:06:53 -0700391 .replaceAll("\\$\\{project.groupId\\}", projectGroupId)
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700392 .replaceAll("\\$\\{project.artifactId\\}", projectArtifactId)
393 .replaceAll("\\$\\{project.version\\}", projectVersion)
Jian Li8bcb4f22016-01-20 10:36:18 -0800394 .replaceAll("\\$\\{project.description\\}", readme);
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700395 }
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 Vachuska41e8c182015-04-21 11:09:19 -0700400 byte[] buffer = new byte[BUFFER_SIZE];
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700401 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 Vachuska8c8b0372015-03-10 11:11:24 -0700421 }
Jian Lic35415d2016-01-14 17:22:31 -0800422}