001 package org.apache.myfaces.tobago.maven.plugin;
002
003 /*
004 * Licensed to the Apache Software Foundation (ASF) under one or more
005 * contributor license agreements. See the NOTICE file distributed with
006 * this work for additional information regarding copyright ownership.
007 * The ASF licenses this file to You under the Apache License, Version 2.0
008 * (the "License"); you may not use this file except in compliance with
009 * the License. You may obtain a copy of the License at
010 *
011 * http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019
020 import org.apache.maven.artifact.Artifact;
021 import org.apache.maven.plugin.MojoExecutionException;
022 import org.codehaus.plexus.archiver.ArchiverException;
023 import org.codehaus.plexus.archiver.UnArchiver;
024 import org.codehaus.plexus.archiver.manager.ArchiverManager;
025 import org.codehaus.plexus.archiver.manager.NoSuchArchiverException;
026 import org.codehaus.plexus.util.FileUtils;
027
028 import java.io.File;
029 import java.io.IOException;
030 import java.io.FileInputStream;
031 import java.util.Iterator;
032 import java.util.List;
033 import java.util.Locale;
034 import java.util.zip.ZipInputStream;
035 import java.util.zip.ZipEntry;
036
037 /**
038 *
039 * @version $Id: UnPackThemeMojo.java 601107 2007-12-04 22:12:14Z bommel $
040 * @goal resources
041 * @phase process-resources
042 * @requiresDependencyResolution compile
043 */
044 public class UnPackThemeMojo extends AbstractThemeMojo {
045 /**
046 * To look up Archiver/UnArchiver implementations
047 *
048 * @parameter expression="${component.org.codehaus.plexus.archiver.manager.ArchiverManager}"
049 * @required
050 */
051 private ArchiverManager archiverManager;
052
053 /**
054 * Directory to unpack JARs into if needed
055 *
056 * @parameter expression="${project.build.directory}/theme/work"
057 * @required
058 */
059 private File workDirectory;
060
061 /**
062 * The directory where the webapp is built.
063 *
064 * @parameter expression="${project.build.directory}/${project.build.finalName}"
065 * @required
066 */
067 private File webappDirectory;
068
069 /**
070 * @parameter expression="${plugin.artifacts}"
071 * @required
072 */
073 private List pluginArtifacts;
074
075 private boolean findThemeDescriptor(File jarFile) throws MojoExecutionException {
076 ZipInputStream zip = null;
077 try {
078 zip = new ZipInputStream(new FileInputStream(jarFile));
079 while (zip.available() > 0) {
080 ZipEntry nextEntry = zip.getNextEntry();
081 if (nextEntry == null || nextEntry.isDirectory()) {
082 continue;
083 }
084 String name = nextEntry.getName();
085 if (name.equals("META-INF/tobago-theme.xml")) {
086 return true;
087 }
088 }
089 } catch (IOException e) {
090 throw new MojoExecutionException("Error find ThemeDescriptor", e);
091 } finally {
092 if (zip != null) {
093 try {
094 zip.close();
095 } catch (IOException e) {
096 // ignore
097 }
098 }
099 }
100
101 return false;
102 }
103
104 public void execute() throws MojoExecutionException {
105
106 Iterator artifacts = getProject().getDependencyArtifacts().iterator();
107 while (artifacts.hasNext()) {
108 if (!workDirectory.exists()) {
109 workDirectory.mkdirs();
110 }
111 Artifact artifact = (Artifact) artifacts.next();
112 getLog().debug("Expanding theme "+ artifact);
113
114 if (Artifact.SCOPE_COMPILE.equals(artifact.getScope())
115 && "jar".equals(artifact.getType()) && findThemeDescriptor(artifact.getFile())) {
116
117 String name = artifact.getFile().getName();
118 getLog().debug("Expanding theme "+ name);
119 File tempLocation = new File(workDirectory, name.substring(0, name.length() - 4));
120 boolean process = false;
121 if (!tempLocation.exists()) {
122 tempLocation.mkdirs();
123 process = true;
124 } else if (artifact.getFile().lastModified() > tempLocation.lastModified()) {
125 process = true;
126 }
127 if (process) {
128 File file = artifact.getFile();
129 try {
130 unpack(file, tempLocation);
131 String[] fileNames = getThemeFiles(tempLocation);
132 for (String fileName : fileNames) {
133
134 File fromFile = new File(tempLocation, fileName);
135 File toFile = new File(webappDirectory, fileName);
136 try {
137 FileUtils.copyFile(fromFile, toFile);
138 } catch (IOException e) {
139 throw new MojoExecutionException("Error copy file: " + fromFile + "to: " + toFile, e);
140 }
141 }
142 } catch (NoSuchArchiverException e) {
143 this.getLog().info("Skip unpacking dependency file with unknown extension: " + file.getPath());
144 }
145 }
146 }
147 }
148 }
149
150 private void unpack(File file, File location)
151 throws MojoExecutionException, NoSuchArchiverException {
152 String archiveExt = FileUtils.getExtension(file.getAbsolutePath()).toLowerCase(Locale.ENGLISH);
153 try {
154 UnArchiver unArchiver = archiverManager.getUnArchiver(archiveExt);
155 unArchiver.setSourceFile(file);
156 unArchiver.setDestDirectory(location);
157 unArchiver.extract();
158 } catch (IOException e) {
159 throw new MojoExecutionException("Error unpacking file: " + file + "to: " + location, e);
160 } catch (ArchiverException e) {
161 throw new MojoExecutionException("Error unpacking file: " + file + "to: " + location, e);
162 }
163 }
164 }
165
166