KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Set JavaDoc;
25
26 import org.apache.maven.artifact.Artifact;
27 import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
28 import org.apache.maven.plugin.MojoExecutionException;
29 import org.apache.maven.plugin.MojoFailureException;
30 import org.codehaus.plexus.util.FileUtils;
31
32 /**
33  * A Mojo used to build the jbi.xml file for a shared library
34  *
35  * @author <a HREF="pdodds@apache.org">Philip Dodds</a>
36  * @version $Id: GenerateComponentDescriptorMojo 314956 2005-10-12 16:27:15Z
37  * brett $
38  * @goal generate-jbi-shared-library-descriptor
39  * @phase generate-resources
40  * @requiresDependencyResolution runtime
41  * @description generates the jbi.xml deployment descriptor for a shared library
42  */

43 public class GenerateSharedLibraryDescriptorMojo extends AbstractJbiMojo {
44
45     public static final String JavaDoc UTF_8 = "UTF-8";
46
47     /**
48      * Whether the jbi.xml should be generated or not.
49      *
50      * @parameter
51      */

52     private Boolean JavaDoc generateJbiDescriptor = Boolean.TRUE;
53
54     /**
55      * The shared library name.
56      *
57      * @parameter expression="${project.artifactId}"
58      */

59     private String JavaDoc name;
60
61     /**
62      * The shared library description.
63      *
64      * @parameter expression="${project.name}"
65      */

66     private String JavaDoc description;
67
68     /**
69      * The shared library version.
70      *
71      * @parameter expression="${project.version}"
72      */

73     private String JavaDoc version;
74
75     /**
76      * The shared library class loader delegation
77      *
78      * @parameter expression="parent-first"
79      */

80     private String JavaDoc classLoaderDelegation;
81
82     /**
83      * Character encoding for the auto-generated application.xml file.
84      *
85      * @parameter
86      */

87     private String JavaDoc encoding = UTF_8;
88
89     /**
90      * Directory where the application.xml file will be auto-generated.
91      *
92      * @parameter expression="${project.build.directory}"
93      */

94     private String JavaDoc generatedDescriptorLocation;
95
96     public void execute() throws MojoExecutionException, MojoFailureException {
97
98         getLog()
99                 .debug(
100                         " ======= GenerateSharedLibraryDescriptorMojo settings =======");
101         getLog().debug("workDirectory[" + workDirectory + "]");
102         getLog().debug("generateJbiDescriptor[" + generateJbiDescriptor + "]");
103         getLog().debug("name[" + name + "]");
104         getLog().debug("description[" + description + "]");
105         getLog().debug("encoding[" + encoding + "]");
106         getLog().debug("generatedDescriptorLocation[" + generatedDescriptorLocation + "]");
107         getLog().debug("version[" + version + "]");
108
109         if (!generateJbiDescriptor.booleanValue()) {
110             getLog().debug("Generation of jbi.xml is disabled");
111             return;
112         }
113
114         // Generate jbi descriptor and copy it to the build directory
115
getLog().info("Generating jbi.xml");
116         try {
117             generateJbiDescriptor();
118         } catch (JbiPluginException e) {
119             throw new MojoExecutionException("Failed to generate jbi.xml", e);
120         }
121
122         try {
123             FileUtils.copyFileToDirectory(new File JavaDoc(generatedDescriptorLocation,
124                     JBI_DESCRIPTOR), new File JavaDoc(getWorkDirectory(), META_INF));
125         } catch (IOException JavaDoc e) {
126             throw new MojoExecutionException(
127                     "Unable to copy jbi.xml to final destination", e);
128         }
129     }
130
131     /**
132      * Generates the deployment descriptor if necessary.
133      */

134     protected void generateJbiDescriptor() throws JbiPluginException {
135         File JavaDoc outputDir = new File JavaDoc(generatedDescriptorLocation);
136         if (!outputDir.exists()) {
137             outputDir.mkdirs();
138         }
139
140         File JavaDoc descriptor = new File JavaDoc(outputDir, JBI_DESCRIPTOR);
141
142         List JavaDoc embeddedLibraries = new ArrayList JavaDoc();
143
144         DependencyInformation info = new DependencyInformation();
145         info.setFilename(LIB_DIRECTORY + "/" + project.getArtifactId() + "-"
146                 + project.getVersion() + ".jar");
147         info.setVersion(project.getVersion());
148         info.setName(project.getArtifactId());
149         info.setType("jar");
150         embeddedLibraries.add(info);
151
152         Set JavaDoc artifacts = project.getArtifacts();
153         for (Iterator JavaDoc iter = artifacts.iterator(); iter.hasNext();) {
154             Artifact artifact = (Artifact) iter.next();
155
156             // TODO: utilise appropriate methods from project builder
157
ScopeArtifactFilter filter = new ScopeArtifactFilter(
158                     Artifact.SCOPE_RUNTIME);
159             if (!artifact.isOptional() && filter.include(artifact)) {
160                 String JavaDoc type = artifact.getType();
161                 if ("jar".equals(type)) {
162                     info = new DependencyInformation();
163                     info.setFilename(LIB_DIRECTORY + "/" + artifact.getFile().getName());
164                     embeddedLibraries.add(info);
165                 }
166             }
167         }
168
169         JbiSharedLibraryDescriptorWriter writer = new JbiSharedLibraryDescriptorWriter(
170                 encoding);
171         writer.write(descriptor, name, description, version,
172                 classLoaderDelegation, embeddedLibraries);
173     }
174 }
175
Popular Tags