001 /**
002 * Copyright 2004-2012 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.apache.torque.mojo;
017
018 import java.io.File;
019 import java.io.IOException;
020 import java.util.Collections;
021 import java.util.List;
022
023 import org.apache.commons.io.FileUtils;
024 import org.apache.maven.plugin.AbstractMojo;
025 import org.apache.maven.plugin.MojoExecutionException;
026 import org.apache.torque.util.SimpleScanner;
027 import org.codehaus.plexus.util.StringUtils;
028
029 /**
030 * Create a location listing file that describes resources inside of a jar file
031 *
032 * @author Jeff Caddel
033 * @goal metainf
034 */
035 public class MetaInfMojo extends AbstractMojo {
036
037 /**
038 * Regular expression pattern for files to include
039 *
040 * @parameter expression="${metainf.include}" default-value="\*\*\/*"
041 * @required
042 */
043 private String include;
044
045 /**
046 * Regular expression pattern for files to exclude
047 *
048 * @parameter expression="${metainf.exclude}" default-value="\*\*\/META-INF/*"
049 */
050 private String exclude;
051
052 /**
053 * The directory to scan using the include/exclude patterns. Paths in <code>outputFile</code> are generated relative to this directory
054 *
055 * @parameter expression="${metainf.basedir}" default-value="${project.build.outputDirectory}"
056 * @required
057 */
058 private File baseDir;
059
060 /**
061 * The file which will contain <code>classpath</code> references to the files that were located
062 *
063 * @parameter expression="${metainf.outputFile}" default-value="${project.build.outputDirectory}/META-INF/classpath.resources"
064 * @required
065 */
066 private File outputFile;
067
068 /**
069 * The prefix to insert before the relative path name
070 *
071 * @parameter expression="${metainf.prefix}" default-value="classpath:"
072 */
073 private String prefix;
074
075 @Override
076 public void execute() throws MojoExecutionException {
077 try {
078 getLog().info("Examining - " + baseDir.getCanonicalPath());
079 getLog().info("Include - " + include);
080 getLog().info("Exclude - " + exclude);
081 SimpleScanner scanner = new SimpleScanner(baseDir, include, exclude);
082 List<File> files = scanner.getFiles();
083 getLog().info("Located " + files.size() + " files");
084 String content = getLocations(baseDir, files, prefix);
085 getLog().info("Creating " + outputFile.getCanonicalPath());
086 FileUtils.writeStringToFile(outputFile, content);
087 } catch (Exception e) {
088 throw new MojoExecutionException("Unexpected error", e);
089 }
090 }
091
092 protected String getLocations(File baseDir, List<File> files, String prefix) throws IOException {
093 Collections.sort(files);
094 StringBuilder sb = new StringBuilder();
095 for (int i = 0; i < files.size(); i++) {
096 if (i != 0) {
097 sb.append("\n");
098 }
099 sb.append(getLocation(baseDir, files.get(i), prefix));
100 }
101 return sb.toString();
102 }
103
104 protected String getLocation(File baseDir, File file, String prefix) throws IOException {
105 String dir = baseDir.getCanonicalPath();
106 String path = file.getCanonicalPath();
107 int pos = dir.length() + 1;
108 return prefix + StringUtils.substring(path, pos);
109 }
110
111 public String getInclude() {
112 return include;
113 }
114
115 public void setInclude(String include) {
116 this.include = include;
117 }
118
119 public String getExclude() {
120 return exclude;
121 }
122
123 public void setExclude(String exclude) {
124 this.exclude = exclude;
125 }
126
127 public File getBaseDir() {
128 return baseDir;
129 }
130
131 public void setBaseDir(File baseDir) {
132 this.baseDir = baseDir;
133 }
134
135 public File getOutputFile() {
136 return outputFile;
137 }
138
139 public void setOutputFile(File outputFile) {
140 this.outputFile = outputFile;
141 }
142
143 public String getPrefix() {
144 return prefix;
145 }
146
147 public void setPrefix(String prefix) {
148 this.prefix = prefix;
149 }
150
151 }