KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > maven > plugin > jbi > GenerateComponentMojo


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.servicemix.maven.plugin.jbi;
18
19 import java.io.File JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.util.HashSet JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Set JavaDoc;
24
25 import org.apache.maven.archiver.MavenArchiveConfiguration;
26 import org.apache.maven.archiver.MavenArchiver;
27 import org.apache.maven.artifact.Artifact;
28 import org.apache.maven.artifact.DependencyResolutionRequiredException;
29 import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
30 import org.apache.maven.plugin.MojoExecutionException;
31 import org.apache.maven.plugin.MojoFailureException;
32 import org.apache.maven.project.MavenProject;
33 import org.apache.maven.project.ProjectBuildingException;
34 import org.codehaus.plexus.archiver.ArchiverException;
35 import org.codehaus.plexus.archiver.jar.JarArchiver;
36 import org.codehaus.plexus.archiver.jar.ManifestException;
37 import org.codehaus.plexus.util.DirectoryScanner;
38 import org.codehaus.plexus.util.FileUtils;
39
40 /**
41  * A Mojo used to build the jbi component installer file.
42  *
43  * @author <a HREF="gnodet@apache.org">Guillaume Nodet</a>
44  * @version $Id: GenerateApplicationXmlMojo.java 314956 2005-10-12 16:27:15Z
45  * brett $
46  * @goal jbi-component
47  * @phase package
48  * @requiresDependencyResolution runtime
49  * @description generates the component installer
50  */

51 public class GenerateComponentMojo extends AbstractJbiMojo {
52
53     /**
54      * The directory for the generated JBI component.
55      *
56      * @parameter expression="${project.build.directory}"
57      * @required
58      */

59     private File JavaDoc outputDirectory;
60
61     /**
62      * The name of the generated war.
63      *
64      * @parameter expression="${project.artifactId}-${project.version}-installer.zip"
65      * @required
66      */

67     private String JavaDoc installerName;
68
69     /**
70      * The Zip archiver.
71      *
72      * @parameter expression="${component.org.codehaus.plexus.archiver.Archiver#jar}"
73      * @required
74      */

75     private JarArchiver jarArchiver;
76
77     /**
78      * Single directory for extra files to include in the JBI component.
79      *
80      * @parameter expression="${basedir}/src/main/jbi"
81      * @required
82      */

83     private File JavaDoc jbiSourceDirectory;
84
85     /**
86      * The maven archive configuration to use.
87      *
88      * @parameter
89      */

90     private MavenArchiveConfiguration archive = new MavenArchiveConfiguration();
91
92     public void execute() throws MojoExecutionException, MojoFailureException {
93
94         getLog().debug(" ======= GenerateInstallerMojo settings =======");
95         getLog().debug("workDirectory[" + workDirectory + "]");
96         getLog().debug("installerName[" + installerName + "]");
97         getLog().debug("jbiSourceDirectory[" + jbiSourceDirectory + "]");
98
99         try {
100
101             createUnpackedInstaller();
102
103             File JavaDoc installerFile = new File JavaDoc(outputDirectory, installerName);
104             createArchive(installerFile);
105
106             projectHelper.attachArtifact(project, "zip", "installer", new File JavaDoc(
107                     outputDirectory, installerName));
108
109         } catch (JbiPluginException e) {
110             throw new MojoExecutionException("Failed to create installer", e);
111         }
112     }
113
114     private void createArchive(File JavaDoc installerFile) throws JbiPluginException {
115         try {
116
117             // generate war file
118
getLog().info(
119                     "Generating installer " + installerFile.getAbsolutePath());
120             MavenArchiver archiver = new MavenArchiver();
121             archiver.setArchiver(jarArchiver);
122             archiver.setOutputFile(installerFile);
123             jarArchiver.addDirectory(workDirectory);
124             if (jbiSourceDirectory.isDirectory()) {
125                 jarArchiver.addDirectory(jbiSourceDirectory, null,
126                         DirectoryScanner.DEFAULTEXCLUDES);
127             }
128             // create archive
129
archiver.createArchive(getProject(), archive);
130
131         } catch (ArchiverException e) {
132             throw new JbiPluginException("Error creating assembly: "
133                     + e.getMessage(), e);
134         } catch (ManifestException e) {
135             throw new JbiPluginException("Error creating assembly: "
136                     + e.getMessage(), e);
137         } catch (IOException JavaDoc e) {
138             throw new JbiPluginException("Error creating assembly: "
139                     + e.getMessage(), e);
140         } catch (DependencyResolutionRequiredException e) {
141             throw new JbiPluginException("Error creating assembly: "
142                     + e.getMessage(), e);
143         }
144
145     }
146
147     private void createUnpackedInstaller() throws JbiPluginException {
148
149         if (!workDirectory.isDirectory()) {
150             if (!workDirectory.mkdirs()) {
151                 throw new JbiPluginException(
152                         "Unable to create work directory: " + workDirectory);
153             }
154         }
155
156         File JavaDoc projectArtifact = new File JavaDoc(outputDirectory, project
157                 .getArtifactId()
158                 + "-" + project.getVersion() + ".jar");
159         try {
160             FileUtils.copyFileToDirectory(projectArtifact, new File JavaDoc(
161                     workDirectory, LIB_DIRECTORY));
162         } catch (IOException JavaDoc e) {
163             throw new JbiPluginException("Unable to copy file "
164                     + projectArtifact, e);
165         }
166
167         ScopeArtifactFilter filter = new ScopeArtifactFilter(
168                 Artifact.SCOPE_RUNTIME);
169
170         JbiResolutionListener listener = resolveProject();
171         // print(listener.getRootNode(), "");
172

173         Set JavaDoc includes = new HashSet JavaDoc();
174         for (Iterator JavaDoc iter = project.getArtifacts().iterator(); iter.hasNext();) {
175             Artifact artifact = (Artifact) iter.next();
176             if (!artifact.isOptional() && filter.include(artifact)) {
177                 MavenProject project = null;
178                 try {
179                     project = projectBuilder.buildFromRepository(artifact,
180                             remoteRepos, localRepo);
181                 } catch (ProjectBuildingException e) {
182                     getLog().warn(
183                             "Unable to determine packaging for dependency : "
184                                     + artifact.getArtifactId()
185                                     + " assuming jar");
186                 }
187                 String JavaDoc type = project != null ? project.getPackaging()
188                         : artifact.getType();
189                 if ("jbi-shared-library".equals(type)) {
190                     removeBranch(listener, artifact);
191                 } else if ("jar".equals(type)) {
192                     includes.add(artifact);
193                 }
194             }
195         }
196         // print(listener.getRootNode(), "");
197

198         for (Iterator JavaDoc iter = retainArtifacts(includes, listener).iterator(); iter
199                 .hasNext();) {
200             Artifact artifact = (Artifact) iter.next();
201             try {
202                 getLog().info("Including: " + artifact);
203                 FileUtils.copyFileToDirectory(artifact.getFile(), new File JavaDoc(
204                         workDirectory, LIB_DIRECTORY));
205             } catch (IOException JavaDoc e) {
206                 throw new JbiPluginException("Unable to copy file "
207                         + artifact.getFile(), e);
208             }
209         }
210     }
211 }
212
Popular Tags