Save This Page
Home » maven-launchpad-plugin-2.0.6-source-release » org.apache.sling.maven.projectsupport » [javadoc | source]
    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   package org.apache.sling.maven.projectsupport;
   18   
   19   import java.io.File;
   20   import java.io.FileOutputStream;
   21   import java.io.IOException;
   22   
   23   import org.apache.maven.plugin.MojoExecutionException;
   24   import org.apache.maven.plugin.MojoFailureException;
   25   import org.apache.sling.maven.projectsupport.bundlelist.v1_0_0.Bundle;
   26   import org.apache.sling.maven.projectsupport.bundlelist.v1_0_0.BundleList;
   27   import org.apache.sling.maven.projectsupport.bundlelist.v1_0_0.StartLevel;
   28   import org.jdom.Document;
   29   import org.jdom.Element;
   30   import org.jdom.output.Format;
   31   import org.jdom.output.XMLOutputter;
   32   
   33   /**
   34    * Create and attach a karaf feature descriptor XML file.
   35    *
   36    * @goal create-karaf-descriptor
   37    * @phase package
   38    * @description create a karaf feature descriptor
   39    */
   40   public class CreateKarafFeatureDescriptorMojo extends AbstractBundleListMojo {
   41   
   42       private static final String CLASSIFIER = "features";
   43   
   44       private static final String TYPE = "xml";
   45   
   46       /**
   47        * @parameter default-value="sling"
   48        */
   49       private String featureName;
   50   
   51       /**
   52        * @parameter default-value="sling-2.0"
   53        */
   54       private String featuresName;
   55   
   56       /**
   57        * @parameter default-value="${project.version}"
   58        */
   59       private String featureVersion;
   60   
   61       /**
   62        * The output directory.
   63        *
   64        * @parameter default-value="${project.build.directory}/features.xml"
   65        */
   66       private File outputFile;
   67   
   68       @Override
   69       protected void executeWithArtifacts() throws MojoExecutionException, MojoFailureException {
   70           Document doc = new Document();
   71   
   72           Element features = new Element("features");
   73           doc.setRootElement(features);
   74           features.setAttribute("name", featuresName);
   75   
   76           Element feature = new Element("feature");
   77           features.addContent(feature);
   78           feature.setAttribute("name", featureName);
   79           feature.setAttribute("version", featureVersion);
   80   
   81           BundleList bundleList = getBundleList();
   82           for (StartLevel level : bundleList.getStartLevels()) {
   83               for (Bundle bundle : level.getBundles()) {
   84                   String bundleRef = String.format("mvn:%s/%s/%s", bundle.getGroupId(), bundle.getArtifactId(), bundle
   85                           .getVersion());
   86                   feature.addContent(new Element("bundle").setText(bundleRef));
   87               }
   88           }
   89   
   90           FileOutputStream out = null;
   91           try {
   92               out = new FileOutputStream(outputFile);
   93               new XMLOutputter(Format.getPrettyFormat().setEncoding("UTF-8")).output(doc, out);
   94               projectHelper.attachArtifact(project, TYPE, CLASSIFIER, outputFile);
   95           } catch (IOException e) {
   96               throw new MojoExecutionException("Unable to write features.xml", e);
   97           } finally {
   98               if (out != null) {
   99                   try {
  100                       out.close();
  101                   } catch (IOException e) {
  102                   }
  103               }
  104           }
  105   
  106       }
  107   
  108   }

Save This Page
Home » maven-launchpad-plugin-2.0.6-source-release » org.apache.sling.maven.projectsupport » [javadoc | source]