View Javadoc

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   * Author: Siamak Haschemi
20   * Contact: haschemi@informatik.hu-berlin.de
21   */
22  package net.sourceforge.osgi.deployment.maven.mojo;
23  
24  import java.io.File;
25  import java.util.Arrays;
26  import java.util.List;
27  
28  import net.sourceforge.osgi.deployment.maven.DeploymentPluginException;
29  import net.sourceforge.osgi.deployment.maven.DeploymentPlugin;
30  import net.sourceforge.osgi.deployment.maven.IDeploymentPluginContext;
31  import net.sourceforge.osgi.deployment.maven.container.BundleResource;
32  import net.sourceforge.osgi.deployment.maven.container.DeploymentPackageInfo;
33  
34  import org.apache.maven.artifact.Artifact;
35  import org.apache.maven.artifact.factory.ArtifactFactory;
36  import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
37  import org.apache.maven.artifact.repository.ArtifactRepository;
38  import org.apache.maven.artifact.resolver.ArtifactResolver;
39  import org.apache.maven.plugin.AbstractMojo;
40  import org.apache.maven.plugin.MojoExecutionException;
41  import org.apache.maven.plugin.MojoFailureException;
42  import org.apache.maven.plugin.logging.Log;
43  import org.apache.maven.project.MavenProject;
44  import org.codehaus.plexus.archiver.manager.ArchiverManager;
45  
46  /**
47   * Create an OSGi deployment package from Maven project.
48   * 
49   * @goal package
50   * @phase package
51   * @requiresDependencyResolution runtime
52   * @description build an OSGi deployment package jar
53   * @author Siamak Haschemi, haschemi@informatik.hu-berlin.de
54   */
55  public class DeploymentPluginMojoContext extends AbstractMojo implements IDeploymentPluginContext {
56    /**
57     * The directory for the generated bundles.
58     * 
59     * @parameter property="ouputDirectory" expression="${project.build.outputDirectory}"
60     * @required
61     */
62    private File m_outputDirectory;
63  
64    /**
65     * The directory for the pom.
66     * 
67     * @parameter property="basedir" expression="${basedir}"
68     * @required
69     */
70    private File m_baseDir;
71  
72    /**
73     * The infos for the deployment-package.
74     * 
75     * @parameter property="deploymentPackage"
76     * @required
77     */
78    private DeploymentPackageInfo m_deploymentPackageInfo = null;
79  
80    /**
81     * Directory where the manifest will be written.
82     * 
83     * @parameter property="manifestLocation" expression="${manifestLocation}" default-value="${project.build.outputDirectory}/META-INF"
84     */
85    private File m_manifestLocation;
86  
87    /**
88     * The Maven project.
89     * 
90     * @parameter expression="${project}"
91     * @required
92     * @readonly
93     */
94    private MavenProject m_project;
95  
96    /**
97     * The directory for the generated JAR.
98     * 
99     * @parameter property="buildDirectory" expression="${project.build.directory}"
100    * @required
101    */
102   private String m_buildDirectory;
103 
104   /**
105    * Project types which this plugin supports.
106    * 
107    * @parameter property="supportedProjectTypes"
108    * @required
109    */
110   private List< String > m_supportedProjectTypes = Arrays.asList(new String[] { "deployment-package" });
111 
112   /**
113    * The local repository used to resolve artifacts.
114    * 
115    * @parameter property="localRepository" expression="${localRepository}"
116    * @required
117    */
118   private ArtifactRepository m_localRepository;
119 
120   /**
121    * The remote repositories used to resolve artifacts.
122    * 
123    * @parameter property="remoteRepositories" expression="${project.remoteArtifactRepositories}"
124    * @required
125    */
126   private List< ArtifactRepository > m_remoteRepositories;
127 
128   /**
129    * Flag that indicates if the manifest of the resulting deployment-package should contain extra data like "Created-By, Creation-Date, ...".
130    * 
131    * @parameter property="writeExtraData"
132    * @required
133    */
134   private boolean m_writeExtraData = true;
135 
136   /**
137    * @component
138    */
139   private ArtifactFactory m_artifactFactory;
140 
141   /**
142    * @component
143    */
144   private ArtifactResolver m_artifactresolver;
145 
146   /**
147    * @component
148    */
149   private ArtifactHandlerManager m_artifactHandlerManager;
150 
151   /**
152    * @component
153    */
154   private ArchiverManager m_archiverManager;
155 
156   /**
157    * This method will be called by the Maven framework in order to execute this plugin.
158    * 
159    * @throws MojoExecutionException
160    *           id any error occures
161    * @throws MojoFailureException
162    *           id any error occures
163    */
164   public final void execute() throws MojoExecutionException, MojoFailureException {
165     try {
166       // Inject the context
167       // TODO Can we do this with maven automatically?
168       DeploymentPackageInfo deploymentPackageInfo = getDeploymentPackageInfo();
169       if (deploymentPackageInfo == null) {
170         throw new MojoExecutionException("No DeploymentPackageInfo provided");
171       }
172 
173       for (final BundleResource bundleResource : deploymentPackageInfo.getBundleResources()) {
174         bundleResource.setDeploymentContext(this);
175       }
176 
177       new DeploymentPlugin(this).execute();
178     } catch (final RuntimeException e) {
179       throw new MojoExecutionException("Error while executing plugin ", e);
180     } catch (final Throwable e) {
181       throw new MojoExecutionException("Error while executing plugin ", e);
182     }
183   }
184 
185   /**
186    * @return the logger
187    * @see net.sourceforge.osgi.deployment.maven.IDeploymentPluginContext#getLogger()
188    */
189   public final Log getLogger() {
190     return super.getLog();
191   }
192 
193   /**
194    * @return the outputDirectory
195    * @see net.sourceforge.osgi.deployment.maven.IDeploymentPluginContext#getOutputDirectory()
196    */
197   public final File getOutputDirectory() {
198     return m_outputDirectory;
199   }
200 
201   /**
202    * @param p_outputDirectory
203    *          the outputDirectory to set
204    */
205   public final void setOutputDirectory(final File p_outputDirectory) {
206     m_outputDirectory = p_outputDirectory;
207   }
208 
209   /**
210    * @return the base directory
211    * @see net.sourceforge.osgi.deployment.maven.IDeploymentPluginContext#getBaseDir()
212    */
213   public final File getBaseDir() {
214     return m_baseDir;
215   }
216 
217   /**
218    * @return the menifest location
219    * @see net.sourceforge.osgi.deployment.maven.IDeploymentPluginContext#getManifestLocation()
220    */
221   public final File getManifestLocation() {
222     return m_manifestLocation;
223   }
224 
225   /**
226    * @return the maven project
227    * @see net.sourceforge.osgi.deployment.maven.IDeploymentPluginContext#getProject()
228    */
229   public final MavenProject getProject() {
230     return m_project;
231   }
232 
233   /**
234    * @return the build directory
235    * @see net.sourceforge.osgi.deployment.maven.IDeploymentPluginContext#getBuildDirectory()
236    */
237   public final String getBuildDirectory() {
238     return m_buildDirectory;
239   }
240 
241   /**
242    * @return the supported packaging types
243    * @see net.sourceforge.osgi.deployment.maven.IDeploymentPluginContext#getSupportedProjectTypes()
244    */
245   public final List< String > getSupportedProjectTypes() {
246     return m_supportedProjectTypes;
247   }
248 
249   /**
250    * @return the local repository
251    * @see net.sourceforge.osgi.deployment.maven.IDeploymentPluginContext#getLocalRepository()
252    */
253   public final ArtifactRepository getLocalRepository() {
254     return m_localRepository;
255   }
256 
257   /**
258    * @return the remote repositories
259    * @see net.sourceforge.osgi.deployment.maven.IDeploymentPluginContext#getRemoteRepositories()
260    */
261   public final List< ArtifactRepository > getRemoteRepositories() {
262     return m_remoteRepositories;
263   }
264 
265   /**
266    * @return the artifact factory
267    * @see net.sourceforge.osgi.deployment.maven.IDeploymentPluginContext#getArtifactFactory()
268    */
269   public final ArtifactFactory getArtifactFactory() {
270     return m_artifactFactory;
271   }
272 
273   /**
274    * @return the artifact resolver
275    * @see net.sourceforge.osgi.deployment.maven.IDeploymentPluginContext#getArtifactResolver()
276    */
277   public final ArtifactResolver getArtifactResolver() {
278     return m_artifactresolver;
279   }
280 
281   /**
282    * @return the artifact handler manager
283    * @see net.sourceforge.osgi.deployment.maven.IDeploymentPluginContext#getArtifactHandlerManager()
284    */
285   public final ArtifactHandlerManager getArtifactHandlerManager() {
286     return m_artifactHandlerManager;
287   }
288 
289   /**
290    * @return the archiver manager
291    * @see net.sourceforge.osgi.deployment.maven.IDeploymentPluginContext#getArchiverManager()
292    */
293   public final ArchiverManager getArchiverManager() {
294     return m_archiverManager;
295   }
296 
297   /**
298    * @return the deployment package info
299    * @see net.sourceforge.osgi.deployment.maven.IDeploymentPluginContext#getDeploymentPackageInfo()
300    */
301   public final DeploymentPackageInfo getDeploymentPackageInfo() {
302     return m_deploymentPackageInfo;
303   }
304 
305   /**
306    * @return <CODE>TRUE</CODE> if extra data should be generated into the manifest file, else <CODE>FALSE></CODE>. Default is <CODE>TRUE</CODE>.
307    * @see net.sourceforge.osgi.deployment.maven.IDeploymentPluginContext#isWriteExtraData()
308    */
309   public final boolean isWriteExtraData() {
310     return m_writeExtraData;
311   }
312 
313   /**
314    * TODO How do we get the plugin name?
315    * 
316    * @return the plugin name
317    * @see net.sourceforge.osgi.deployment.maven.IDeploymentPluginContext#getPluginName()
318    */
319   public final String getPluginName() {
320     return "osgi-deployment-maven-plugin";
321   }
322 
323   /**
324    * TODO How do we get the plugin version?
325    * 
326    * @return the plugin version
327    * @see net.sourceforge.osgi.deployment.maven.IDeploymentPluginContext#getPluginVersion()
328    */
329   public final String getPluginVersion() {
330     return "0.1.3";
331   }
332 
333   /**
334    * @param p_baseDir
335    *          the baseDir to set
336    */
337   public final void setBaseDir(final File p_baseDir) {
338     m_baseDir = p_baseDir;
339   }
340 
341   /**
342    * @param p_manifestLocation
343    *          the manifestLocation to set
344    */
345   public final void setManifestLocation(final File p_manifestLocation) {
346     m_manifestLocation = p_manifestLocation;
347   }
348 
349   /**
350    * @param p_project
351    *          the project to set
352    */
353   public final void setProject(final MavenProject p_project) {
354     m_project = p_project;
355   }
356 
357   /**
358    * @param p_buildDirectory
359    *          the buildDirectory to set
360    */
361   public final void setBuildDirectory(final String p_buildDirectory) {
362     m_buildDirectory = p_buildDirectory;
363   }
364 
365   /**
366    * @param p_supportedProjectTypes
367    *          the supportedProjectTypes to set
368    */
369   public final void setSupportedProjectTypes(final List< String > p_supportedProjectTypes) {
370     m_supportedProjectTypes = p_supportedProjectTypes;
371   }
372 
373   /**
374    * @param p_localRepository
375    *          the localRepository to set
376    */
377   public final void setLocalRepository(final ArtifactRepository p_localRepository) {
378     m_localRepository = p_localRepository;
379   }
380 
381   /**
382    * @param p_remoteRepositories
383    *          the remoteRepositories to set
384    */
385   public final void setRemoteRepositories(final List< ArtifactRepository > p_remoteRepositories) {
386     m_remoteRepositories = p_remoteRepositories;
387   }
388 
389   /**
390    * @param p_artifactFactory
391    *          the artifactFactory to set
392    */
393   public final void setArtifactFactory(final ArtifactFactory p_artifactFactory) {
394     m_artifactFactory = p_artifactFactory;
395   }
396 
397   /**
398    * @param p_artifactResolver
399    *          the artifactResolver to set
400    */
401   public final void setArtifactResolver(final ArtifactResolver p_artifactResolver) {
402     m_artifactresolver = p_artifactResolver;
403   }
404 
405   /**
406    * @param p_artifactHandlerManager
407    *          the artifactHandlerManager to set
408    */
409   public final void setArtifactHandlerManager(final ArtifactHandlerManager p_artifactHandlerManager) {
410     m_artifactHandlerManager = p_artifactHandlerManager;
411   }
412 
413   /**
414    * @param p_archiverManager
415    *          the archiverManager to set
416    */
417   public final void setArchiverManager(final ArchiverManager p_archiverManager) {
418     m_archiverManager = p_archiverManager;
419   }
420 
421   /**
422    * @param p_deploymentPackageInfo
423    *          the deploymentPackage to set
424    */
425   public final void setDeploymentPackageInfo(final DeploymentPackageInfo p_deploymentPackageInfo) {
426     m_deploymentPackageInfo = p_deploymentPackageInfo;
427   }
428 
429   /**
430    * @param p_deploymentPackageInfo
431    *          the deploymentPackage to set
432    */
433   public final void setDeploymentPackage(final DeploymentPackageInfo p_deploymentPackageInfo) {
434     setDeploymentPackageInfo(p_deploymentPackageInfo);
435   }
436 
437   /**
438    * @param p_writeExtraData
439    *          the writeExtraData to set
440    */
441   public final void setWriteExtraData(final boolean p_writeExtraData) {
442     m_writeExtraData = p_writeExtraData;
443   }
444 
445   /**
446    * This method resolves an artifact on all available repositories and returns the file handle to that artifact.
447    * 
448    * @param p_groupId
449    *          the groupId of the artifact to resolve
450    * @param p_artifactId
451    *          the artifactId of the artifact to resolve
452    * @param p_version
453    *          the version of the artifact to resolve
454    * @return the resolved file handle of the artifact
455    */
456   public final File resolveResource(final String p_groupId, final String p_artifactId, final String p_version) {
457     try {
458       final Artifact artifact = getArtifactFactory().createArtifact(p_groupId, p_artifactId, p_version, Artifact.SCOPE_RUNTIME, "jar");
459       getArtifactResolver().resolve(artifact, getRemoteRepositories(), getLocalRepository());
460       final File artifactFile = artifact.getFile();
461       return artifactFile;
462     } catch (final Exception e) {
463       // Wrap checked exception
464       throw new DeploymentPluginException("Error while resolving resource " + p_groupId + ":" + p_artifactId + ":" + p_version, e);
465     }
466   }
467 
468 }