blob: 6865ccf249bf610c7cbf0f5a0244c8b91b150747 [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 SNAPSHOT = "-SNAPSHOT";
Thomas Vachuska586afd82015-04-17 11:06:53 -070067
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -070068 private static final String JAR = "jar";
69 private static final String XML = "xml";
70 private static final String APP_ZIP = "oar";
71 private static final String PACKAGE_DIR = "oar";
Thomas Vachuska8c8b0372015-03-10 11:11:24 -070072
Thomas Vachuska586afd82015-04-17 11:06:53 -070073 private static final String DEFAULT_ORIGIN = "ON.Lab";
74 private static final String DEFAULT_VERSION = "${short.version}";
75
76 private static final String DEFAULT_FEATURES_REPO =
77 "mvn:${project.groupId}/${project.artifactId}/${project.version}/xml/features";
78 private static final String DEFAULT_ARTIFACT =
79 "mvn:${project.groupId}/${project.artifactId}/${project.version}";
80
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -070081 private static final int BUFFER_ZIZE = 8192;
Thomas Vachuska8c8b0372015-03-10 11:11:24 -070082
Thomas Vachuska586afd82015-04-17 11:06:53 -070083 private String name;
84 private String origin;
85 private String version = DEFAULT_VERSION;
86 private String shortVersion;
87 private String featuresRepo = DEFAULT_FEATURES_REPO;
Thomas Vachuska8c8b0372015-03-10 11:11:24 -070088 private List<String> artifacts;
89
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -070090 /**
91 * The project base directory.
92 */
93 @Parameter(defaultValue = "${basedir}")
94 protected File baseDir;
Thomas Vachuska8c8b0372015-03-10 11:11:24 -070095
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -070096 /**
97 * The directory where the generated catalogue file will be put.
98 */
99 @Parameter(defaultValue = "${project.build.directory}")
100 protected File dstDirectory;
101
102 /**
103 * The project group ID.
104 */
105 @Parameter(defaultValue = "${project.groupId}")
106 protected String projectGroupId;
107
108 /**
109 * The project artifact ID.
110 */
111 @Parameter(defaultValue = "${project.artifactId}")
112 protected String projectArtifactId;
113
114 /**
115 * The project version.
116 */
117 @Parameter(defaultValue = "${project.version}")
118 protected String projectVersion;
119
120 /**
121 * The project version.
122 */
123 @Parameter(defaultValue = "${project.description}")
124 protected String projectDescription;
125
Brian O'Connorbf7eca52015-04-15 16:25:46 -0700126 @Parameter(defaultValue = "${localRepository}")
127 protected ArtifactRepository localRepository;
128
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700129 /**
130 * Maven project
131 */
Brian O'Connorbf7eca52015-04-15 16:25:46 -0700132 @Parameter(defaultValue = "${project}")
133 protected MavenProject project;
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700134
135 /**
136 * Maven project helper.
137 */
138 @Component
Brian O'Connorbf7eca52015-04-15 16:25:46 -0700139 protected MavenProjectHelper projectHelper;
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700140
141
142 private File m2Directory;
143 protected File stageDirectory;
144 protected String projectPath;
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700145
146 @Override
Thomas Vachuska8c8b0372015-03-10 11:11:24 -0700147 public void execute() throws MojoExecutionException {
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700148 File appFile = new File(baseDir, APP_XML);
149 File featuresFile = new File(baseDir, FEATURES_XML);
Thomas Vachuska8c8b0372015-03-10 11:11:24 -0700150
Thomas Vachuska586afd82015-04-17 11:06:53 -0700151 name = (String) project.getProperties().get(ONOS_APP_NAME);
152
153 // If neither the app.xml file exists, nor the onos.app.name property
154 // is defined, there is nothing for this Mojo to do, so bail.
155 if (!appFile.exists() && name == null) {
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700156 return;
157 }
158
Brian O'Connorbf7eca52015-04-15 16:25:46 -0700159 m2Directory = new File(localRepository.getBasedir());
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700160 stageDirectory = new File(dstDirectory, PACKAGE_DIR);
Thomas Vachuska586afd82015-04-17 11:06:53 -0700161 shortVersion = projectVersion.replace(SNAPSHOT, "");
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700162 projectPath = M2_PREFIX + "/" + artifactDir(projectGroupId, projectArtifactId, projectVersion);
163
Thomas Vachuska586afd82015-04-17 11:06:53 -0700164 origin = (String) project.getProperties().get(ONOS_APP_ORIGIN);
165 origin = origin != null ? origin : DEFAULT_ORIGIN;
166
167 if (appFile.exists()) {
168 loadAppFile(appFile);
169 } else {
170 artifacts = ImmutableList.of(eval(DEFAULT_ARTIFACT));
171 }
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700172
173 // If there are any artifacts, stage the
174 if (!artifacts.isEmpty()) {
Thomas Vachuska586afd82015-04-17 11:06:53 -0700175 getLog().info("Building ONOS application package for " + name + " (v" + eval(version) + ")");
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700176 artifacts.forEach(a -> getLog().debug("Including artifact: " + a));
177
Brian O'Connorbf7eca52015-04-15 16:25:46 -0700178 if (stageDirectory.exists() || stageDirectory.mkdirs()) {
179 processAppXml(appFile);
180 processFeaturesXml(featuresFile);
181 processArtifacts();
182 generateAppPackage();
183 } else {
184 throw new MojoExecutionException("Unable to create directory: " + stageDirectory);
185 }
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700186 }
187 }
188
189 // Loads the app.xml file.
190 private void loadAppFile(File appFile) throws MojoExecutionException {
191 XMLConfiguration xml = new XMLConfiguration();
192 xml.setRootElementName(APP);
193
194 try (FileInputStream stream = new FileInputStream(appFile)) {
195 xml.load(stream);
196 xml.setAttributeSplittingDisabled(true);
197 xml.setDelimiterParsingDisabled(true);
198
199 name = xml.getString(NAME);
200 version = eval(xml.getString(VERSION));
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700201 featuresRepo = eval(xml.getString(FEATURES_REPO));
202
203 artifacts = xml.configurationsAt(ARTIFACT).stream()
204 .map(cfg -> eval(cfg.getRootNode().getValue().toString()))
205 .collect(Collectors.toList());
206
207 } catch (ConfigurationException e) {
208 throw new MojoExecutionException("Unable to parse app.xml file", e);
209 } catch (FileNotFoundException e) {
210 throw new MojoExecutionException("Unable to find app.xml file", e);
211 } catch (IOException e) {
212 throw new MojoExecutionException("Unable to read app.xml file", e);
213 }
214 }
215
216 // Processes and stages the app.xml file.
217 private void processAppXml(File appFile) throws MojoExecutionException {
218 try {
219 File file = new File(stageDirectory, APP_XML);
220 forceMkdir(stageDirectory);
Thomas Vachuska586afd82015-04-17 11:06:53 -0700221 String contents;
222
223 if (appFile.exists()) {
224 contents = fileRead(appFile);
225 } else {
226 byte[] bytes = toByteArray(getClass().getResourceAsStream(APP_XML));
227 contents = new String(bytes);
228 }
229 fileWrite(file.getAbsolutePath(), eval(contents));
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700230 } catch (IOException e) {
231 throw new MojoExecutionException("Unable to process app.xml", e);
232 }
233 }
234
235 private void processFeaturesXml(File featuresFile) throws MojoExecutionException {
236 boolean specified = featuresRepo != null && featuresRepo.length() > 0;
237
238 // If featuresRepo attribute is specified and there is a features.xml
239 // file present, add the features repo as an artifact
240 try {
241 if (specified && featuresFile.exists()) {
242 processFeaturesXml(new FileInputStream(featuresFile));
243 } else if (specified) {
244 processFeaturesXml(getClass().getResourceAsStream(FEATURES_XML));
245 }
246 } catch (FileNotFoundException e) {
247 throw new MojoExecutionException("Unable to find features.xml file", e);
248 } catch (IOException e) {
249 throw new MojoExecutionException("Unable to process features.xml file", e);
250 }
251 }
252
253 // Processes and stages the features.xml file.
254 private void processFeaturesXml(InputStream stream) throws IOException {
255 String featuresArtifact =
256 artifactFile(projectArtifactId, projectVersion, XML, "features");
257 File dstDir = new File(stageDirectory, projectPath);
258 forceMkdir(dstDir);
Thomas Vachuska586afd82015-04-17 11:06:53 -0700259 String s = eval(new String(toByteArray(stream)));
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700260 fileWrite(new File(dstDir, featuresArtifact).getAbsolutePath(), s);
261 }
262
263 // Stages all artifacts.
Thomas Vachuskaa7a02202015-04-14 23:13:02 -0700264 private void processArtifacts() throws MojoExecutionException {
265 for (String artifact : artifacts) {
266 processArtifact(artifact);
267 }
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700268 }
269
270 // Stages the specified artifact.
Thomas Vachuskaa7a02202015-04-14 23:13:02 -0700271 private void processArtifact(String artifact) throws MojoExecutionException {
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700272 if (!artifact.startsWith(MVN_URL)) {
Thomas Vachuskaa7a02202015-04-14 23:13:02 -0700273 throw new MojoExecutionException("Unsupported artifact URL:" + artifact);
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700274 }
275
276 String[] fields = artifact.substring(4).split("/");
277 if (fields.length < 3) {
Thomas Vachuskaa7a02202015-04-14 23:13:02 -0700278 throw new MojoExecutionException("Illegal artifact URL:" + artifact);
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700279 }
280
281 try {
282 String file = artifactFile(fields);
283
284 if (projectGroupId.equals(fields[0]) && projectArtifactId.equals(fields[1])) {
285 // Local artifact is not installed yet, package it from target directory.
286 File dstDir = new File(stageDirectory, projectPath);
287 forceMkdir(dstDir);
288 copyFile(new File(dstDirectory, file), new File(dstDir, file));
289 } else {
290 // Other artifacts are packaged from ~/.m2/repository directory.
291 String m2Path = artifactDir(fields);
292 File srcDir = new File(m2Directory, m2Path);
Thomas Vachuskaa7a02202015-04-14 23:13:02 -0700293 File dstDir = new File(stageDirectory, M2_PREFIX + "/" + m2Path);
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700294 forceMkdir(dstDir);
295 copyFile(new File(srcDir, file), new File(dstDir, file));
296 }
297 } catch (IOException e) {
Thomas Vachuskaa7a02202015-04-14 23:13:02 -0700298 throw new MojoExecutionException("Unable to stage artifact " + artifact, e);
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700299 }
300 }
301
302 // Generates the ONOS package ZIP file.
303 private void generateAppPackage() throws MojoExecutionException {
304 File appZip = new File(dstDirectory, artifactFile(projectArtifactId, projectVersion,
305 APP_ZIP, null));
306 try (FileOutputStream fos = new FileOutputStream(appZip);
307 ZipOutputStream zos = new ZipOutputStream(fos)) {
308 zipDirectory("", stageDirectory, zos);
309 projectHelper.attachArtifact(this.project, APP_ZIP, null, appZip);
310 } catch (IOException e) {
311 throw new MojoExecutionException("Unable to compress application package", e);
312 }
313 }
314
315 // Generates artifact directory name from the specified fields.
316 private String artifactDir(String[] fields) {
317 return artifactDir(fields[0], fields[1], fields[2]);
318 }
319
320 // Generates artifact directory name from the specified elements.
321 private String artifactDir(String gid, String aid, String version) {
322 return gid.replace('.', '/') + "/" + aid.replace('.', '/') + "/" + version;
323 }
324
325 // Generates artifact file name from the specified fields.
326 private String artifactFile(String[] fields) {
327 return fields.length < 5 ?
328 artifactFile(fields[1], fields[2],
329 (fields.length < 4 ? JAR : fields[3]), null) :
330 artifactFile(fields[1], fields[2], fields[3], fields[4]);
331 }
332
333 // Generates artifact file name from the specified elements.
334 private String artifactFile(String aid, String version, String type,
335 String classifier) {
336 return classifier == null ? aid + "-" + version + "." + type :
337 aid + "-" + version + "-" + classifier + "." + type;
338 }
339
340 // Returns the given string with project variable substitutions.
341 private String eval(String string) {
342 return string == null ? null :
Thomas Vachuska586afd82015-04-17 11:06:53 -0700343 string.replaceAll("\\$\\{onos.app.name\\}", name)
344 .replaceAll("\\$\\{onos.app.origin\\}", origin)
345 .replaceAll("\\$\\{project.groupId\\}", projectGroupId)
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700346 .replaceAll("\\$\\{project.artifactId\\}", projectArtifactId)
347 .replaceAll("\\$\\{project.version\\}", projectVersion)
Thomas Vachuska586afd82015-04-17 11:06:53 -0700348 .replaceAll("\\$\\{short.version\\}", shortVersion)
349 .replaceAll("\\$\\{project.description\\}", projectDescription);
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -0700350 }
351
352 // Recursively archives the specified directory into a given ZIP stream.
353 private void zipDirectory(String root, File dir, ZipOutputStream zos)
354 throws IOException {
355 byte[] buffer = new byte[BUFFER_ZIZE];
356 File[] files = dir.listFiles();
357 if (files != null && files.length > 0) {
358 for (File file : files) {
359 if (file.isDirectory()) {
360 String path = root + file.getName() + "/";
361 zos.putNextEntry(new ZipEntry(path));
362 zipDirectory(path, file, zos);
363 zos.closeEntry();
364 } else {
365 FileInputStream fin = new FileInputStream(file);
366 zos.putNextEntry(new ZipEntry(root + file.getName()));
367 int length;
368 while ((length = fin.read(buffer)) > 0) {
369 zos.write(buffer, 0, length);
370 }
371 zos.closeEntry();
372 fin.close();
373 }
374 }
375 }
Thomas Vachuska8c8b0372015-03-10 11:11:24 -0700376 }
377}