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.zip.ZipInputStream;
034 import java.util.zip.ZipEntry;
035
036 /**
037 * Created by IntelliJ IDEA.
038 * User: bommel
039 *
040 * @version $Id: UnPackThemeMojo.java 578592 2007-09-23 18:51:32Z bommel $
041 * @goal resources
042 * @phase process-resources
043 * @requiresDependencyResolution compile
044 */
045 public class UnPackThemeMojo extends AbstractThemeMojo {
046 /**
047 * To look up Archiver/UnArchiver implementations
048 *
049 * @parameter expression="${component.org.codehaus.plexus.archiver.manager.ArchiverManager}"
050 * @required
051 */
052 private ArchiverManager archiverManager;
053
054 /**
055 * Directory to unpack JARs into if needed
056 *
057 * @parameter expression="${project.build.directory}/theme/work"
058 * @required
059 */
060 private File workDirectory;
061
062 /**
063 * The directory where the webapp is built.
064 *
065 * @parameter expression="${project.build.directory}/${project.build.finalName}"
066 * @required
067 */
068 private File webappDirectory;
069
070 /**
071 * @parameter expression="${plugin.artifacts}"
072 * @required
073 */
074 private List pluginArtifacts;
075
076 private boolean findThemeDescriptor(File jarFile) throws MojoExecutionException {
077 ZipInputStream zip = null;
078 try {
079 zip = new ZipInputStream(new FileInputStream(jarFile));
080 while (zip.available() > 0) {
081 ZipEntry nextEntry = zip.getNextEntry();
082 if (nextEntry == null || nextEntry.isDirectory()) {
083 continue;
084 }
085 String name = nextEntry.getName();
086 if (name.equals("META-INF/tobago-theme.xml")) {
087 return true;
088 }
089 }
090 } catch (IOException e) {
091 throw new MojoExecutionException("Error find ThemeDescriptor", e);
092 } finally {
093 if (zip != null) {
094 try {
095 zip.close();
096 } catch (IOException e) {
097 // ignore
098 }
099 }
100 }
101
102 return false;
103 }
104
105 public void execute() throws MojoExecutionException {
106
107 Iterator artifacts = getProject().getDependencyArtifacts().iterator();
108 while (artifacts.hasNext()) {
109 if (!workDirectory.exists()) {
110 workDirectory.mkdirs();
111 }
112 Artifact artifact = (Artifact) artifacts.next();
113 getLog().debug("Expanding theme "+ artifact);
114
115 if (Artifact.SCOPE_COMPILE.equals(artifact.getScope())
116 && "jar".equals(artifact.getType()) && findThemeDescriptor(artifact.getFile())) {
117
118 String name = artifact.getFile().getName();
119 getLog().debug("Expanding theme "+ name);
120 File tempLocation = new File(workDirectory, name.substring(0, name.length() - 4));
121 boolean process = false;
122 if (!tempLocation.exists()) {
123 tempLocation.mkdirs();
124 process = true;
125 } else if (artifact.getFile().lastModified() > tempLocation.lastModified()) {
126 process = true;
127 }
128 if (process) {
129 File file = artifact.getFile();
130 try {
131 unpack(file, tempLocation);
132 String[] fileNames = getThemeFiles(tempLocation);
133 for (String fileName : fileNames) {
134
135 File fromFile = new File(tempLocation, fileName);
136 File toFile = new File(webappDirectory, fileName);
137 try {
138 FileUtils.copyFile(fromFile, toFile);
139 } catch (IOException e) {
140 throw new MojoExecutionException("Error copy file: " + fromFile + "to: " + toFile, e);
141 }
142 }
143 } catch (NoSuchArchiverException e) {
144 this.getLog().info("Skip unpacking dependency file with unknown extension: " + file.getPath());
145 }
146 }
147 }
148 }
149 }
150
151 private void unpack(File file, File location)
152 throws MojoExecutionException, NoSuchArchiverException {
153 String archiveExt = FileUtils.getExtension(file.getAbsolutePath()).toLowerCase();
154 try {
155 UnArchiver unArchiver = archiverManager.getUnArchiver(archiveExt);
156 unArchiver.setSourceFile(file);
157 unArchiver.setDestDirectory(location);
158 unArchiver.extract();
159 } catch (IOException e) {
160 throw new MojoExecutionException("Error unpacking file: " + file + "to: " + location, e);
161 } catch (ArchiverException e) {
162 throw new MojoExecutionException("Error unpacking file: " + file + "to: " + location, e);
163 }
164 }
165 }
166
167