KickJava   Java API By Example, From Geeks To Geeks.

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


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.FileUtils;
38
39 /**
40  * A Mojo used to build the jbi service unit zip file
41  *
42  * @author <a HREF="pdodds@apache.org">Philip Dodds</a>
43  * @version $Id: GenerateApplicationXmlMojo.java 314956 2005-10-12 16:27:15Z
44  * brett $
45  * @goal jbi-service-unit
46  * @phase package
47  * @requiresDependencyResolution runtime
48  * @description injects additional libraries into service unit
49  */

50 public class GenerateServiceUnitMojo extends AbstractJbiMojo {
51
52     /**
53      * The name of the generated war.
54      *
55      * @parameter expression="${project.artifactId}-${project.version}.zip"
56      * @required
57      */

58     private String JavaDoc serviceUnitName;
59
60     /**
61      * The directory for the generated JBI component.
62      *
63      * @parameter expression="${project.build.directory}"
64      * @required
65      */

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

74     private JarArchiver jarArchiver;
75
76     /**
77      * The maven archive configuration to use.
78      *
79      * @parameter
80      */

81     private MavenArchiveConfiguration archive = new MavenArchiveConfiguration();
82
83     /**
84      * Directory where the application.xml file will be auto-generated.
85      *
86      * @parameter expression="${project.build.directory}/classes"
87      */

88     private File JavaDoc serviceUnitLocation;
89
90     public void execute() throws MojoExecutionException, MojoFailureException {
91         try {
92
93             createUnpackedInstaller();
94
95             File JavaDoc serviceUnitFile = new File JavaDoc(outputDirectory, serviceUnitName);
96             createArchive(serviceUnitFile);
97
98             projectHelper.attachArtifact(project, "zip", "", new File JavaDoc(
99                     outputDirectory, serviceUnitName));
100
101         } catch (JbiPluginException e) {
102             throw new MojoExecutionException("Failed to create service unit", e);
103         }
104
105     }
106
107     private void createArchive(File JavaDoc installerFile) throws JbiPluginException {
108         try {
109
110             // generate war file
111
getLog().info(
112                     "Generating service unit "
113                             + installerFile.getAbsolutePath());
114             MavenArchiver archiver = new MavenArchiver();
115             archiver.setArchiver(jarArchiver);
116             archiver.setOutputFile(installerFile);
117             jarArchiver.addDirectory(workDirectory);
118
119             // create archive
120
archiver.createArchive(getProject(), archive);
121
122         } catch (ArchiverException e) {
123             throw new JbiPluginException("Error creating service unit: "
124                     + e.getMessage(), e);
125         } catch (ManifestException e) {
126             throw new JbiPluginException("Error creating service unit: "
127                     + e.getMessage(), e);
128         } catch (IOException JavaDoc e) {
129             throw new JbiPluginException("Error creating service unit: "
130                     + e.getMessage(), e);
131         } catch (DependencyResolutionRequiredException e) {
132             throw new JbiPluginException("Error creating service unit: "
133                     + e.getMessage(), e);
134         }
135
136     }
137
138     private void createUnpackedInstaller() throws JbiPluginException {
139
140         if (!workDirectory.isDirectory()) {
141             if (!workDirectory.mkdirs()) {
142                 throw new JbiPluginException(
143                         "Unable to create work directory: " + workDirectory);
144             }
145         }
146
147         try {
148             FileUtils.copyDirectoryStructure(serviceUnitLocation, workDirectory);
149         } catch (IOException JavaDoc e) {
150             throw new JbiPluginException("Unable to copy directory "
151                     + serviceUnitLocation, e);
152         }
153
154         ScopeArtifactFilter filter = new ScopeArtifactFilter(
155                 Artifact.SCOPE_RUNTIME);
156
157         JbiResolutionListener listener = resolveProject();
158         // print(listener.getRootNode(), "");
159

160         Set JavaDoc includes = new HashSet JavaDoc();
161         for (Iterator JavaDoc iter = project.getArtifacts().iterator(); iter.hasNext();) {
162             Artifact artifact = (Artifact) iter.next();
163             if (!artifact.isOptional() && filter.include(artifact)) {
164                 MavenProject project = null;
165                 getLog().info("Resolving "+artifact);
166                 try {
167                     project = projectBuilder.buildFromRepository(artifact,
168                             remoteRepos, localRepo);
169                 } catch (ProjectBuildingException e) {
170                     getLog().warn(
171                             "Unable to determine packaging for dependency : "
172                                     + artifact.getArtifactId()
173                                     + " assuming jar");
174                 }
175                 String JavaDoc type = project != null ? project.getPackaging()
176                         : artifact.getType();
177                 if ("jbi-component".equals(type)) {
178                     removeBranch(listener, artifact);
179                 } else if ("jbi-shared-library".equals(type)) {
180                     removeBranch(listener, artifact);
181                 } else if ("jar".equals(type)) {
182                     includes.add(artifact);
183                 }
184             }
185         }
186         // print(listener.getRootNode(), "");
187

188         for (Iterator JavaDoc iter = retainArtifacts(includes, listener).iterator(); iter
189                 .hasNext();) {
190             Artifact artifact = (Artifact) iter.next();
191             try {
192                 getLog().info("Including: " + artifact);
193                 FileUtils.copyFileToDirectory(artifact.getFile(), new File JavaDoc(
194                         workDirectory, LIB_DIRECTORY));
195             } catch (IOException JavaDoc e) {
196                 throw new JbiPluginException("Unable to copy file "
197                         + artifact.getFile(), e);
198             }
199         }
200     }
201 }
202
Popular Tags