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