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.archiver.MavenArchiveConfiguration;
021    import org.apache.maven.artifact.Artifact;
022    import org.apache.maven.plugin.MojoExecutionException;
023    import org.apache.maven.project.MavenProject;
024    import org.apache.maven.project.MavenProjectHelper;
025    import org.codehaus.plexus.archiver.ArchiverException;
026    import org.codehaus.plexus.archiver.zip.ZipArchiver;
027    import org.codehaus.plexus.util.FileUtils;
028    
029    import java.io.File;
030    import java.io.IOException;
031    import java.util.ArrayList;
032    import java.util.List;
033    
034    /**
035     * Created by IntelliJ IDEA.
036     * User: bommel
037     * Date: 23.10.2005
038     * Time: 16:33:04
039     *
040     * @goal theme
041     * @phase package
042     * @requiresDependencyResolution compile
043     */
044    public class PackThemeMojo extends AbstractThemeMojo {
045    
046      /**
047       * Maven ProjectHelper
048       *
049       * @component
050       */
051      private MavenProjectHelper projectHelper;
052    
053      /**
054       * The directory for the generated JAR.
055       *
056       * @parameter expression="${project.build.directory}"
057       * @required
058       */
059      private String outputDirectory;
060    
061    
062      /**
063       * The name of the generated jar.
064       *
065       * @parameter expression="${project.build.finalName}"
066       * @required
067       */
068      private String jarName;
069    
070    
071      /**
072       * The directory where the webapp is built.
073       *
074       * @parameter expression="${project.build.directory}/${project.build.finalName}"
075       * @required
076       */
077      private File webappDirectory;
078    
079      /**
080       * The Jar archiver.
081       *
082       * @parameter expression="${component.org.codehaus.plexus.archiver.Archiver#zip}"
083       * @required
084       */
085      // TODO Zip or jar ????
086      private ZipArchiver archiver;
087    
088      /**
089       * Single directory for extra files to include in the ZIP.
090       *
091       * @parameter expression="${basedir}/src/main/resources/org/apache/myfaces/tobago/renderkit"
092       * @required
093       */
094      private File warSourceDirectory;
095    
096      public File getWarSourceDirectory() {
097        return warSourceDirectory;
098      }
099    
100      public File getWebappDirectory() {
101        return webappDirectory;
102      }
103    
104      /**
105       * The maven archive configuration to use.
106       *
107       * @parameter
108       */
109      private MavenArchiveConfiguration archive = new MavenArchiveConfiguration();
110    
111      public static final String WEB_INF = "WEB-INF";
112    
113      /**
114       * Executes the WarMojo on the current project.
115       *
116       * @throws MojoExecutionException
117       *          if an error occured while building the webapp
118       */
119      public void execute()
120          throws MojoExecutionException {
121        File jarFile = new File(outputDirectory, jarName +"-THEME.jar");
122    
123        try {
124          performPackaging(jarFile);
125          projectHelper.attachArtifact(getProject(), "jar", "THEME", jarFile);
126    
127        } catch (Exception e) {
128          // TODO: improve error handling
129          throw new MojoExecutionException("Error assembling theme", e);
130        }
131      }
132    
133    
134      private void performPackaging(File jarFile)
135          throws IOException, ArchiverException,
136          MojoExecutionException {
137        buildExplodedTheme(getWebappDirectory());
138    
139        getLog().info("Generating theme " + jarFile.getAbsolutePath());
140        archiver.addDirectory(getWebappDirectory(), getIncludes(), getExcludes());
141        archiver.setDestFile(jarFile);
142        archiver.createArchive();
143      }
144    
145      public void buildExplodedTheme(File zipDirectory)
146          throws MojoExecutionException {
147        getLog().info("Exploding theme...");
148        zipDirectory.mkdirs();
149    
150    
151        try {
152          copyResources(getWarSourceDirectory(), zipDirectory);
153    
154          buildTheme(getProject(), getWebappDirectory());
155        } catch (IOException e) {
156          throw new MojoExecutionException("Could not explode theme...", e);
157        }
158      }
159    
160      public void copyResources(File sourceDirectory, File webappDirectory)
161          throws IOException {
162        if (!sourceDirectory.equals(webappDirectory)) {
163          getLog().info("Copy theme resources to " + webappDirectory.getAbsolutePath());
164          if (getWarSourceDirectory().exists()) {
165            String[] fileNames = getThemeFiles(sourceDirectory);
166            for (int i = 0; i < fileNames.length; i++) {
167              FileUtils.copyFile(new File(sourceDirectory, fileNames[i]),
168                  new File(webappDirectory, "tobago/" + fileNames[i]));
169            }
170          }
171        }
172      }
173    
174      public void buildTheme(MavenProject project, File webappDirectory)
175          throws IOException {
176        getLog().info("Assembling theme " + project.getArtifactId() + " in " + webappDirectory);
177    
178        File libDirectory = new File(webappDirectory, WEB_INF + "/lib");
179        libDirectory.mkdirs();
180       // List artifacts =
181    
182        //for (Iterator iter = artifacts.iterator(); iter.hasNext();) {
183          Artifact artifact = project.getArtifact();
184          getLog().debug(artifact.toString());
185          FileUtils.copyFile(artifact.getFile(), new File(libDirectory, artifact.getFile().getName()));
186        //}
187      }
188    
189    
190      protected String[] getExcludes() {
191        List excludeList = new ArrayList(FileUtils.getDefaultExcludesAsList());
192        return (String[]) excludeList.toArray(new String[]{});
193      }
194    
195      protected String[] getIncludes() {
196        return new String[]{"**"};
197      }
198    
199    }