AntPlugin xref

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one
    3    * or more contributor license agreements.  See the NOTICE file
    4    * distributed with this work for additional information
    5    * regarding copyright ownership.  The ASF licenses this file
    6    * to you under the Apache License, Version 2.0 (the
    7    * "License"); you may not use this file except in compliance
    8    * with the License.  You may obtain a copy of the License at
    9    *
    10   *   http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   * Unless required by applicable law or agreed to in writing,
    13   * software distributed under the License is distributed on an
    14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    15   * KIND, either express or implied.  See the License for the
    16   * specific language governing permissions and limitations
    17   * under the License.
    18   */
    19  package org.apache.felix.bundleplugin;
    20
    21
    22  import java.io.FileOutputStream;
    23  import java.io.OutputStream;
    24  import java.util.Iterator;
    25  import java.util.Map;
    26  import java.util.Properties;
    27
    28  import org.apache.maven.plugin.MojoExecutionException;
    29  import org.apache.maven.project.MavenProject;
    30  import org.codehaus.plexus.util.FileUtils;
    31  import org.codehaus.plexus.util.IOUtil;
    32  import org.codehaus.plexus.util.StringUtils;
    33
    34  import aQute.bnd.osgi.Builder;
    35  import aQute.bnd.osgi.Jar;
    36
    37
    38  /**
    39   * Generate Ant script to create the bundle (you should run ant:ant first).
    40   *
    41   * @goal ant
    42   * @requiresDependencyResolution test
    43   * @description generate Ant script to create the bundle
    44   */
    45  public class AntPlugin extends BundlePlugin
    46  {
    47      static final String BUILD_XML = "/build.xml";
    48      static final String BUILD_BND = "/maven-build.bnd";
    49
    50
    51      protected void execute( MavenProject currentProject, Map originalInstructions, Properties properties,
    52          Jar[] classpath ) throws MojoExecutionException
    53      {
    54          final String artifactId = getProject().getArtifactId();
    55          final String baseDir = getProject().getBasedir().getPath();
    56
    57          try
    58          {
    59              // assemble bundle as usual, but don't save it - this way we have all the instructions we need
    60              Builder builder = buildOSGiBundle( currentProject, originalInstructions, properties, classpath );
    61              Properties bndProperties = builder.getProperties();
    62
    63              // cleanup and remove all non-strings from the builder properties
    64              for ( Iterator i = bndProperties.values().iterator(); i.hasNext(); )
    65              {
    66                  if ( !( i.next() instanceof String ) )
    67                  {
    68                      i.remove();
    69                  }
    70              }
    71
    72              // save the BND generated bundle to the same output directory that maven uses
    73              bndProperties.setProperty( "-output", "${maven.build.dir}/${maven.build.finalName}.jar" );
    74
    75              OutputStream out = new FileOutputStream( baseDir + BUILD_BND );
    76              bndProperties.store( out, " Merged BND Instructions" );
    77              IOUtil.close( out );
    78
    79              // modify build template
    80              String buildXml = IOUtil.toString( getClass().getResourceAsStream( BUILD_XML ) );
    81              buildXml = StringUtils.replace( buildXml, "BND_VERSION", builder.getVersion() );
    82              buildXml = StringUtils.replace( buildXml, "ARTIFACT_ID", artifactId );
    83
    84              FileUtils.fileWrite( baseDir + BUILD_XML, buildXml );
    85
    86              // cleanup...
    87              builder.close();
    88          }
    89          catch ( Exception e )
    90          {
    91              throw new MojoExecutionException( "Problem creating Ant script", e );
    92          }
    93
    94          getLog().info( "Wrote Ant bundle project for " + artifactId + " to " + baseDir );
    95      }
    96  }