KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
22 import java.util.Set JavaDoc;
23
24 import org.apache.maven.archiver.MavenArchiveConfiguration;
25 import org.apache.maven.archiver.MavenArchiver;
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.archiver.jar.JarArchiver;
31 import org.codehaus.plexus.util.DirectoryScanner;
32 import org.codehaus.plexus.util.FileUtils;
33
34 /**
35  * A Mojo used to build the jbi shared library zip file
36  *
37  * @author <a HREF="pdodds@apache.org">Philip Dodds</a>
38  * @version $Id: GenerateApplicationXmlMojo.java 314956 2005-10-12 16:27:15Z
39  * brett $
40  * @goal jbi-shared-library
41  * @phase package
42  * @requiresDependencyResolution runtime
43  * @description injects additional libraries into shared library
44  */

45 public class GenerateSharedLibraryMojo extends AbstractJbiMojo {
46
47     /**
48      * The directory for the generated JBI component.
49      *
50      * @parameter expression="${project.build.directory}"
51      * @required
52      */

53     private File JavaDoc outputDirectory;
54
55     /**
56      * The name of the generated war.
57      *
58      * @parameter expression="${project.artifactId}-${project.version}.zip"
59      * @required
60      */

61     private String JavaDoc sharedLibraryName;
62     
63     /**
64      * The name of the generated war.
65      *
66      * @parameter expression="${project.artifactId}-${project.version}.jar"
67      * @required
68      */

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

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

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

92     private MavenArchiveConfiguration archive = new MavenArchiveConfiguration();
93
94     public void execute() throws MojoExecutionException, MojoFailureException {
95
96         getLog().debug(" ======= GenerateInstallerMojo settings =======");
97         getLog().debug("workDirectory[" + workDirectory + "]");
98         getLog().debug("installerName[" + sharedLibraryName + "]");
99         getLog().debug("jbiSourceDirectory[" + jbiSourceDirectory + "]");
100
101         try {
102
103             createUnpackedSharedLibrary();
104
105             File JavaDoc installerFile = new File JavaDoc(outputDirectory, sharedLibraryName);
106             createArchive(installerFile);
107             
108             projectHelper.attachArtifact(project,"jar", "", new File JavaDoc(
109                     outputDirectory, jarName));
110
111             projectHelper.attachArtifact(project,"zip", "installer", new File JavaDoc(
112                     outputDirectory, sharedLibraryName));
113
114         } catch (JbiPluginException e) {
115             throw new MojoExecutionException("Failed to create shared library",
116                     e);
117         }
118     }
119
120     private void createArchive(File JavaDoc installerFile) throws JbiPluginException {
121         try {
122
123             // generate war file
124
getLog().info(
125                     "Generating shared library "
126                             + installerFile.getAbsolutePath());
127             MavenArchiver archiver = new MavenArchiver();
128             archiver.setArchiver(jarArchiver);
129             archiver.setOutputFile(installerFile);
130             jarArchiver.addDirectory(workDirectory);
131             if (jbiSourceDirectory.isDirectory()) {
132                 jarArchiver.addDirectory(jbiSourceDirectory, null,
133                         DirectoryScanner.DEFAULTEXCLUDES);
134             }
135             // create archive
136
archiver.createArchive(getProject(), archive);
137
138         } catch (Exception JavaDoc e) {
139             throw new JbiPluginException("Error creating shared library: "
140                     + e.getMessage(), e);
141         }
142     }
143
144     private void createUnpackedSharedLibrary() throws JbiPluginException {
145
146         if (!workDirectory.isDirectory()) {
147             if (!workDirectory.mkdirs()) {
148                 throw new JbiPluginException(
149                         "Unable to create work directory: " + workDirectory);
150             }
151         }
152
153         File JavaDoc projectArtifact = new File JavaDoc(outputDirectory, project
154                 .getArtifactId()
155                 + "-" + project.getVersion() + ".jar");
156         try {
157             FileUtils.copyFileToDirectory(projectArtifact, new File JavaDoc(
158                     workDirectory, LIB_DIRECTORY));
159             
160         } catch (IOException JavaDoc e) {
161             throw new JbiPluginException("Unable to copy file "
162                     + projectArtifact, e);
163         }
164         
165         Set JavaDoc artifacts = project.getArtifacts();
166         for (Iterator JavaDoc iter = artifacts.iterator(); iter.hasNext();) {
167             Artifact artifact = (Artifact) iter.next();
168
169             // TODO: utilise appropriate methods from project builder
170
ScopeArtifactFilter filter = new ScopeArtifactFilter(
171                     Artifact.SCOPE_RUNTIME);
172             if (!artifact.isOptional() && filter.include(artifact)) {
173                 String JavaDoc type = artifact.getType();
174                 if ("jar".equals(type)) {
175                     try {
176                         FileUtils.copyFileToDirectory(artifact.getFile(),
177                                 new File JavaDoc(workDirectory, LIB_DIRECTORY));
178                     } catch (IOException JavaDoc e) {
179                         throw new JbiPluginException("Unable to copy file "
180                                 + artifact.getFile(), e);
181                     }
182                 }
183             }
184         }
185     }
186
187 }
188
Popular Tags