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.List;
21  
22  import org.apache.commons.io.FileUtils;
23  import org.apache.maven.plugin.AbstractMojo;
24  import org.apache.maven.plugin.MojoExecutionException;
25  import org.apache.torque.util.SimpleScanner;
26  import org.codehaus.plexus.util.StringUtils;
27  
28  /**
29   * Create a location listing file that describes resources inside of a jar file
30   *
31   * @author Jeff Caddel
32   * @goal metainf
33   */
34  public class MetaInfMojo extends AbstractMojo {
35  
36  	/**
37  	 * Regular expression pattern for files to include
38  	 *
39  	 * @parameter expression="${metainf.include}" default-value="\*\*\/*"
40  	 * @required
41  	 */
42  	private String include;
43  
44  	/**
45  	 * Regular expression pattern for files to exclude
46  	 *
47  	 * @parameter expression="${metainf.exclude}" default-value="\*\*\/META-INF/*"
48  	 */
49  	private String exclude;
50  
51  	/**
52  	 * The directory to scan using the include/exclude patterns. Paths in <code>outputFile</code> are generated relative to this directory
53  	 *
54  	 * @parameter expression="${metainf.basedir}" default-value="${project.build.outputDirectory}"
55  	 * @required
56  	 */
57  	private File baseDir;
58  
59  	/**
60  	 * The file which will contain <code>classpath</code> references to the files that were located
61  	 *
62  	 * @parameter expression="${metainf.outputFile}" default-value="${project.build.outputDirectory}/META-INF/classpath.resources"
63  	 * @required
64  	 */
65  	private File outputFile;
66  
67  	/**
68  	 * The prefix to insert before the relative path name
69  	 *
70  	 * @parameter expression="${metainf.prefix}" default-value="classpath:"
71  	 */
72  	private String prefix;
73  
74  	@Override
75  	public void execute() throws MojoExecutionException {
76  		try {
77  			getLog().info("Examining - " + baseDir.getAbsolutePath());
78  			getLog().info("Include - " + include);
79  			getLog().info("Exclude - " + exclude);
80  			SimpleScanner scanner = new SimpleScanner(baseDir, include, exclude);
81  			List<File> files = scanner.getFiles();
82  			getLog().info("Located " + files.size() + " files");
83  			String content = getLocations(baseDir, files, prefix);
84  			getLog().info("Creating " + outputFile);
85  			FileUtils.writeStringToFile(outputFile, content);
86  		} catch (Exception e) {
87  			throw new MojoExecutionException("Unexpected error", e);
88  		}
89  	}
90  
91  	protected String getLocations(File baseDir, List<File> files, String prefix) throws IOException {
92  		StringBuilder sb = new StringBuilder();
93  		for (int i = 0; i < files.size(); i++) {
94  			if (i != 0) {
95  				sb.append("\n");
96  			}
97  			sb.append(getLocation(baseDir, files.get(i), prefix));
98  		}
99  		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 }