View Javadoc

1   package org.apache.myfaces.tobago.maven.plugin;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one or more
5    * contributor license agreements.  See the NOTICE file distributed with
6    * this work for additional information regarding copyright ownership.
7    * The ASF licenses this file to You under the Apache License, Version 2.0
8    * (the "License"); you may not use this file except in compliance with
9    * the License.  You may obtain a copy of the License at
10   *
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  import org.apache.maven.artifact.Artifact;
21  import org.apache.maven.plugin.MojoExecutionException;
22  import org.codehaus.plexus.archiver.ArchiverException;
23  import org.codehaus.plexus.archiver.UnArchiver;
24  import org.codehaus.plexus.archiver.manager.ArchiverManager;
25  import org.codehaus.plexus.archiver.manager.NoSuchArchiverException;
26  import org.codehaus.plexus.util.FileUtils;
27  
28  import java.io.File;
29  import java.io.IOException;
30  import java.io.FileInputStream;
31  import java.util.Iterator;
32  import java.util.List;
33  import java.util.Locale;
34  import java.util.zip.ZipInputStream;
35  import java.util.zip.ZipEntry;
36  
37  /**
38   *
39   * @version $Id: UnPackThemeMojo.java 601107 2007-12-04 22:12:14Z bommel $
40   * @goal resources
41   * @phase process-resources
42   * @requiresDependencyResolution compile
43   */
44  public class UnPackThemeMojo extends AbstractThemeMojo {
45    /**
46     * To look up Archiver/UnArchiver implementations
47     *
48     * @parameter expression="${component.org.codehaus.plexus.archiver.manager.ArchiverManager}"
49     * @required
50     */
51    private ArchiverManager archiverManager;
52  
53    /**
54     * Directory to unpack JARs into if needed
55     *
56     * @parameter expression="${project.build.directory}/theme/work"
57     * @required
58     */
59    private File workDirectory;
60  
61    /**
62     * The directory where the webapp is built.
63     *
64     * @parameter expression="${project.build.directory}/${project.build.finalName}"
65     * @required
66     */
67    private File webappDirectory;
68  
69    /**
70     * @parameter expression="${plugin.artifacts}"
71     * @required
72     */
73    private List pluginArtifacts;
74  
75    private boolean findThemeDescriptor(File jarFile) throws MojoExecutionException {
76      ZipInputStream zip = null;
77      try {
78        zip = new ZipInputStream(new FileInputStream(jarFile));
79        while (zip.available() > 0) {
80          ZipEntry nextEntry = zip.getNextEntry();
81          if (nextEntry == null || nextEntry.isDirectory()) {
82            continue;
83          }
84          String name = nextEntry.getName();
85          if (name.equals("META-INF/tobago-theme.xml")) {
86            return true;
87          }
88        }
89      } catch (IOException e) {
90        throw new MojoExecutionException("Error find ThemeDescriptor", e);
91      } finally {
92        if (zip != null) {
93          try {
94            zip.close();
95          } catch (IOException e) {
96            // ignore
97          }
98        }
99      }
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