blob: 084d55ccb020e159049187c27a601691d55e401d [file] [log] [blame]
Richard S. Hallf3120092006-10-31 17:32:16 +00001/*
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 */
Richard S. Hall01a56ce2007-05-20 22:31:11 +000019package org.apache.felix.bundleplugin;
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +000020
Stuart McCulloch5ae59142008-01-29 06:21:05 +000021
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +000022import java.io.ByteArrayOutputStream;
23import java.io.File;
Stuart McCulloch3d34d502008-01-09 15:19:32 +000024import java.io.FileInputStream;
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +000025import java.io.IOException;
Stuart McCulloch3d34d502008-01-09 15:19:32 +000026import java.io.InputStream;
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +000027import java.lang.reflect.Method;
Hiram R. Chirino2d401a32011-01-27 20:58:44 +000028import java.util.*;
Stuart McCulloch3d34d502008-01-09 15:19:32 +000029import java.util.jar.Attributes;
Stuart McCulloch717740b2007-09-21 11:18:31 +000030import java.util.jar.Manifest;
Richard S. Hall420d2782007-06-14 12:31:48 +000031
Hiram R. Chirino2d401a32011-01-27 20:58:44 +000032import aQute.lib.osgi.*;
Stuart McCulloch3d34d502008-01-09 15:19:32 +000033import org.apache.maven.archiver.ManifestSection;
Stuart McCullochf978c532007-12-27 09:16:34 +000034import org.apache.maven.archiver.MavenArchiveConfiguration;
35import org.apache.maven.archiver.MavenArchiver;
Richard S. Hallf3120092006-10-31 17:32:16 +000036import org.apache.maven.artifact.Artifact;
Stuart McCullochffadcf62007-08-24 10:39:35 +000037import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +000038import org.apache.maven.model.License;
39import org.apache.maven.model.Model;
40import org.apache.maven.plugin.AbstractMojo;
41import org.apache.maven.plugin.MojoExecutionException;
42import org.apache.maven.plugin.MojoFailureException;
Stuart McCulloch48fcb1e2008-01-30 06:58:34 +000043import org.apache.maven.plugin.logging.Log;
Richard S. Hallf3120092006-10-31 17:32:16 +000044import org.apache.maven.project.MavenProject;
Stuart McCulloch2d301732008-02-18 09:01:17 +000045import org.apache.maven.project.MavenProjectHelper;
Stuart McCulloch99f4aa62009-02-17 13:40:44 +000046import org.apache.maven.shared.osgi.DefaultMaven2OsgiConverter;
Richard S. Hall17b8e3d2007-06-13 16:20:40 +000047import org.apache.maven.shared.osgi.Maven2OsgiConverter;
Stuart McCullochdd0d9ba2007-12-07 08:22:20 +000048import org.codehaus.plexus.archiver.UnArchiver;
49import org.codehaus.plexus.archiver.manager.ArchiverManager;
Stuart McCulloch44deaef2007-11-18 04:36:21 +000050import org.codehaus.plexus.util.DirectoryScanner;
Stuart McCullochf978c532007-12-27 09:16:34 +000051import org.codehaus.plexus.util.StringInputStream;
Stuart McCulloch6cb86db2008-03-15 21:38:26 +000052import org.codehaus.plexus.util.StringUtils;
Richard S. Hall420d2782007-06-14 12:31:48 +000053
Guillaume Nodet459eaed2009-09-02 21:15:28 +000054import aQute.lib.spring.SpringXMLType;
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +000055
Stuart McCulloch5ae59142008-01-29 06:21:05 +000056
Richard S. Hallf3120092006-10-31 17:32:16 +000057/**
Richard S. Hall420d2782007-06-14 12:31:48 +000058 * Create an OSGi bundle from Maven project
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +000059 *
Richard S. Hallf3120092006-10-31 17:32:16 +000060 * @goal bundle
61 * @phase package
Stuart McCullochf7d0c7c2009-01-29 07:23:06 +000062 * @requiresDependencyResolution test
Richard S. Hallf3120092006-10-31 17:32:16 +000063 * @description build an OSGi bundle jar
64 */
Stuart McCulloch5ae59142008-01-29 06:21:05 +000065public class BundlePlugin extends AbstractMojo
66{
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +000067 /**
Stuart McCulloch717740b2007-09-21 11:18:31 +000068 * Directory where the manifest will be written
Carsten Ziegelerb39d23d2007-09-24 12:22:32 +000069 *
Stuart McCulloch717740b2007-09-21 11:18:31 +000070 * @parameter expression="${manifestLocation}" default-value="${project.build.outputDirectory}/META-INF"
71 */
Stuart McCulloch0ab63892007-10-19 11:43:24 +000072 protected File manifestLocation;
Stuart McCulloch717740b2007-09-21 11:18:31 +000073
74 /**
Stuart McCullochdd0d9ba2007-12-07 08:22:20 +000075 * When true, unpack the bundle contents to the outputDirectory
76 *
77 * @parameter expression="${unpackBundle}"
78 */
79 protected boolean unpackBundle;
80
81 /**
Stuart McCulloch5ffc3fe2008-02-20 05:37:57 +000082 * Comma separated list of artifactIds to exclude from the dependency classpath passed to BND (use "true" to exclude everything)
Stuart McCullochcb02d6a2008-01-09 16:42:16 +000083 *
84 * @parameter expression="${excludeDependencies}"
85 */
Stuart McCulloch5ffc3fe2008-02-20 05:37:57 +000086 protected String excludeDependencies;
Stuart McCullochcb02d6a2008-01-09 16:42:16 +000087
88 /**
Stuart McCulloch2d301732008-02-18 09:01:17 +000089 * Classifier type of the bundle to be installed. For example, "jdk14".
90 * Defaults to none which means this is the project's main bundle.
91 *
Stuart McCulloch96f089c2009-09-02 16:42:51 +000092 * @parameter
Stuart McCulloch2d301732008-02-18 09:01:17 +000093 */
94 protected String classifier;
95
96 /**
97 * @component
98 */
99 private MavenProjectHelper m_projectHelper;
100
101 /**
Stuart McCullochdd0d9ba2007-12-07 08:22:20 +0000102 * @component
103 */
Stuart McCulloch9366a822008-01-29 07:45:57 +0000104 private ArchiverManager m_archiverManager;
Stuart McCullochdd0d9ba2007-12-07 08:22:20 +0000105
106 /**
Stuart McCullochffadcf62007-08-24 10:39:35 +0000107 * @component
108 */
Stuart McCulloch9366a822008-01-29 07:45:57 +0000109 private ArtifactHandlerManager m_artifactHandlerManager;
Stuart McCullochffadcf62007-08-24 10:39:35 +0000110
111 /**
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000112 * Project types which this plugin supports.
113 *
114 * @parameter
115 */
Hiram R. Chirino12264ad2011-02-01 20:54:49 +0000116 protected List supportedProjectTypes = Arrays.asList( new String[]
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000117 { "jar", "bundle" } );
Richard S. Hall3c0051d2007-04-27 17:07:17 +0000118
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000119 /**
120 * The directory for the generated bundles.
121 *
122 * @parameter expression="${project.build.outputDirectory}"
123 * @required
124 */
125 private File outputDirectory;
Richard S. Hall17b8e3d2007-06-13 16:20:40 +0000126
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000127 /**
128 * The directory for the pom
129 *
130 * @parameter expression="${basedir}"
131 * @required
132 */
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000133 private File baseDir;
Richard S. Hall17b8e3d2007-06-13 16:20:40 +0000134
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000135 /**
136 * The directory for the generated JAR.
137 *
138 * @parameter expression="${project.build.directory}"
139 * @required
140 */
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000141 private String buildDirectory;
Richard S. Hall17b8e3d2007-06-13 16:20:40 +0000142
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000143 /**
144 * The Maven project.
145 *
146 * @parameter expression="${project}"
147 * @required
148 * @readonly
149 */
150 private MavenProject project;
Richard S. Hall420d2782007-06-14 12:31:48 +0000151
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000152 /**
Stuart McCulloch2d301732008-02-18 09:01:17 +0000153 * The BND instructions for the bundle.
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000154 *
155 * @parameter
156 */
Stuart McCullochd00f9712009-07-13 10:06:47 +0000157 private Map instructions = new LinkedHashMap();
Richard S. Hall6cf6f0b2007-04-10 16:50:31 +0000158
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000159 /**
Stuart McCulloch99f4aa62009-02-17 13:40:44 +0000160 * Use locally patched version for now.
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000161 */
Stuart McCulloch99f4aa62009-02-17 13:40:44 +0000162 private Maven2OsgiConverter m_maven2OsgiConverter = new DefaultMaven2OsgiConverter();
Richard S. Hall6cf6f0b2007-04-10 16:50:31 +0000163
Stuart McCulloch378e76c2009-01-30 09:51:35 +0000164 /**
165 * The archive configuration to use.
166 *
167 * @parameter
168 */
169 private MavenArchiveConfiguration archive; // accessed indirectly in JarPluginConfiguration
170
Stuart McCulloch6d4c8372009-02-16 06:49:21 +0000171 private static final String MAVEN_SYMBOLICNAME = "maven-symbolicname";
Stuart McCulloch9c406b12007-08-23 09:41:08 +0000172 private static final String MAVEN_RESOURCES = "{maven-resources}";
Guillaume Nodete5f5c7a2010-11-17 08:09:42 +0000173 private static final String LOCAL_PACKAGES = "{local-packages}";
Stuart McCulloch6cb86db2008-03-15 21:38:26 +0000174
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000175 private static final String[] EMPTY_STRING_ARRAY =
176 {};
177 private static final String[] DEFAULT_INCLUDES =
178 { "**/**" };
Stuart McCulloch9c406b12007-08-23 09:41:08 +0000179
180
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000181 protected Maven2OsgiConverter getMaven2OsgiConverter()
182 {
Stuart McCulloch9366a822008-01-29 07:45:57 +0000183 return m_maven2OsgiConverter;
Richard S. Hall5b521ff2007-04-11 14:25:20 +0000184 }
Richard S. Hallbec8dc42007-05-18 14:33:55 +0000185
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000186
Stuart McCulloch9366a822008-01-29 07:45:57 +0000187 protected void setMaven2OsgiConverter( Maven2OsgiConverter maven2OsgiConverter )
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000188 {
Stuart McCulloch9366a822008-01-29 07:45:57 +0000189 m_maven2OsgiConverter = maven2OsgiConverter;
Richard S. Hallbec8dc42007-05-18 14:33:55 +0000190 }
191
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000192
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000193 protected MavenProject getProject()
194 {
Stuart McCulloch9366a822008-01-29 07:45:57 +0000195 return project;
Richard S. Hallf3120092006-10-31 17:32:16 +0000196 }
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000197
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000198
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000199 /**
200 * @see org.apache.maven.plugin.AbstractMojo#execute()
201 */
202 public void execute() throws MojoExecutionException
203 {
204 Properties properties = new Properties();
Stuart McCulloch3a965b92008-03-24 02:34:11 +0000205 String projectType = getProject().getArtifact().getType();
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000206
Stuart McCulloch3a965b92008-03-24 02:34:11 +0000207 // ignore unsupported project types, useful when bundleplugin is configured in parent pom
208 if ( !supportedProjectTypes.contains( projectType ) )
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000209 {
Stuart McCulloch464dbac2008-08-03 09:47:04 +0000210 getLog().warn(
211 "Ignoring project type " + projectType + " - supportedProjectTypes = " + supportedProjectTypes );
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000212 return;
213 }
214
Stuart McCulloch9366a822008-01-29 07:45:57 +0000215 execute( getProject(), instructions, properties );
Richard S. Hallf3120092006-10-31 17:32:16 +0000216 }
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000217
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000218
Stuart McCulloch83e0da02008-01-29 08:25:27 +0000219 protected void execute( MavenProject currentProject, Map originalInstructions, Properties properties )
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000220 throws MojoExecutionException
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000221 {
222 try
223 {
Stuart McCulloch83e0da02008-01-29 08:25:27 +0000224 execute( currentProject, originalInstructions, properties, getClasspath( currentProject ) );
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000225 }
226 catch ( IOException e )
227 {
Stuart McCulloch9366a822008-01-29 07:45:57 +0000228 throw new MojoExecutionException( "Error calculating classpath for project " + currentProject, e );
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000229 }
Richard S. Hallf3120092006-10-31 17:32:16 +0000230 }
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000231
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000232
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000233 /* transform directives from their XML form to the expected BND syntax (eg. _include becomes -include) */
Stuart McCulloch9366a822008-01-29 07:45:57 +0000234 protected static Map transformDirectives( Map originalInstructions )
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000235 {
Stuart McCullochd00f9712009-07-13 10:06:47 +0000236 Map transformedInstructions = new LinkedHashMap();
Stuart McCulloch9366a822008-01-29 07:45:57 +0000237 for ( Iterator i = originalInstructions.entrySet().iterator(); i.hasNext(); )
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000238 {
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000239 Map.Entry e = ( Map.Entry ) i.next();
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000240
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000241 String key = ( String ) e.getKey();
242 if ( key.startsWith( "_" ) )
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000243 {
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000244 key = "-" + key.substring( 1 );
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000245 }
246
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000247 String value = ( String ) e.getValue();
248 if ( null == value )
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000249 {
250 value = "";
251 }
Richard S. Hall6be327f2007-08-13 16:47:27 +0000252 else
253 {
Guillaume Nodet4eb49be2010-02-19 23:10:08 +0000254 value = value.replaceAll( "\\p{Blank}*[\r\n]\\p{Blank}*", "" );
Richard S. Hall6be327f2007-08-13 16:47:27 +0000255 }
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000256
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000257 transformedInstructions.put( key, value );
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000258 }
259 return transformedInstructions;
Richard S. Hall6cf6f0b2007-04-10 16:50:31 +0000260 }
Richard S. Hall6cf6f0b2007-04-10 16:50:31 +0000261
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000262
Stuart McCulloch83e0da02008-01-29 08:25:27 +0000263 protected void execute( MavenProject currentProject, Map originalInstructions, Properties properties,
264 Jar[] classpath ) throws MojoExecutionException
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000265 {
266 try
267 {
Stuart McCulloch9366a822008-01-29 07:45:57 +0000268 File jarFile = new File( getBuildDirectory(), getBundleName( currentProject ) );
Richard S. Hall6cf6f0b2007-04-10 16:50:31 +0000269
Stuart McCulloch1a42bfa2008-01-30 07:50:33 +0000270 Builder builder = buildOSGiBundle( currentProject, originalInstructions, properties, classpath );
Stuart McCulloch48fcb1e2008-01-30 06:58:34 +0000271
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000272 List errors = builder.getErrors();
273 List warnings = builder.getWarnings();
Richard S. Hall6cf6f0b2007-04-10 16:50:31 +0000274
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000275 for ( Iterator w = warnings.iterator(); w.hasNext(); )
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000276 {
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000277 String msg = ( String ) w.next();
Stuart McCulloch9366a822008-01-29 07:45:57 +0000278 getLog().warn( "Warning building bundle " + currentProject.getArtifact() + " : " + msg );
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000279 }
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000280 for ( Iterator e = errors.iterator(); e.hasNext(); )
Stuart McCulloch6d3e62a2007-09-21 15:07:36 +0000281 {
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000282 String msg = ( String ) e.next();
Stuart McCulloch9366a822008-01-29 07:45:57 +0000283 getLog().error( "Error building bundle " + currentProject.getArtifact() + " : " + msg );
Stuart McCulloch6d3e62a2007-09-21 15:07:36 +0000284 }
285
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000286 if ( errors.size() > 0 )
Stuart McCulloch6d3e62a2007-09-21 15:07:36 +0000287 {
Stuart McCulloch84bf81e2008-12-03 14:07:08 +0000288 String failok = builder.getProperty( "-failok" );
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000289 if ( null == failok || "false".equalsIgnoreCase( failok ) )
Stuart McCulloch6d3e62a2007-09-21 15:07:36 +0000290 {
291 jarFile.delete();
292
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000293 throw new MojoFailureException( "Error(s) found in bundle configuration" );
Stuart McCulloch6d3e62a2007-09-21 15:07:36 +0000294 }
295 }
296
Stuart McCulloch48fcb1e2008-01-30 06:58:34 +0000297 // attach bundle to maven project
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000298 jarFile.getParentFile().mkdirs();
299 builder.getJar().write( jarFile );
Stuart McCulloch2d301732008-02-18 09:01:17 +0000300
301 Artifact mainArtifact = currentProject.getArtifact();
302
303 // workaround for MNG-1682: force maven to install artifact using the "jar" handler
304 mainArtifact.setArtifactHandler( m_artifactHandlerManager.getArtifactHandler( "jar" ) );
305
306 if ( null == classifier || classifier.trim().length() == 0 )
307 {
308 mainArtifact.setFile( jarFile );
309 }
310 else
311 {
312 m_projectHelper.attachArtifact( currentProject, jarFile, classifier );
313 }
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000314
315 if ( unpackBundle )
Stuart McCullochdd0d9ba2007-12-07 08:22:20 +0000316 {
Stuart McCulloch48fcb1e2008-01-30 06:58:34 +0000317 unpackBundle( jarFile );
Stuart McCullochdd0d9ba2007-12-07 08:22:20 +0000318 }
319
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000320 if ( manifestLocation != null )
Stuart McCulloch6d3e62a2007-09-21 15:07:36 +0000321 {
322 File outputFile = new File( manifestLocation, "MANIFEST.MF" );
323
324 try
325 {
326 Manifest manifest = builder.getJar().getManifest();
327 ManifestPlugin.writeManifest( manifest, outputFile );
328 }
329 catch ( IOException e )
330 {
331 getLog().error( "Error trying to write Manifest to file " + outputFile, e );
332 }
333 }
Stuart McCullochd8a04c52008-08-06 16:05:23 +0000334
335 // cleanup...
336 builder.close();
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000337 }
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000338 catch ( MojoFailureException e )
Stuart McCulloch6787ce42007-10-26 08:07:14 +0000339 {
340 getLog().error( e.getLocalizedMessage() );
341 throw new MojoExecutionException( "Error(s) found in bundle configuration", e );
342 }
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000343 catch ( Exception e )
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000344 {
Stuart McCulloch6787ce42007-10-26 08:07:14 +0000345 getLog().error( "An internal error occurred", e );
346 throw new MojoExecutionException( "Internal error in maven-bundle-plugin", e );
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000347 }
348 }
Richard S. Hall6cf6f0b2007-04-10 16:50:31 +0000349
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000350
Stuart McCulloch1a42bfa2008-01-30 07:50:33 +0000351 protected Builder buildOSGiBundle( MavenProject currentProject, Map originalInstructions, Properties properties,
352 Jar[] classpath ) throws Exception
353 {
354 properties.putAll( getDefaultProperties( currentProject ) );
Stuart McCulloch1a42bfa2008-01-30 07:50:33 +0000355 properties.putAll( transformDirectives( originalInstructions ) );
356
Stuart McCulloch1a42bfa2008-01-30 07:50:33 +0000357 Builder builder = new Builder();
358 builder.setBase( currentProject.getBasedir() );
359 builder.setProperties( properties );
360 builder.setClasspath( classpath );
361
Stuart McCullocha8cfd1b2008-04-01 06:54:26 +0000362 // update BND instructions to add included Maven resources
Stuart McCulloch84bf81e2008-12-03 14:07:08 +0000363 includeMavenResources( currentProject, builder, getLog() );
Stuart McCullocha8cfd1b2008-04-01 06:54:26 +0000364
Stuart McCullochb371e7e2009-02-17 11:58:12 +0000365 // calculate default export/private settings based on sources
Hiram R. Chirino4f5f2272011-01-27 18:32:03 +0000366 addLocalPackages( outputDirectory, builder );
Stuart McCulloch9c4a94d2008-01-30 09:56:30 +0000367
Stuart McCulloch1a42bfa2008-01-30 07:50:33 +0000368 // update BND instructions to embed selected Maven dependencies
Stuart McCulloch84bf81e2008-12-03 14:07:08 +0000369 Collection embeddableArtifacts = getEmbeddableArtifacts( currentProject, builder );
Stuart McCullochcb2a3b22009-09-02 17:44:00 +0000370 new DependencyEmbedder( getLog(), embeddableArtifacts ).processHeaders( builder );
Stuart McCulloch1a42bfa2008-01-30 07:50:33 +0000371
Stuart McCulloche8125182008-12-05 10:41:46 +0000372 dumpInstructions( "BND Instructions:", builder.getProperties(), getLog() );
Stuart McCulloch53e42e12009-01-29 08:45:51 +0000373 dumpClasspath( "BND Classpath:", builder.getClasspath(), getLog() );
Stuart McCulloche8125182008-12-05 10:41:46 +0000374
Stuart McCulloch1a42bfa2008-01-30 07:50:33 +0000375 builder.build();
376 Jar jar = builder.getJar();
Stuart McCulloche8125182008-12-05 10:41:46 +0000377
378 dumpManifest( "BND Manifest:", jar.getManifest(), getLog() );
Stuart McCulloch1a42bfa2008-01-30 07:50:33 +0000379
Richard S. Hall2c9e5922010-10-25 19:07:06 +0000380 String[] removeHeaders = builder.getProperty( Constants.REMOVEHEADERS, "" ).split( "," );
Stuart McCulloche8125182008-12-05 10:41:46 +0000381
Stuart McCulloche19d4092008-01-30 08:42:48 +0000382 mergeMavenManifest( currentProject, jar, removeHeaders, getLog() );
Stuart McCulloche8125182008-12-05 10:41:46 +0000383 builder.setJar( jar );
384
385 dumpManifest( "Final Manifest:", jar.getManifest(), getLog() );
Stuart McCulloch1a42bfa2008-01-30 07:50:33 +0000386
387 return builder;
388 }
389
390
Stuart McCulloche8125182008-12-05 10:41:46 +0000391 protected static void dumpInstructions( String title, Properties properties, Log log )
392 {
393 if ( log.isDebugEnabled() )
394 {
395 log.debug( title );
396 log.debug( "------------------------------------------------------------------------" );
397 for ( Enumeration e = properties.propertyNames(); e.hasMoreElements(); )
398 {
Stuart McCulloch378e76c2009-01-30 09:51:35 +0000399 String key = ( String ) e.nextElement();
Stuart McCulloche8125182008-12-05 10:41:46 +0000400 log.debug( key + ": " + properties.getProperty( key ) );
401 }
402 log.debug( "------------------------------------------------------------------------" );
403 }
404 }
405
406
Stuart McCulloch53e42e12009-01-29 08:45:51 +0000407 protected static void dumpClasspath( String title, List classpath, Log log )
408 {
409 if ( log.isDebugEnabled() )
410 {
411 log.debug( title );
412 log.debug( "------------------------------------------------------------------------" );
413 for ( Iterator i = classpath.iterator(); i.hasNext(); )
414 {
Stuart McCulloch378e76c2009-01-30 09:51:35 +0000415 File path = ( ( Jar ) i.next() ).getSource();
Stuart McCulloch53e42e12009-01-29 08:45:51 +0000416 log.debug( null == path ? "null" : path.toString() );
417 }
418 log.debug( "------------------------------------------------------------------------" );
419 }
420 }
421
422
Stuart McCulloche8125182008-12-05 10:41:46 +0000423 protected static void dumpManifest( String title, Manifest manifest, Log log )
424 {
425 if ( log.isDebugEnabled() )
426 {
427 log.debug( title );
428 log.debug( "------------------------------------------------------------------------" );
429 for ( Iterator i = manifest.getMainAttributes().entrySet().iterator(); i.hasNext(); )
430 {
Stuart McCulloch378e76c2009-01-30 09:51:35 +0000431 Map.Entry entry = ( Map.Entry ) i.next();
Stuart McCulloche8125182008-12-05 10:41:46 +0000432 log.debug( entry.getKey() + ": " + entry.getValue() );
433 }
434 log.debug( "------------------------------------------------------------------------" );
435 }
436 }
437
438
Stuart McCulloch84bf81e2008-12-03 14:07:08 +0000439 protected static void includeMavenResources( MavenProject currentProject, Analyzer analyzer, Log log )
Stuart McCulloch48fcb1e2008-01-30 06:58:34 +0000440 {
441 // pass maven resource paths onto BND analyzer
442 final String mavenResourcePaths = getMavenResourcePaths( currentProject );
Stuart McCulloch84bf81e2008-12-03 14:07:08 +0000443 final String includeResource = ( String ) analyzer.getProperty( Analyzer.INCLUDE_RESOURCE );
Stuart McCulloch48fcb1e2008-01-30 06:58:34 +0000444 if ( includeResource != null )
445 {
446 if ( includeResource.indexOf( MAVEN_RESOURCES ) >= 0 )
447 {
448 // if there is no maven resource path, we do a special treatment and replace
449 // every occurance of MAVEN_RESOURCES and a following comma with an empty string
450 if ( mavenResourcePaths.length() == 0 )
451 {
452 String cleanedResource = removeTagFromInstruction( includeResource, MAVEN_RESOURCES );
453 if ( cleanedResource.length() > 0 )
454 {
Stuart McCulloch84bf81e2008-12-03 14:07:08 +0000455 analyzer.setProperty( Analyzer.INCLUDE_RESOURCE, cleanedResource );
Stuart McCulloch48fcb1e2008-01-30 06:58:34 +0000456 }
457 else
458 {
Stuart McCulloch84bf81e2008-12-03 14:07:08 +0000459 analyzer.unsetProperty( Analyzer.INCLUDE_RESOURCE );
Stuart McCulloch48fcb1e2008-01-30 06:58:34 +0000460 }
461 }
462 else
463 {
Stuart McCulloch464dbac2008-08-03 09:47:04 +0000464 String combinedResource = StringUtils
465 .replace( includeResource, MAVEN_RESOURCES, mavenResourcePaths );
Stuart McCulloch84bf81e2008-12-03 14:07:08 +0000466 analyzer.setProperty( Analyzer.INCLUDE_RESOURCE, combinedResource );
Stuart McCulloch48fcb1e2008-01-30 06:58:34 +0000467 }
468 }
469 else if ( mavenResourcePaths.length() > 0 )
470 {
471 log.warn( Analyzer.INCLUDE_RESOURCE + ": overriding " + mavenResourcePaths + " with " + includeResource
472 + " (add " + MAVEN_RESOURCES + " if you want to include the maven resources)" );
473 }
474 }
475 else if ( mavenResourcePaths.length() > 0 )
476 {
Stuart McCulloch84bf81e2008-12-03 14:07:08 +0000477 analyzer.setProperty( Analyzer.INCLUDE_RESOURCE, mavenResourcePaths );
Stuart McCulloch48fcb1e2008-01-30 06:58:34 +0000478 }
479 }
480
481
Stuart McCulloch378e76c2009-01-30 09:51:35 +0000482 protected void mergeMavenManifest( MavenProject currentProject, Jar jar, String[] removeHeaders, Log log )
483 throws IOException
Stuart McCulloch48fcb1e2008-01-30 06:58:34 +0000484 {
Stuart McCulloch378e76c2009-01-30 09:51:35 +0000485 boolean addMavenDescriptor = true;
486
Stuart McCulloch48fcb1e2008-01-30 06:58:34 +0000487 try
488 {
489 /*
490 * Grab customized manifest entries from the maven-jar-plugin configuration
491 */
492 MavenArchiveConfiguration archiveConfig = JarPluginConfiguration.getArchiveConfiguration( currentProject );
493 String mavenManifestText = new MavenArchiver().getManifest( currentProject, archiveConfig ).toString();
Stuart McCulloch378e76c2009-01-30 09:51:35 +0000494 addMavenDescriptor = archiveConfig.isAddMavenDescriptor();
495
Stuart McCulloch48fcb1e2008-01-30 06:58:34 +0000496 Manifest mavenManifest = new Manifest();
497
498 // First grab the external manifest file (if specified)
499 File externalManifestFile = archiveConfig.getManifestFile();
500 if ( null != externalManifestFile && externalManifestFile.exists() )
501 {
502 InputStream mis = new FileInputStream( externalManifestFile );
503 mavenManifest.read( mis );
504 mis.close();
505 }
506
507 // Then apply the customized entries from the jar plugin
508 mavenManifest.read( new StringInputStream( mavenManifestText ) );
509
510 if ( !archiveConfig.isManifestSectionsEmpty() )
511 {
512 /*
513 * Add customized manifest sections (for some reason MavenArchiver doesn't do this for us)
514 */
515 List sections = archiveConfig.getManifestSections();
516 for ( Iterator i = sections.iterator(); i.hasNext(); )
517 {
518 ManifestSection section = ( ManifestSection ) i.next();
519 Attributes attributes = new Attributes();
520
521 if ( !section.isManifestEntriesEmpty() )
522 {
523 Map entries = section.getManifestEntries();
524 for ( Iterator j = entries.entrySet().iterator(); j.hasNext(); )
525 {
526 Map.Entry entry = ( Map.Entry ) j.next();
527 attributes.putValue( ( String ) entry.getKey(), ( String ) entry.getValue() );
528 }
529 }
530
531 mavenManifest.getEntries().put( section.getName(), attributes );
532 }
533 }
534
Stuart McCulloche19d4092008-01-30 08:42:48 +0000535 Attributes mainMavenAttributes = mavenManifest.getMainAttributes();
536 mainMavenAttributes.putValue( "Created-By", "Apache Maven Bundle Plugin" );
537
538 // apply -removeheaders to the custom manifest
539 for ( int i = 0; i < removeHeaders.length; i++ )
540 {
541 for ( Iterator j = mainMavenAttributes.keySet().iterator(); j.hasNext(); )
542 {
543 if ( j.next().toString().matches( removeHeaders[i].trim() ) )
544 {
545 j.remove();
546 }
547 }
548 }
549
Stuart McCulloch48fcb1e2008-01-30 06:58:34 +0000550 /*
551 * Overlay generated bundle manifest with customized entries
552 */
553 Manifest bundleManifest = jar.getManifest();
Stuart McCulloche19d4092008-01-30 08:42:48 +0000554 bundleManifest.getMainAttributes().putAll( mainMavenAttributes );
Stuart McCulloch48fcb1e2008-01-30 06:58:34 +0000555 bundleManifest.getEntries().putAll( mavenManifest.getEntries() );
Hiram R. Chirino2d401a32011-01-27 20:58:44 +0000556
557 // adjust the import package attributes so that optional dependencies use
558 // optional resolution.
559 String importPackages = bundleManifest.getMainAttributes().getValue("Import-Package");
560 if( importPackages!=null ) {
561 Set optionalPackages = getOptionalPackages(currentProject);
562
563 Map<String, Map<String, String>> values = new Analyzer().parseHeader(importPackages);
564 for (Map.Entry<String, Map<String, String>> entry: values.entrySet()) {
565 String pkg = entry.getKey();
566 Map<String, String> options = entry.getValue();
567 if( !options.containsKey("resolution:") && optionalPackages.contains(pkg) ) {
568 options.put("resolution:", "optional");
569 }
570 }
571 String result = Processor.printClauses(values, "resolution:");
572 bundleManifest.getMainAttributes().putValue("Import-Package", result);
573 }
574
Stuart McCulloch48fcb1e2008-01-30 06:58:34 +0000575 jar.setManifest( bundleManifest );
576 }
577 catch ( Exception e )
578 {
579 log.warn( "Unable to merge Maven manifest: " + e.getLocalizedMessage() );
580 }
Stuart McCulloch378e76c2009-01-30 09:51:35 +0000581
582 if ( addMavenDescriptor )
583 {
584 doMavenMetadata( currentProject, jar );
585 }
Stuart McCulloch48fcb1e2008-01-30 06:58:34 +0000586 }
587
588
Hiram R. Chirino2d401a32011-01-27 20:58:44 +0000589 protected Set getOptionalPackages( MavenProject currentProject ) throws IOException, MojoExecutionException
590 {
Hiram R. Chirino922498b2011-01-28 15:13:33 +0000591 ArrayList inscope = new ArrayList();
Hiram R. Chirino2d401a32011-01-27 20:58:44 +0000592 final Collection artifacts = getSelectedDependencies( currentProject.getArtifacts() );
593 for ( Iterator it = artifacts.iterator(); it.hasNext(); )
594 {
595 Artifact artifact = ( Artifact ) it.next();
596 if ( artifact.getArtifactHandler().isAddedToClasspath() )
597 {
598 if ( !Artifact.SCOPE_TEST.equals( artifact.getScope() ) )
599 {
Hiram R. Chirino922498b2011-01-28 15:13:33 +0000600 inscope.add(artifact);
Hiram R. Chirino2d401a32011-01-27 20:58:44 +0000601 }
602 }
603 }
Hiram R. Chirino922498b2011-01-28 15:13:33 +0000604
605 HashSet optionalArtifactIds = new HashSet();
606 for ( Iterator it = inscope.iterator(); it.hasNext(); )
607 {
608 Artifact artifact = ( Artifact ) it.next();
609 if( artifact.isOptional() ) {
610 String id = artifact.toString();
611 if( artifact.getScope()!=null ) {
612 // strip the scope...
613 id = id.replaceFirst(":[^:]*$", "");
614 }
615 optionalArtifactIds.add(id);
616 }
617
618 }
619
620 HashSet required = new HashSet();
621 HashSet optional = new HashSet();
622 for ( Iterator it = inscope.iterator(); it.hasNext(); )
623 {
624 Artifact artifact = ( Artifact ) it.next();
625 File file = getFile( artifact );
626 if ( file == null )
627 {
628 continue;
629 }
630
631 Jar jar = new Jar( artifact.getArtifactId(), file );
632 if( isTransitivelyOptional(optionalArtifactIds, artifact) ) {
633 optional.addAll(jar.getPackages());
634 } else {
635 required.addAll(jar.getPackages());
636 }
637 jar.close();
638 }
639
Hiram R. Chirino2d401a32011-01-27 20:58:44 +0000640 optional.removeAll(required);
641 return optional;
642 }
643
Hiram R. Chirino922498b2011-01-28 15:13:33 +0000644 /**
645 * Check to see if any dependency along the dependency trail of
646 * the artifact is optional.
647 *
648 * @param artifact
649 */
650 protected boolean isTransitivelyOptional(HashSet optionalArtifactIds, Artifact artifact) {
651 List trail = artifact.getDependencyTrail();
652 for (Iterator iterator = trail.iterator(); iterator.hasNext();) {
653 String next = (String) iterator.next();
654 if( optionalArtifactIds.contains(next) ) {
655 return true;
656 }
657 }
658 return false;
659 }
660
Stuart McCulloch48fcb1e2008-01-30 06:58:34 +0000661 private void unpackBundle( File jarFile )
662 {
663 File outputDir = getOutputDirectory();
664 if ( null == outputDir )
665 {
666 outputDir = new File( getBuildDirectory(), "classes" );
667 }
668
669 try
670 {
671 /*
672 * this directory must exist before unpacking, otherwise the plexus
673 * unarchiver decides to use the current working directory instead!
674 */
675 if ( !outputDir.exists() )
676 {
677 outputDir.mkdirs();
678 }
679
680 UnArchiver unArchiver = m_archiverManager.getUnArchiver( "jar" );
681 unArchiver.setDestDirectory( outputDir );
682 unArchiver.setSourceFile( jarFile );
683 unArchiver.extract();
684 }
685 catch ( Exception e )
686 {
687 getLog().error( "Problem unpacking " + jarFile + " to " + outputDir, e );
688 }
689 }
690
691
Stuart McCulloch5abb4c22008-01-29 11:26:33 +0000692 protected static String removeTagFromInstruction( String instruction, String tag )
Stuart McCulloch00b06942007-09-24 14:14:09 +0000693 {
694 StringBuffer buf = new StringBuffer();
695
Stuart McCulloch5abb4c22008-01-29 11:26:33 +0000696 String[] clauses = instruction.split( "," );
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000697 for ( int i = 0; i < clauses.length; i++ )
Stuart McCulloch00b06942007-09-24 14:14:09 +0000698 {
699 String clause = clauses[i].trim();
Stuart McCulloch5abb4c22008-01-29 11:26:33 +0000700 if ( !tag.equals( clause ) )
Stuart McCulloch00b06942007-09-24 14:14:09 +0000701 {
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000702 if ( buf.length() > 0 )
Stuart McCulloch00b06942007-09-24 14:14:09 +0000703 {
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000704 buf.append( ',' );
Stuart McCulloch00b06942007-09-24 14:14:09 +0000705 }
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000706 buf.append( clause );
Stuart McCulloch00b06942007-09-24 14:14:09 +0000707 }
708 }
709
710 return buf.toString();
711 }
712
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000713
Stuart McCullochf806f862008-02-04 07:57:09 +0000714 private static Map getProperties( Model projectModel, String prefix )
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000715 {
Stuart McCullochd00f9712009-07-13 10:06:47 +0000716 Map properties = new LinkedHashMap();
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000717 Method methods[] = Model.class.getDeclaredMethods();
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000718 for ( int i = 0; i < methods.length; i++ )
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000719 {
720 String name = methods[i].getName();
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000721 if ( name.startsWith( "get" ) )
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000722 {
723 try
724 {
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000725 Object v = methods[i].invoke( projectModel, null );
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000726 if ( v != null )
727 {
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000728 name = prefix + Character.toLowerCase( name.charAt( 3 ) ) + name.substring( 4 );
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000729 if ( v.getClass().isArray() )
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000730 properties.put( name, Arrays.asList( ( Object[] ) v ).toString() );
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000731 else
732 properties.put( name, v );
Richard S. Hall420d2782007-06-14 12:31:48 +0000733
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000734 }
735 }
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000736 catch ( Exception e )
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000737 {
738 // too bad
739 }
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000740 }
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000741 }
742 return properties;
743 }
Richard S. Hall46e7a852007-05-17 14:47:48 +0000744
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000745
Stuart McCulloch9366a822008-01-29 07:45:57 +0000746 private static StringBuffer printLicenses( List licenses )
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000747 {
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000748 if ( licenses == null || licenses.size() == 0 )
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000749 return null;
750 StringBuffer sb = new StringBuffer();
751 String del = "";
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000752 for ( Iterator i = licenses.iterator(); i.hasNext(); )
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000753 {
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000754 License l = ( License ) i.next();
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000755 String url = l.getUrl();
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000756 if ( url == null )
757 continue;
758 sb.append( del );
759 sb.append( url );
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000760 del = ", ";
761 }
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000762 if ( sb.length() == 0 )
763 return null;
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000764 return sb;
765 }
Richard S. Hall46e7a852007-05-17 14:47:48 +0000766
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000767
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000768 /**
769 * @param jar
770 * @throws IOException
771 */
Stuart McCulloch9366a822008-01-29 07:45:57 +0000772 private void doMavenMetadata( MavenProject currentProject, Jar jar ) throws IOException
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000773 {
Stuart McCulloch9366a822008-01-29 07:45:57 +0000774 String path = "META-INF/maven/" + currentProject.getGroupId() + "/" + currentProject.getArtifactId();
775 File pomFile = new File( baseDir, "pom.xml" );
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000776 jar.putResource( path + "/pom.xml", new FileResource( pomFile ) );
Richard S. Hall46e7a852007-05-17 14:47:48 +0000777
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000778 Properties p = new Properties();
Stuart McCulloch9366a822008-01-29 07:45:57 +0000779 p.put( "version", currentProject.getVersion() );
780 p.put( "groupId", currentProject.getGroupId() );
781 p.put( "artifactId", currentProject.getArtifactId() );
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000782 ByteArrayOutputStream out = new ByteArrayOutputStream();
Stuart McCulloch9366a822008-01-29 07:45:57 +0000783 p.store( out, "Generated by org.apache.felix.bundleplugin" );
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000784 jar
785 .putResource( path + "/pom.properties",
786 new EmbeddedResource( out.toByteArray(), System.currentTimeMillis() ) );
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000787 }
Richard S. Hall46e7a852007-05-17 14:47:48 +0000788
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000789
Stuart McCulloch888d6da2009-02-16 08:25:03 +0000790 protected Jar[] getClasspath( MavenProject currentProject ) throws IOException, MojoExecutionException
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000791 {
792 List list = new ArrayList();
Richard S. Hall46e7a852007-05-17 14:47:48 +0000793
Stuart McCulloch9366a822008-01-29 07:45:57 +0000794 if ( getOutputDirectory() != null && getOutputDirectory().exists() )
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000795 {
Stuart McCulloch9366a822008-01-29 07:45:57 +0000796 list.add( new Jar( ".", getOutputDirectory() ) );
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000797 }
Richard S. Hall46e7a852007-05-17 14:47:48 +0000798
Stuart McCulloch5ffc3fe2008-02-20 05:37:57 +0000799 final Collection artifacts = getSelectedDependencies( currentProject.getArtifacts() );
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000800 for ( Iterator it = artifacts.iterator(); it.hasNext(); )
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000801 {
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000802 Artifact artifact = ( Artifact ) it.next();
803 if ( artifact.getArtifactHandler().isAddedToClasspath() )
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000804 {
Stuart McCullochf7d0c7c2009-01-29 07:23:06 +0000805 if ( !Artifact.SCOPE_TEST.equals( artifact.getScope() ) )
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000806 {
Stuart McCulloch9366a822008-01-29 07:45:57 +0000807 File file = getFile( artifact );
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000808 if ( file == null )
Stuart McCulloch55fcffd2007-08-22 03:19:15 +0000809 {
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000810 getLog().warn(
Stuart McCulloch9366a822008-01-29 07:45:57 +0000811 "File is not available for artifact " + artifact + " in project "
812 + currentProject.getArtifact() );
Stuart McCullocha719bc02007-09-25 13:48:58 +0000813 continue;
Stuart McCulloch55fcffd2007-08-22 03:19:15 +0000814 }
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000815 Jar jar = new Jar( artifact.getArtifactId(), file );
816 list.add( jar );
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000817 }
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000818 }
819 }
820 Jar[] cp = new Jar[list.size()];
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000821 list.toArray( cp );
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000822 return cp;
823 }
Richard S. Hall46e7a852007-05-17 14:47:48 +0000824
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000825
Stuart McCulloch888d6da2009-02-16 08:25:03 +0000826 private Collection getSelectedDependencies( Collection artifacts ) throws MojoExecutionException
Stuart McCulloch5ffc3fe2008-02-20 05:37:57 +0000827 {
828 if ( null == excludeDependencies || excludeDependencies.length() == 0 )
829 {
830 return artifacts;
831 }
832 else if ( "true".equalsIgnoreCase( excludeDependencies ) )
833 {
834 return Collections.EMPTY_LIST;
835 }
836
Stuart McCullochd00f9712009-07-13 10:06:47 +0000837 Collection selectedDependencies = new LinkedHashSet( artifacts );
Stuart McCulloch888d6da2009-02-16 08:25:03 +0000838 DependencyExcluder excluder = new DependencyExcluder( artifacts );
839 excluder.processHeaders( excludeDependencies );
840 selectedDependencies.removeAll( excluder.getExcludedArtifacts() );
Stuart McCulloch5ffc3fe2008-02-20 05:37:57 +0000841
Stuart McCulloch888d6da2009-02-16 08:25:03 +0000842 return selectedDependencies;
Stuart McCulloch5ffc3fe2008-02-20 05:37:57 +0000843 }
844
845
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000846 /**
847 * Get the file for an Artifact
848 *
849 * @param artifact
850 */
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000851 protected File getFile( Artifact artifact )
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000852 {
853 return artifact.getFile();
854 }
Richard S. Hall46e7a852007-05-17 14:47:48 +0000855
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000856
Stuart McCulloch9366a822008-01-29 07:45:57 +0000857 private static void header( Properties properties, String key, Object value )
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000858 {
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000859 if ( value == null )
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000860 return;
Richard S. Hall46e7a852007-05-17 14:47:48 +0000861
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000862 if ( value instanceof Collection && ( ( Collection ) value ).isEmpty() )
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000863 return;
864
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000865 properties.put( key, value.toString().replaceAll( "[\r\n]", "" ) );
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000866 }
867
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000868
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000869 /**
870 * Convert a Maven version into an OSGi compliant version
871 *
872 * @param version Maven version
873 * @return the OSGi version
874 */
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000875 protected String convertVersionToOsgi( String version )
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000876 {
Stuart McCulloch9366a822008-01-29 07:45:57 +0000877 return getMaven2OsgiConverter().getVersion( version );
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000878 }
879
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000880
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000881 /**
882 * TODO this should return getMaven2Osgi().getBundleFileName( project.getArtifact() )
883 */
Stuart McCulloch9366a822008-01-29 07:45:57 +0000884 protected String getBundleName( MavenProject currentProject )
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000885 {
Stuart McCulloch0491afa2008-02-19 05:06:41 +0000886 String finalName = currentProject.getBuild().getFinalName();
887 if ( null != classifier && classifier.trim().length() > 0 )
888 {
889 return finalName + '-' + classifier + ".jar";
890 }
Stuart McCulloch0491afa2008-02-19 05:06:41 +0000891 return finalName + ".jar";
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000892 }
893
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000894
Stuart McCulloch1a862e92007-10-16 14:13:49 +0000895 protected String getBuildDirectory()
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000896 {
Stuart McCulloch9366a822008-01-29 07:45:57 +0000897 return buildDirectory;
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000898 }
899
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000900
Stuart McCulloch9366a822008-01-29 07:45:57 +0000901 protected void setBuildDirectory( String _buildirectory )
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000902 {
Stuart McCulloch9366a822008-01-29 07:45:57 +0000903 buildDirectory = _buildirectory;
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000904 }
905
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000906
Stuart McCulloch9366a822008-01-29 07:45:57 +0000907 protected Properties getDefaultProperties( MavenProject currentProject )
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000908 {
909 Properties properties = new Properties();
Stuart McCulloch02afa5f2007-10-04 17:23:30 +0000910
911 String bsn;
912 try
913 {
Stuart McCulloch9366a822008-01-29 07:45:57 +0000914 bsn = getMaven2OsgiConverter().getBundleSymbolicName( currentProject.getArtifact() );
Stuart McCulloch02afa5f2007-10-04 17:23:30 +0000915 }
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000916 catch ( Exception e )
Stuart McCulloch02afa5f2007-10-04 17:23:30 +0000917 {
Stuart McCulloch9366a822008-01-29 07:45:57 +0000918 bsn = currentProject.getGroupId() + "." + currentProject.getArtifactId();
Stuart McCulloch02afa5f2007-10-04 17:23:30 +0000919 }
920
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000921 // Setup defaults
Stuart McCulloch6d4c8372009-02-16 06:49:21 +0000922 properties.put( MAVEN_SYMBOLICNAME, bsn );
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000923 properties.put( Analyzer.BUNDLE_SYMBOLICNAME, bsn );
924 properties.put( Analyzer.IMPORT_PACKAGE, "*" );
Guillaume Nodet6da22542010-03-05 14:12:41 +0000925 properties.put( Analyzer.BUNDLE_VERSION, getMaven2OsgiConverter().getVersion(currentProject.getVersion()) );
Stuart McCulloch02afa5f2007-10-04 17:23:30 +0000926
Stuart McCullochc966ef62009-09-02 18:02:59 +0000927 // remove the extraneous Include-Resource and Private-Package entries from generated manifest
Richard S. Hall2c9e5922010-10-25 19:07:06 +0000928 properties.put( Constants.REMOVEHEADERS, Analyzer.INCLUDE_RESOURCE + ',' + Analyzer.PRIVATE_PACKAGE );
Stuart McCulloch8f21a4a2008-01-09 17:08:32 +0000929
Stuart McCulloch9366a822008-01-29 07:45:57 +0000930 header( properties, Analyzer.BUNDLE_DESCRIPTION, currentProject.getDescription() );
931 StringBuffer licenseText = printLicenses( currentProject.getLicenses() );
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000932 if ( licenseText != null )
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000933 {
Stuart McCulloch9366a822008-01-29 07:45:57 +0000934 header( properties, Analyzer.BUNDLE_LICENSE, licenseText );
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000935 }
Stuart McCulloch9366a822008-01-29 07:45:57 +0000936 header( properties, Analyzer.BUNDLE_NAME, currentProject.getName() );
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000937
Stuart McCulloch9366a822008-01-29 07:45:57 +0000938 if ( currentProject.getOrganization() != null )
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000939 {
Stuart McCulloch6dd4dd22009-09-02 22:45:33 +0000940 String organizationName = currentProject.getOrganization().getName();
941 header( properties, Analyzer.BUNDLE_VENDOR, organizationName );
942 properties.put( "project.organization.name", organizationName );
943 properties.put( "pom.organization.name", organizationName );
Stuart McCulloch9366a822008-01-29 07:45:57 +0000944 if ( currentProject.getOrganization().getUrl() != null )
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000945 {
Stuart McCulloch6dd4dd22009-09-02 22:45:33 +0000946 String organizationUrl = currentProject.getOrganization().getUrl();
947 header( properties, Analyzer.BUNDLE_DOCURL, organizationUrl );
948 properties.put( "project.organization.url", organizationUrl );
949 properties.put( "pom.organization.url", organizationUrl );
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000950 }
951 }
952
Stuart McCulloch9366a822008-01-29 07:45:57 +0000953 properties.putAll( currentProject.getProperties() );
954 properties.putAll( currentProject.getModel().getProperties() );
Stuart McCullochf806f862008-02-04 07:57:09 +0000955 properties.putAll( getProperties( currentProject.getModel(), "project.build." ) );
956 properties.putAll( getProperties( currentProject.getModel(), "pom." ) );
957 properties.putAll( getProperties( currentProject.getModel(), "project." ) );
Stuart McCulloch9366a822008-01-29 07:45:57 +0000958 properties.put( "project.baseDir", baseDir );
959 properties.put( "project.build.directory", getBuildDirectory() );
960 properties.put( "project.build.outputdirectory", getOutputDirectory() );
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000961
Stuart McCulloch1d3a0a02008-04-30 05:38:03 +0000962 properties.put( "classifier", classifier == null ? "" : classifier );
963
Guillaume Nodet459eaed2009-09-02 21:15:28 +0000964 // Add default plugins
965 header( properties, Analyzer.PLUGIN,
Guillaume Nodet09157a92010-03-10 17:40:45 +0000966 BlueprintPlugin.class.getName() + ","
967 + SpringXMLType.class.getName());
Guillaume Nodet459eaed2009-09-02 21:15:28 +0000968
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000969 return properties;
970 }
971
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000972
Stuart McCulloch9366a822008-01-29 07:45:57 +0000973 protected void setBasedir( File _basedir )
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000974 {
Stuart McCulloch9366a822008-01-29 07:45:57 +0000975 baseDir = _basedir;
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000976 }
977
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000978
Stuart McCulloch9366a822008-01-29 07:45:57 +0000979 protected File getOutputDirectory()
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000980 {
Stuart McCulloch9366a822008-01-29 07:45:57 +0000981 return outputDirectory;
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000982 }
983
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000984
Stuart McCulloch9366a822008-01-29 07:45:57 +0000985 protected void setOutputDirectory( File _outputDirectory )
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000986 {
Stuart McCulloch9366a822008-01-29 07:45:57 +0000987 outputDirectory = _outputDirectory;
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +0000988 }
989
Stuart McCulloch5ae59142008-01-29 06:21:05 +0000990
Hiram R. Chirino4f5f2272011-01-27 18:32:03 +0000991 private static void addLocalPackages( File outputDirectory, Analyzer analyzer )
Stuart McCulloch5341ef02009-02-16 09:44:47 +0000992 {
Stuart McCullochd00f9712009-07-13 10:06:47 +0000993 Collection packages = new LinkedHashSet();
Stuart McCulloch5341ef02009-02-16 09:44:47 +0000994
Hiram R. Chirino4f5f2272011-01-27 18:32:03 +0000995 if ( outputDirectory != null && outputDirectory.isDirectory() )
Stuart McCulloch5341ef02009-02-16 09:44:47 +0000996 {
Hiram R. Chirino4f5f2272011-01-27 18:32:03 +0000997 // scan classes directory for potential packages
998 DirectoryScanner scanner = new DirectoryScanner();
999 scanner.setBasedir( outputDirectory );
1000 scanner.setIncludes( new String[]
1001 { "**/*.class" } );
1002
1003 scanner.addDefaultExcludes();
1004 scanner.scan();
1005
1006 String[] paths = scanner.getIncludedFiles();
1007 for ( int i = 0; i < paths.length; i++ )
Stuart McCullochb371e7e2009-02-17 11:58:12 +00001008 {
Hiram R. Chirino4f5f2272011-01-27 18:32:03 +00001009 packages.add( getPackageName( paths[i] ) );
Stuart McCullochb371e7e2009-02-17 11:58:12 +00001010 }
Stuart McCulloch5341ef02009-02-16 09:44:47 +00001011 }
1012
Stuart McCullochb371e7e2009-02-17 11:58:12 +00001013 StringBuffer exportedPkgs = new StringBuffer();
1014 StringBuffer privatePkgs = new StringBuffer();
Hiram R. Chirino2172ef92011-01-29 01:26:36 +00001015
1016 boolean noprivatePackages = "!*".equals(analyzer.getProperty( Analyzer.PRIVATE_PACKAGE ) );
Stuart McCulloch5341ef02009-02-16 09:44:47 +00001017
1018 for ( Iterator i = packages.iterator(); i.hasNext(); )
1019 {
1020 String pkg = ( String ) i.next();
Stuart McCullochb371e7e2009-02-17 11:58:12 +00001021
1022 // mark all source packages as private by default (can be overridden by export list)
Stuart McCullochc966ef62009-09-02 18:02:59 +00001023 privatePkgs.append( pkg ).append( ";-split-package:=merge-first," );
Stuart McCullochb371e7e2009-02-17 11:58:12 +00001024
1025 // we can't export the default package (".") and we shouldn't export internal packages
Hiram R. Chirino2172ef92011-01-29 01:26:36 +00001026 if ( noprivatePackages || !( ".".equals( pkg ) || pkg.contains( ".internal" ) || pkg.contains( ".impl" ) ) )
Stuart McCulloch5341ef02009-02-16 09:44:47 +00001027 {
Guillaume Nodete5f5c7a2010-11-17 08:09:42 +00001028 if( exportedPkgs.length() > 0 )
1029 {
1030 exportedPkgs.append( ';' );
1031 }
1032 exportedPkgs.append( pkg );
Stuart McCulloch5341ef02009-02-16 09:44:47 +00001033 }
1034 }
1035
Stuart McCullochb371e7e2009-02-17 11:58:12 +00001036 if ( analyzer.getProperty( Analyzer.EXPORT_PACKAGE ) == null )
1037 {
1038 if ( analyzer.getProperty( Analyzer.EXPORT_CONTENTS ) == null )
1039 {
1040 // no -exportcontents overriding the exports, so use our computed list
1041 analyzer.setProperty( Analyzer.EXPORT_PACKAGE, exportedPkgs.toString() );
1042 }
1043 else
1044 {
1045 // leave Export-Package empty (but non-null) as we have -exportcontents
1046 analyzer.setProperty( Analyzer.EXPORT_PACKAGE, "" );
1047 }
1048 }
Guillaume Nodete5f5c7a2010-11-17 08:09:42 +00001049 else
1050 {
1051 String exported = analyzer.getProperty( Analyzer.EXPORT_PACKAGE );
1052 if( exported.indexOf( LOCAL_PACKAGES ) >= 0 )
1053 {
1054 String newExported = StringUtils.replace( exported, LOCAL_PACKAGES, exportedPkgs.toString() );
1055 analyzer.setProperty( Analyzer.EXPORT_PACKAGE, newExported );
1056
1057 }
1058 }
Stuart McCullochb371e7e2009-02-17 11:58:12 +00001059
1060 if ( analyzer.getProperty( Analyzer.PRIVATE_PACKAGE ) == null )
1061 {
1062 // if there are really no private packages then use "!*" as this will keep the Bnd Tool happy
1063 analyzer.setProperty( Analyzer.PRIVATE_PACKAGE, privatePkgs.length() == 0 ? "!*" : privatePkgs.toString() );
1064 }
Stuart McCulloch5341ef02009-02-16 09:44:47 +00001065 }
1066
1067
Stuart McCullochb371e7e2009-02-17 11:58:12 +00001068 private static String getPackageName( String filename )
Stuart McCulloch5341ef02009-02-16 09:44:47 +00001069 {
1070 int n = filename.lastIndexOf( File.separatorChar );
1071 return n < 0 ? "." : filename.substring( 0, n ).replace( File.separatorChar, '.' );
1072 }
1073
1074
Stuart McCulloch9366a822008-01-29 07:45:57 +00001075 private static String getMavenResourcePaths( MavenProject project )
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +00001076 {
Stuart McCulloch5ba746b2007-10-31 06:49:53 +00001077 final String basePath = project.getBasedir().getAbsolutePath();
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +00001078
Stuart McCulloch63682bf2009-07-13 10:59:39 +00001079 Set pathSet = new LinkedHashSet();
Stuart McCulloch5ae59142008-01-29 06:21:05 +00001080 for ( Iterator i = project.getResources().iterator(); i.hasNext(); )
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +00001081 {
Stuart McCulloch5ae59142008-01-29 06:21:05 +00001082 org.apache.maven.model.Resource resource = ( org.apache.maven.model.Resource ) i.next();
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +00001083
1084 final String sourcePath = resource.getDirectory();
1085 final String targetPath = resource.getTargetPath();
1086
1087 // ignore empty or non-local resources
Stuart McCulloch5ae59142008-01-29 06:21:05 +00001088 if ( new File( sourcePath ).exists() && ( ( targetPath == null ) || ( targetPath.indexOf( ".." ) < 0 ) ) )
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +00001089 {
Stuart McCulloch44deaef2007-11-18 04:36:21 +00001090 DirectoryScanner scanner = new DirectoryScanner();
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +00001091
Stuart McCulloch44deaef2007-11-18 04:36:21 +00001092 scanner.setBasedir( resource.getDirectory() );
1093 if ( resource.getIncludes() != null && !resource.getIncludes().isEmpty() )
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +00001094 {
Stuart McCulloch5ae59142008-01-29 06:21:05 +00001095 scanner.setIncludes( ( String[] ) resource.getIncludes().toArray( EMPTY_STRING_ARRAY ) );
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +00001096 }
1097 else
1098 {
Stuart McCulloch44deaef2007-11-18 04:36:21 +00001099 scanner.setIncludes( DEFAULT_INCLUDES );
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +00001100 }
Stuart McCulloch44deaef2007-11-18 04:36:21 +00001101
1102 if ( resource.getExcludes() != null && !resource.getExcludes().isEmpty() )
1103 {
Stuart McCulloch5ae59142008-01-29 06:21:05 +00001104 scanner.setExcludes( ( String[] ) resource.getExcludes().toArray( EMPTY_STRING_ARRAY ) );
Stuart McCulloch44deaef2007-11-18 04:36:21 +00001105 }
1106
1107 scanner.addDefaultExcludes();
1108 scanner.scan();
1109
1110 List includedFiles = Arrays.asList( scanner.getIncludedFiles() );
1111
1112 for ( Iterator j = includedFiles.iterator(); j.hasNext(); )
1113 {
Stuart McCulloch5ae59142008-01-29 06:21:05 +00001114 String name = ( String ) j.next();
Stuart McCulloch7c16d692007-11-18 05:02:09 +00001115 String path = sourcePath + '/' + name;
Stuart McCulloch44deaef2007-11-18 04:36:21 +00001116
1117 // make relative to project
Stuart McCulloch5ae59142008-01-29 06:21:05 +00001118 if ( path.startsWith( basePath ) )
Stuart McCulloch44deaef2007-11-18 04:36:21 +00001119 {
1120 if ( path.length() == basePath.length() )
1121 {
1122 path = ".";
1123 }
1124 else
1125 {
1126 path = path.substring( basePath.length() + 1 );
1127 }
1128 }
1129
1130 // replace windows backslash with a slash
1131 // this is a workaround for a problem with bnd 0.0.189
1132 if ( File.separatorChar != '/' )
1133 {
Stuart McCulloch5ae59142008-01-29 06:21:05 +00001134 name = name.replace( File.separatorChar, '/' );
1135 path = path.replace( File.separatorChar, '/' );
Stuart McCulloch44deaef2007-11-18 04:36:21 +00001136 }
1137
Stuart McCulloch7c16d692007-11-18 05:02:09 +00001138 // copy to correct place
1139 path = name + '=' + path;
Stuart McCulloch5ae59142008-01-29 06:21:05 +00001140 if ( targetPath != null )
Stuart McCulloch44deaef2007-11-18 04:36:21 +00001141 {
Stuart McCulloch7c16d692007-11-18 05:02:09 +00001142 path = targetPath + '/' + path;
Stuart McCulloch44deaef2007-11-18 04:36:21 +00001143 }
1144
Stuart McCulloch63682bf2009-07-13 10:59:39 +00001145 // use Bnd filtering?
Stuart McCulloch5ae59142008-01-29 06:21:05 +00001146 if ( resource.isFiltering() )
Stuart McCulloch44deaef2007-11-18 04:36:21 +00001147 {
Stuart McCulloch63682bf2009-07-13 10:59:39 +00001148 path = '{' + path + '}';
Stuart McCulloch44deaef2007-11-18 04:36:21 +00001149 }
Stuart McCulloch63682bf2009-07-13 10:59:39 +00001150
1151 pathSet.add( path );
Stuart McCulloch5ae59142008-01-29 06:21:05 +00001152 }
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +00001153 }
1154 }
1155
Stuart McCulloch63682bf2009-07-13 10:59:39 +00001156 StringBuffer resourcePaths = new StringBuffer();
1157 for ( Iterator i = pathSet.iterator() ; i.hasNext(); )
1158 {
1159 resourcePaths.append( i.next() );
1160 if ( i.hasNext() )
1161 {
1162 resourcePaths.append( ',' );
1163 }
1164 }
1165
Carsten Ziegeler8fb2f052007-08-01 08:28:54 +00001166 return resourcePaths.toString();
1167 }
Stuart McCullochf7c44882007-08-23 08:10:03 +00001168
Stuart McCulloch5ae59142008-01-29 06:21:05 +00001169
Stuart McCulloch888d6da2009-02-16 08:25:03 +00001170 protected Collection getEmbeddableArtifacts( MavenProject project, Analyzer analyzer )
1171 throws MojoExecutionException
Stuart McCullochf7c44882007-08-23 08:10:03 +00001172 {
Stuart McCulloch888d6da2009-02-16 08:25:03 +00001173 final Collection artifacts;
1174
Stuart McCulloch84bf81e2008-12-03 14:07:08 +00001175 String embedTransitive = analyzer.getProperty( DependencyEmbedder.EMBED_TRANSITIVE );
Stuart McCulloch5ae59142008-01-29 06:21:05 +00001176 if ( Boolean.valueOf( embedTransitive ).booleanValue() )
Stuart McCullochf7c44882007-08-23 08:10:03 +00001177 {
1178 // includes transitive dependencies
Stuart McCulloch888d6da2009-02-16 08:25:03 +00001179 artifacts = project.getArtifacts();
1180 }
1181 else
1182 {
1183 // only includes direct dependencies
1184 artifacts = project.getDependencyArtifacts();
Stuart McCullochf7c44882007-08-23 08:10:03 +00001185 }
Stuart McCullochf806f862008-02-04 07:57:09 +00001186
Stuart McCulloch888d6da2009-02-16 08:25:03 +00001187 return getSelectedDependencies( artifacts );
Stuart McCullochf7c44882007-08-23 08:10:03 +00001188 }
Richard S. Hallf3120092006-10-31 17:32:16 +00001189}