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.List;
021    
022    import org.apache.commons.io.FileUtils;
023    import org.apache.maven.plugin.AbstractMojo;
024    import org.apache.maven.plugin.MojoExecutionException;
025    import org.apache.torque.util.SimpleScanner;
026    import org.codehaus.plexus.util.StringUtils;
027    
028    /**
029     * Create a location listing file that describes resources inside of a jar file
030     *
031     * @author Jeff Caddel
032     * @goal metainf
033     */
034    public class MetaInfMojo extends AbstractMojo {
035    
036            /**
037             * Regular expression pattern for files to include
038             *
039             * @parameter expression="${metainf.include}" default-value="\*\*\/*"
040             * @required
041             */
042            private String include;
043    
044            /**
045             * Regular expression pattern for files to exclude
046             *
047             * @parameter expression="${metainf.exclude}" default-value="\*\*\/META-INF/*"
048             */
049            private String exclude;
050    
051            /**
052             * The directory to scan using the include/exclude patterns. Paths in <code>outputFile</code> are generated relative to this directory
053             *
054             * @parameter expression="${metainf.basedir}" default-value="${project.build.outputDirectory}"
055             * @required
056             */
057            private File baseDir;
058    
059            /**
060             * The file which will contain <code>classpath</code> references to the files that were located
061             *
062             * @parameter expression="${metainf.outputFile}" default-value="${project.build.outputDirectory}/META-INF/classpath.resources"
063             * @required
064             */
065            private File outputFile;
066    
067            /**
068             * The prefix to insert before the relative path name
069             *
070             * @parameter expression="${metainf.prefix}" default-value="classpath:"
071             */
072            private String prefix;
073    
074            @Override
075            public void execute() throws MojoExecutionException {
076                    try {
077                            getLog().info("Examining - " + baseDir.getAbsolutePath());
078                            getLog().info("Include - " + include);
079                            getLog().info("Exclude - " + exclude);
080                            SimpleScanner scanner = new SimpleScanner(baseDir, include, exclude);
081                            List<File> files = scanner.getFiles();
082                            getLog().info("Located " + files.size() + " files");
083                            String content = getLocations(baseDir, files, prefix);
084                            getLog().info("Creating " + outputFile);
085                            FileUtils.writeStringToFile(outputFile, content);
086                    } catch (Exception e) {
087                            throw new MojoExecutionException("Unexpected error", e);
088                    }
089            }
090    
091            protected String getLocations(File baseDir, List<File> files, String prefix) throws IOException {
092                    StringBuilder sb = new StringBuilder();
093                    for (int i = 0; i < files.size(); i++) {
094                            if (i != 0) {
095                                    sb.append("\n");
096                            }
097                            sb.append(getLocation(baseDir, files.get(i), prefix));
098                    }
099                    return sb.toString();
100            }
101    
102            protected String getLocation(File baseDir, File file, String prefix) throws IOException {
103                    String dir = baseDir.getCanonicalPath();
104                    String path = file.getCanonicalPath();
105                    int pos = dir.length() + 1;
106                    return prefix + StringUtils.substring(path, pos);
107            }
108    
109            public String getInclude() {
110                    return include;
111            }
112    
113            public void setInclude(String include) {
114                    this.include = include;
115            }
116    
117            public String getExclude() {
118                    return exclude;
119            }
120    
121            public void setExclude(String exclude) {
122                    this.exclude = exclude;
123            }
124    
125            public File getBaseDir() {
126                    return baseDir;
127            }
128    
129            public void setBaseDir(File baseDir) {
130                    this.baseDir = baseDir;
131            }
132    
133            public File getOutputFile() {
134                    return outputFile;
135            }
136    
137            public void setOutputFile(File outputFile) {
138                    this.outputFile = outputFile;
139            }
140    
141            public String getPrefix() {
142                    return prefix;
143            }
144    
145            public void setPrefix(String prefix) {
146                    this.prefix = prefix;
147            }
148    
149    }