001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.servicemix.maven.plugin.jbi;
018:
019: import java.io.File;
020: import java.io.IOException;
021: import java.util.Iterator;
022: import java.util.Set;
023:
024: import org.apache.maven.archiver.MavenArchiveConfiguration;
025: import org.apache.maven.archiver.MavenArchiver;
026: import org.apache.maven.artifact.Artifact;
027: import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
028: import org.apache.maven.plugin.MojoExecutionException;
029: import org.apache.maven.plugin.MojoFailureException;
030: import org.codehaus.plexus.archiver.jar.JarArchiver;
031: import org.codehaus.plexus.util.DirectoryScanner;
032: import org.codehaus.plexus.util.FileUtils;
033:
034: /**
035: * A Mojo used to build the jbi shared library zip file
036: *
037: * @author <a href="pdodds@apache.org">Philip Dodds</a>
038: * @version $Id: GenerateApplicationXmlMojo.java 314956 2005-10-12 16:27:15Z
039: * brett $
040: * @goal jbi-shared-library
041: * @phase package
042: * @requiresDependencyResolution runtime
043: * @description injects additional libraries into shared library
044: */
045: public class GenerateSharedLibraryMojo extends AbstractJbiMojo {
046:
047: /**
048: * The directory for the generated JBI component.
049: *
050: * @parameter expression="${project.build.directory}"
051: * @required
052: */
053: private File outputDirectory;
054:
055: /**
056: * The name of the generated war.
057: *
058: * @parameter expression="${project.artifactId}-${project.version}.zip"
059: * @required
060: */
061: private String sharedLibraryName;
062:
063: /**
064: * The name of the generated war.
065: *
066: * @parameter expression="${project.artifactId}-${project.version}.jar"
067: * @required
068: */
069: private String jarName;
070:
071: /**
072: * The Zip archiver.
073: *
074: * @parameter expression="${component.org.codehaus.plexus.archiver.Archiver#jar}"
075: * @required
076: */
077: private JarArchiver jarArchiver;
078:
079: /**
080: * Single directory for extra files to include in the JBI component.
081: *
082: * @parameter expression="${basedir}/src/main/jbi"
083: * @required
084: */
085: private File jbiSourceDirectory;
086:
087: /**
088: * The maven archive configuration to use.
089: *
090: * @parameter
091: */
092: private MavenArchiveConfiguration archive = new MavenArchiveConfiguration();
093:
094: public void execute() throws MojoExecutionException,
095: MojoFailureException {
096:
097: getLog().debug(
098: " ======= GenerateInstallerMojo settings =======");
099: getLog().debug("workDirectory[" + workDirectory + "]");
100: getLog().debug("installerName[" + sharedLibraryName + "]");
101: getLog()
102: .debug("jbiSourceDirectory[" + jbiSourceDirectory + "]");
103:
104: try {
105:
106: createUnpackedSharedLibrary();
107:
108: File installerFile = new File(outputDirectory,
109: sharedLibraryName);
110: createArchive(installerFile);
111:
112: projectHelper.attachArtifact(project, "jar", "", new File(
113: outputDirectory, jarName));
114:
115: projectHelper.attachArtifact(project, "zip", "installer",
116: new File(outputDirectory, sharedLibraryName));
117:
118: } catch (JbiPluginException e) {
119: throw new MojoExecutionException(
120: "Failed to create shared library", e);
121: }
122: }
123:
124: private void createArchive(File installerFile)
125: throws JbiPluginException {
126: try {
127:
128: // generate war file
129: getLog().info(
130: "Generating shared library "
131: + installerFile.getAbsolutePath());
132: MavenArchiver archiver = new MavenArchiver();
133: archiver.setArchiver(jarArchiver);
134: archiver.setOutputFile(installerFile);
135: jarArchiver.addDirectory(workDirectory);
136: jarArchiver.addConfiguredManifest(createManifest());
137: if (jbiSourceDirectory.isDirectory()) {
138: jarArchiver.addDirectory(jbiSourceDirectory, null,
139: DirectoryScanner.DEFAULTEXCLUDES);
140: }
141: // create archive
142: archiver.createArchive(getProject(), archive);
143:
144: } catch (Exception e) {
145: throw new JbiPluginException(
146: "Error creating shared library: " + e.getMessage(),
147: e);
148: }
149: }
150:
151: private void createUnpackedSharedLibrary()
152: throws JbiPluginException {
153:
154: if (!workDirectory.isDirectory() && !workDirectory.mkdirs()) {
155: throw new JbiPluginException(
156: "Unable to create work directory: " + workDirectory);
157: }
158:
159: File projectArtifact = new File(outputDirectory, project
160: .getArtifactId()
161: + "-" + project.getVersion() + ".jar");
162: try {
163: FileUtils.copyFileToDirectory(projectArtifact, new File(
164: workDirectory, LIB_DIRECTORY));
165:
166: } catch (IOException e) {
167: throw new JbiPluginException("Unable to copy file "
168: + projectArtifact, e);
169: }
170:
171: Set artifacts = project.getArtifacts();
172: for (Iterator iter = artifacts.iterator(); iter.hasNext();) {
173: Artifact artifact = (Artifact) iter.next();
174:
175: // TODO: utilise appropriate methods from project builder
176: ScopeArtifactFilter filter = new ScopeArtifactFilter(
177: Artifact.SCOPE_RUNTIME);
178: if (!artifact.isOptional() && filter.include(artifact)) {
179: String type = artifact.getType();
180: if ("jar".equals(type)) {
181: try {
182: FileUtils.copyFileToDirectory(artifact
183: .getFile(), new File(workDirectory,
184: LIB_DIRECTORY));
185: } catch (IOException e) {
186: throw new JbiPluginException(
187: "Unable to copy file "
188: + artifact.getFile(), e);
189: }
190: }
191: }
192: }
193: }
194:
195: }
|