blob: bfc6127a09896a8e022a5dc709ef4c629041a54e [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;
Brian O'Connorbf7eca52015-04-15 16:25:46 -070021import org.apache.maven.artifact.repository.ArtifactRepository;
Thomas Vachuska8c8b0372015-03-10 11:11:24 -070022import org.apache.maven.plugin.AbstractMojo;
23import org.apache.maven.plugin.MojoExecutionException;
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -070024import org.apache.maven.plugins.annotations.Component;
Thomas Vachuska8c8b0372015-03-10 11:11:24 -070025import org.apache.maven.plugins.annotations.LifecyclePhase;
26import org.apache.maven.plugins.annotations.Mojo;
27import org.apache.maven.plugins.annotations.Parameter;
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -070028import org.apache.maven.project.MavenProject;
29import org.apache.maven.project.MavenProjectHelper;
Thomas Vachuska8c8b0372015-03-10 11:11:24 -070030
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -070031import java.io.File;
32import java.io.FileInputStream;
33import java.io.FileNotFoundException;
34import java.io.FileOutputStream;
35import java.io.IOException;
36import java.io.InputStream;
Thomas Vachuska8c8b0372015-03-10 11:11:24 -070037import java.util.List;
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -070038import java.util.stream.Collectors;
39import java.util.zip.ZipEntry;
40import java.util.zip.ZipOutputStream;
41
Thomas Vachuska586afd82015-04-17 11:06:53 -070042import static com.google.common.io.ByteStreams.toByteArray;
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -070043import static org.codehaus.plexus.util.FileUtils.*;
Thomas Vachuska8c8b0372015-03-10 11:11:24 -070044
45/**
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -070046 * Produces ONOS application archive using the app.xml file information.
Thomas Vachuska8c8b0372015-03-10 11:11:24 -070047 */
48@Mojo(name = "app", defaultPhase = LifecyclePhase.PACKAGE)
49public class OnosAppMojo extends AbstractMojo {
50
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -070051 private static final String APP = "app";
52 private static final String NAME = "[@name]";
53 private static final String VERSION = "[@version]";
54 private static final String FEATURES_REPO = "[@featuresRepo]";
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -070055 private static final String ARTIFACT = "artifact";
Thomas Vachuska8c8b0372015-03-10 11:11:24 -070056
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -070057 private static final String APP_XML = "app.xml";
58 private static final String FEATURES_XML = "features.xml";
Thomas Vachuska8c8b0372015-03-10 11:11:24 -070059
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -070060 private static final String MVN_URL = "mvn:";
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -070061 private static final String M2_PREFIX = "m2";
Thomas Vachuska8c8b0372015-03-10 11:11:24 -070062
Thomas Vachuska586afd82015-04-17 11:06:53 -070063 private static final String ONOS_APP_NAME = "onos.app.name";
64 private static final String ONOS_APP_ORIGIN = "onos.app.origin";
65
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -070066 private static final String JAR = "jar";
67 private static final String XML = "xml";
68 private static final String APP_ZIP = "oar";
69 private static final String PACKAGE_DIR = "oar";
Thomas Vachuska8c8b0372015-03-10 11:11:24 -070070
Thomas Vachuska586afd82015-04-17 11:06:53 -070071 private static final String DEFAULT_ORIGIN = "ON.Lab";
Thomas Vachuska41e8c182015-04-21 11:09:19 -070072 private static final String DEFAULT_VERSION = "${project.version}";
Thomas Vachuska586afd82015-04-17 11:06:53 -070073
74 private static final String DEFAULT_FEATURES_REPO =
75 "mvn:${project.groupId}/${project.artifactId}/${project.version}/xml/features";
76 private static final String DEFAULT_ARTIFACT =
77 "mvn:${project.groupId}/${project.artifactId}/${project.version}";
78
Thomas Vachuska41e8c182015-04-21 11:09:19 -070079 private static final int BUFFER_SIZE = 8192;
Thomas Vachuska8c8b0372015-03-10 11:11:24 -070080
Thomas Vachuska586afd82015-04-17 11:06:53 -070081 private String name;
82 private String origin;
83 private String version = DEFAULT_VERSION;
Thomas Vachuska586afd82015-04-17 11:06:53 -070084 private String featuresRepo = DEFAULT_FEATURES_REPO;
Thomas Vachuska8c8b0372015-03-10 11:11:24 -070085 private List<String> artifacts;
86
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -070087 /**
88 * The project base directory.
89 */
90 @Parameter(defaultValue = "${basedir}")
91 protected File baseDir;
Thomas Vachuska8c8b0372015-03-10 11:11:24 -070092
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -070093 /**
94 * The directory where the generated catalogue file will be put.
95 */
96 @Parameter(defaultValue = "${project.build.directory}")
97 protected File dstDirectory;
98
99 /**
100 * The project group ID.
101 */
102 @Parameter(defaultValue = "${project.groupId}")
103 protected String projectGroupId;
104
105 /**
106 * The project artifact ID.
107 */
108 @Parameter(defaultValue = "${project.artifactId}")
109 protected String projectArtifactId;
110
111 /**
112 * The project version.
113 */
114 @Parameter(defaultValue = "${project.version}")
115 protected String projectVersion;
116
117 /**
118 * The project version.
119 */
120 @Parameter(defaultValue = "${project.description}")
121 protected String projectDescription;
122
Brian O'Connorbf7eca52015-04-15 16:25:46 -0700123 @Parameter(defaultValue = "${localRepository}")
124 protected ArtifactRepository localRepository;
125
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700126 /**
127 * Maven project
128 */
Brian O'Connorbf7eca52015-04-15 16:25:46 -0700129 @Parameter(defaultValue = "${project}")
130 protected MavenProject project;
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700131
132 /**
133 * Maven project helper.
134 */
135 @Component
Brian O'Connorbf7eca52015-04-15 16:25:46 -0700136 protected MavenProjectHelper projectHelper;
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700137
138
139 private File m2Directory;
140 protected File stageDirectory;
141 protected String projectPath;
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700142
143 @Override
Thomas Vachuska8c8b0372015-03-10 11:11:24 -0700144 public void execute() throws MojoExecutionException {
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700145 File appFile = new File(baseDir, APP_XML);
146 File featuresFile = new File(baseDir, FEATURES_XML);
Thomas Vachuska8c8b0372015-03-10 11:11:24 -0700147
Thomas Vachuska586afd82015-04-17 11:06:53 -0700148 name = (String) project.getProperties().get(ONOS_APP_NAME);
149
150 // If neither the app.xml file exists, nor the onos.app.name property
151 // is defined, there is nothing for this Mojo to do, so bail.
152 if (!appFile.exists() && name == null) {
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700153 return;
154 }
155
Brian O'Connorbf7eca52015-04-15 16:25:46 -0700156 m2Directory = new File(localRepository.getBasedir());
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700157 stageDirectory = new File(dstDirectory, PACKAGE_DIR);
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700158 projectPath = M2_PREFIX + "/" + artifactDir(projectGroupId, projectArtifactId, projectVersion);
159
Thomas Vachuska586afd82015-04-17 11:06:53 -0700160 origin = (String) project.getProperties().get(ONOS_APP_ORIGIN);
161 origin = origin != null ? origin : DEFAULT_ORIGIN;
162
163 if (appFile.exists()) {
164 loadAppFile(appFile);
165 } else {
166 artifacts = ImmutableList.of(eval(DEFAULT_ARTIFACT));
167 }
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700168
169 // If there are any artifacts, stage the
170 if (!artifacts.isEmpty()) {
Thomas Vachuska586afd82015-04-17 11:06:53 -0700171 getLog().info("Building ONOS application package for " + name + " (v" + eval(version) + ")");
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700172 artifacts.forEach(a -> getLog().debug("Including artifact: " + a));
173
Brian O'Connorbf7eca52015-04-15 16:25:46 -0700174 if (stageDirectory.exists() || stageDirectory.mkdirs()) {
175 processAppXml(appFile);
176 processFeaturesXml(featuresFile);
177 processArtifacts();
178 generateAppPackage();
179 } else {
180 throw new MojoExecutionException("Unable to create directory: " + stageDirectory);
181 }
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700182 }
183 }
184
185 // Loads the app.xml file.
186 private void loadAppFile(File appFile) throws MojoExecutionException {
187 XMLConfiguration xml = new XMLConfiguration();
188 xml.setRootElementName(APP);
189
190 try (FileInputStream stream = new FileInputStream(appFile)) {
191 xml.load(stream);
192 xml.setAttributeSplittingDisabled(true);
193 xml.setDelimiterParsingDisabled(true);
194
195 name = xml.getString(NAME);
196 version = eval(xml.getString(VERSION));
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700197 featuresRepo = eval(xml.getString(FEATURES_REPO));
198
199 artifacts = xml.configurationsAt(ARTIFACT).stream()
200 .map(cfg -> eval(cfg.getRootNode().getValue().toString()))
201 .collect(Collectors.toList());
202
203 } catch (ConfigurationException e) {
204 throw new MojoExecutionException("Unable to parse app.xml file", e);
205 } catch (FileNotFoundException e) {
206 throw new MojoExecutionException("Unable to find app.xml file", e);
207 } catch (IOException e) {
208 throw new MojoExecutionException("Unable to read app.xml file", e);
209 }
210 }
211
212 // Processes and stages the app.xml file.
213 private void processAppXml(File appFile) throws MojoExecutionException {
214 try {
215 File file = new File(stageDirectory, APP_XML);
216 forceMkdir(stageDirectory);
Thomas Vachuska586afd82015-04-17 11:06:53 -0700217 String contents;
218
219 if (appFile.exists()) {
220 contents = fileRead(appFile);
221 } else {
222 byte[] bytes = toByteArray(getClass().getResourceAsStream(APP_XML));
223 contents = new String(bytes);
224 }
225 fileWrite(file.getAbsolutePath(), eval(contents));
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700226 } catch (IOException e) {
227 throw new MojoExecutionException("Unable to process app.xml", e);
228 }
229 }
230
231 private void processFeaturesXml(File featuresFile) throws MojoExecutionException {
232 boolean specified = featuresRepo != null && featuresRepo.length() > 0;
233
234 // If featuresRepo attribute is specified and there is a features.xml
235 // file present, add the features repo as an artifact
236 try {
237 if (specified && featuresFile.exists()) {
238 processFeaturesXml(new FileInputStream(featuresFile));
239 } else if (specified) {
240 processFeaturesXml(getClass().getResourceAsStream(FEATURES_XML));
241 }
242 } catch (FileNotFoundException e) {
243 throw new MojoExecutionException("Unable to find features.xml file", e);
244 } catch (IOException e) {
245 throw new MojoExecutionException("Unable to process features.xml file", e);
246 }
247 }
248
249 // Processes and stages the features.xml file.
250 private void processFeaturesXml(InputStream stream) throws IOException {
251 String featuresArtifact =
252 artifactFile(projectArtifactId, projectVersion, XML, "features");
253 File dstDir = new File(stageDirectory, projectPath);
254 forceMkdir(dstDir);
Thomas Vachuska586afd82015-04-17 11:06:53 -0700255 String s = eval(new String(toByteArray(stream)));
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700256 fileWrite(new File(dstDir, featuresArtifact).getAbsolutePath(), s);
257 }
258
259 // Stages all artifacts.
Thomas Vachuskaa7a02202015-04-14 23:13:02 -0700260 private void processArtifacts() throws MojoExecutionException {
261 for (String artifact : artifacts) {
262 processArtifact(artifact);
263 }
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700264 }
265
266 // Stages the specified artifact.
Thomas Vachuskaa7a02202015-04-14 23:13:02 -0700267 private void processArtifact(String artifact) throws MojoExecutionException {
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700268 if (!artifact.startsWith(MVN_URL)) {
Thomas Vachuskaa7a02202015-04-14 23:13:02 -0700269 throw new MojoExecutionException("Unsupported artifact URL:" + artifact);
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700270 }
271
272 String[] fields = artifact.substring(4).split("/");
273 if (fields.length < 3) {
Thomas Vachuskaa7a02202015-04-14 23:13:02 -0700274 throw new MojoExecutionException("Illegal artifact URL:" + artifact);
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700275 }
276
277 try {
278 String file = artifactFile(fields);
279
280 if (projectGroupId.equals(fields[0]) && projectArtifactId.equals(fields[1])) {
281 // Local artifact is not installed yet, package it from target directory.
282 File dstDir = new File(stageDirectory, projectPath);
283 forceMkdir(dstDir);
284 copyFile(new File(dstDirectory, file), new File(dstDir, file));
285 } else {
286 // Other artifacts are packaged from ~/.m2/repository directory.
287 String m2Path = artifactDir(fields);
288 File srcDir = new File(m2Directory, m2Path);
Thomas Vachuskaa7a02202015-04-14 23:13:02 -0700289 File dstDir = new File(stageDirectory, M2_PREFIX + "/" + m2Path);
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700290 forceMkdir(dstDir);
291 copyFile(new File(srcDir, file), new File(dstDir, file));
292 }
293 } catch (IOException e) {
Thomas Vachuskaa7a02202015-04-14 23:13:02 -0700294 throw new MojoExecutionException("Unable to stage artifact " + artifact, e);
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700295 }
296 }
297
298 // Generates the ONOS package ZIP file.
299 private void generateAppPackage() throws MojoExecutionException {
300 File appZip = new File(dstDirectory, artifactFile(projectArtifactId, projectVersion,
301 APP_ZIP, null));
302 try (FileOutputStream fos = new FileOutputStream(appZip);
303 ZipOutputStream zos = new ZipOutputStream(fos)) {
304 zipDirectory("", stageDirectory, zos);
305 projectHelper.attachArtifact(this.project, APP_ZIP, null, appZip);
306 } catch (IOException e) {
307 throw new MojoExecutionException("Unable to compress application package", e);
308 }
309 }
310
311 // Generates artifact directory name from the specified fields.
312 private String artifactDir(String[] fields) {
313 return artifactDir(fields[0], fields[1], fields[2]);
314 }
315
316 // Generates artifact directory name from the specified elements.
317 private String artifactDir(String gid, String aid, String version) {
318 return gid.replace('.', '/') + "/" + aid.replace('.', '/') + "/" + version;
319 }
320
321 // Generates artifact file name from the specified fields.
322 private String artifactFile(String[] fields) {
323 return fields.length < 5 ?
324 artifactFile(fields[1], fields[2],
325 (fields.length < 4 ? JAR : fields[3]), null) :
326 artifactFile(fields[1], fields[2], fields[3], fields[4]);
327 }
328
329 // Generates artifact file name from the specified elements.
330 private String artifactFile(String aid, String version, String type,
331 String classifier) {
332 return classifier == null ? aid + "-" + version + "." + type :
333 aid + "-" + version + "-" + classifier + "." + type;
334 }
335
336 // Returns the given string with project variable substitutions.
337 private String eval(String string) {
338 return string == null ? null :
Thomas Vachuska586afd82015-04-17 11:06:53 -0700339 string.replaceAll("\\$\\{onos.app.name\\}", name)
340 .replaceAll("\\$\\{onos.app.origin\\}", origin)
341 .replaceAll("\\$\\{project.groupId\\}", projectGroupId)
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700342 .replaceAll("\\$\\{project.artifactId\\}", projectArtifactId)
343 .replaceAll("\\$\\{project.version\\}", projectVersion)
Thomas Vachuska586afd82015-04-17 11:06:53 -0700344 .replaceAll("\\$\\{project.description\\}", projectDescription);
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700345 }
346
347 // Recursively archives the specified directory into a given ZIP stream.
348 private void zipDirectory(String root, File dir, ZipOutputStream zos)
349 throws IOException {
Thomas Vachuska41e8c182015-04-21 11:09:19 -0700350 byte[] buffer = new byte[BUFFER_SIZE];
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700351 File[] files = dir.listFiles();
352 if (files != null && files.length > 0) {
353 for (File file : files) {
354 if (file.isDirectory()) {
355 String path = root + file.getName() + "/";
356 zos.putNextEntry(new ZipEntry(path));
357 zipDirectory(path, file, zos);
358 zos.closeEntry();
359 } else {
360 FileInputStream fin = new FileInputStream(file);
361 zos.putNextEntry(new ZipEntry(root + file.getName()));
362 int length;
363 while ((length = fin.read(buffer)) > 0) {
364 zos.write(buffer, 0, length);
365 }
366 zos.closeEntry();
367 fin.close();
368 }
369 }
370 }
Thomas Vachuska8c8b0372015-03-10 11:11:24 -0700371 }
372}