View Javadoc

1   /**
2    * Copyright 2004-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.torque.mojo;
17  
18  import java.io.File;
19  import java.io.IOException;
20  import java.util.Collections;
21  import java.util.List;
22  
23  import org.apache.commons.io.FileUtils;
24  import org.apache.maven.plugin.AbstractMojo;
25  import org.apache.maven.plugin.MojoExecutionException;
26  import org.apache.torque.util.SimpleScanner;
27  import org.codehaus.plexus.util.StringUtils;
28  
29  /**
30   * Create a location listing file that describes resources inside of a jar file
31   *
32   * @author Jeff Caddel
33   * @goal metainf
34   */
35  public class MetaInfMojo extends AbstractMojo {
36  
37  	/**
38  	 * Regular expression pattern for files to include
39  	 *
40  	 * @parameter expression="${metainf.include}" default-value="\*\*\/*"
41  	 * @required
42  	 */
43  	private String include;
44  
45  	/**
46  	 * Regular expression pattern for files to exclude
47  	 *
48  	 * @parameter expression="${metainf.exclude}" default-value="\*\*\/META-INF/*"
49  	 */
50  	private String exclude;
51  
52  	/**
53  	 * The directory to scan using the include/exclude patterns. Paths in <code>outputFile</code> are generated relative to this directory
54  	 *
55  	 * @parameter expression="${metainf.basedir}" default-value="${project.build.outputDirectory}"
56  	 * @required
57  	 */
58  	private File baseDir;
59  
60  	/**
61  	 * The file which will contain <code>classpath</code> references to the files that were located
62  	 *
63  	 * @parameter expression="${metainf.outputFile}" default-value="${project.build.outputDirectory}/META-INF/classpath.resources"
64  	 * @required
65  	 */
66  	private File outputFile;
67  
68  	/**
69  	 * The prefix to insert before the relative path name
70  	 *
71  	 * @parameter expression="${metainf.prefix}" default-value="classpath:"
72  	 */
73  	private String prefix;
74  
75  	@Override
76  	public void execute() throws MojoExecutionException {
77  		try {
78  			getLog().info("Examining - " + baseDir.getCanonicalPath());
79  			getLog().info("Include - " + include);
80  			getLog().info("Exclude - " + exclude);
81  			SimpleScanner scanner = new SimpleScanner(baseDir, include, exclude);
82  			List<File> files = scanner.getFiles();
83  			getLog().info("Located " + files.size() + " files");
84  			String content = getLocations(baseDir, files, prefix);
85  			getLog().info("Creating " + outputFile.getCanonicalPath());
86  			FileUtils.writeStringToFile(outputFile, content);
87  		} catch (Exception e) {
88  			throw new MojoExecutionException("Unexpected error", e);
89  		}
90  	}
91  
92  	protected String getLocations(File baseDir, List<File> files, String prefix) throws IOException {
93  		Collections.sort(files);
94  		StringBuilder sb = new StringBuilder();
95  		for (int i = 0; i < files.size(); i++) {
96  			if (i != 0) {
97  				sb.append("\n");
98  			}
99  			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 }