Stuart McCulloch | ee51abf | 2008-02-19 09:49:41 +0000 | [diff] [blame] | 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; |
Carsten Ziegeler | 318c2cb | 2015-03-09 13:57:23 +0000 | [diff] [blame] | 29 | import org.apache.maven.plugins.annotations.Mojo; |
| 30 | import org.apache.maven.plugins.annotations.ResolutionScope; |
Stuart McCulloch | ee51abf | 2008-02-19 09:49:41 +0000 | [diff] [blame] | 31 | import org.apache.maven.project.MavenProject; |
Guillaume Nodet | ed7b110 | 2015-07-09 19:45:09 +0000 | [diff] [blame] | 32 | import org.apache.maven.shared.dependency.graph.DependencyNode; |
Stuart McCulloch | ee51abf | 2008-02-19 09:49:41 +0000 | [diff] [blame] | 33 | import org.codehaus.plexus.util.FileUtils; |
| 34 | import org.codehaus.plexus.util.IOUtil; |
| 35 | import org.codehaus.plexus.util.StringUtils; |
| 36 | |
Stuart McCulloch | 42151ee | 2012-07-16 13:43:38 +0000 | [diff] [blame] | 37 | import aQute.bnd.osgi.Builder; |
| 38 | import aQute.bnd.osgi.Jar; |
Stuart McCulloch | ee51abf | 2008-02-19 09:49:41 +0000 | [diff] [blame] | 39 | |
| 40 | |
| 41 | /** |
| 42 | * Generate Ant script to create the bundle (you should run ant:ant first). |
| 43 | * |
Stuart McCulloch | ee51abf | 2008-02-19 09:49:41 +0000 | [diff] [blame] | 44 | */ |
Carsten Ziegeler | 318c2cb | 2015-03-09 13:57:23 +0000 | [diff] [blame] | 45 | @Mojo( name = "ant", requiresDependencyResolution = ResolutionScope.TEST ) |
Stuart McCulloch | ee51abf | 2008-02-19 09:49:41 +0000 | [diff] [blame] | 46 | public class AntPlugin extends BundlePlugin |
| 47 | { |
| 48 | static final String BUILD_XML = "/build.xml"; |
| 49 | static final String BUILD_BND = "/maven-build.bnd"; |
| 50 | |
| 51 | |
Carsten Ziegeler | 82d8740 | 2015-03-04 13:26:21 +0000 | [diff] [blame] | 52 | @Override |
Guillaume Nodet | ed7b110 | 2015-07-09 19:45:09 +0000 | [diff] [blame] | 53 | protected void execute( MavenProject currentProject, DependencyNode dependencyGraph, Map<String, String> originalInstructions, Properties properties, |
Stuart McCulloch | ee51abf | 2008-02-19 09:49:41 +0000 | [diff] [blame] | 54 | Jar[] classpath ) throws MojoExecutionException |
| 55 | { |
| 56 | final String artifactId = getProject().getArtifactId(); |
| 57 | final String baseDir = getProject().getBasedir().getPath(); |
| 58 | |
| 59 | try |
| 60 | { |
| 61 | // assemble bundle as usual, but don't save it - this way we have all the instructions we need |
Guillaume Nodet | ed7b110 | 2015-07-09 19:45:09 +0000 | [diff] [blame] | 62 | Builder builder = buildOSGiBundle( currentProject, |
| 63 | dependencyGraph, originalInstructions, properties, classpath ); |
Stuart McCulloch | ee51abf | 2008-02-19 09:49:41 +0000 | [diff] [blame] | 64 | Properties bndProperties = builder.getProperties(); |
| 65 | |
| 66 | // cleanup and remove all non-strings from the builder properties |
| 67 | for ( Iterator i = bndProperties.values().iterator(); i.hasNext(); ) |
| 68 | { |
| 69 | if ( !( i.next() instanceof String ) ) |
| 70 | { |
| 71 | i.remove(); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | // save the BND generated bundle to the same output directory that maven uses |
| 76 | bndProperties.setProperty( "-output", "${maven.build.dir}/${maven.build.finalName}.jar" ); |
| 77 | |
| 78 | OutputStream out = new FileOutputStream( baseDir + BUILD_BND ); |
| 79 | bndProperties.store( out, " Merged BND Instructions" ); |
| 80 | IOUtil.close( out ); |
| 81 | |
| 82 | // modify build template |
| 83 | String buildXml = IOUtil.toString( getClass().getResourceAsStream( BUILD_XML ) ); |
Stuart McCulloch | 203195b | 2008-05-16 04:39:32 +0000 | [diff] [blame] | 84 | buildXml = StringUtils.replace( buildXml, "BND_VERSION", builder.getVersion() ); |
Stuart McCulloch | ee51abf | 2008-02-19 09:49:41 +0000 | [diff] [blame] | 85 | buildXml = StringUtils.replace( buildXml, "ARTIFACT_ID", artifactId ); |
| 86 | |
| 87 | FileUtils.fileWrite( baseDir + BUILD_XML, buildXml ); |
Stuart McCulloch | d8a04c5 | 2008-08-06 16:05:23 +0000 | [diff] [blame] | 88 | |
| 89 | // cleanup... |
| 90 | builder.close(); |
Stuart McCulloch | ee51abf | 2008-02-19 09:49:41 +0000 | [diff] [blame] | 91 | } |
| 92 | catch ( Exception e ) |
| 93 | { |
| 94 | throw new MojoExecutionException( "Problem creating Ant script", e ); |
| 95 | } |
| 96 | |
| 97 | getLog().info( "Wrote Ant bundle project for " + artifactId + " to " + baseDir ); |
| 98 | } |
| 99 | } |