001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.maven_plugin.jdee;
019:
020: import java.io.BufferedOutputStream;
021: import java.io.File;
022: import java.io.FileOutputStream;
023: import java.io.IOException;
024: import java.io.OutputStreamWriter;
025: import java.io.Writer;
026: import java.util.HashSet;
027: import java.util.Iterator;
028: import java.util.List;
029: import java.util.Properties;
030: import java.util.Set;
031:
032: import org.apache.commons.io.FileUtils;
033: import org.apache.cxf.tools.wsdlto.core.VelocityWriter;
034: import org.apache.maven.artifact.Artifact;
035: import org.apache.maven.artifact.factory.ArtifactFactory;
036: import org.apache.maven.plugin.AbstractMojo;
037: import org.apache.maven.plugin.MojoExecutionException;
038: import org.apache.maven.project.MavenProject;
039: import org.apache.velocity.Template;
040: import org.apache.velocity.VelocityContext;
041: import org.apache.velocity.app.Velocity;
042:
043: /**
044: * A super-simple Mojo to emit JDEE project files.
045: *
046: * @goal jdee
047: * @description Outputs a JDEE project file.
048: * @requiresDependencyResolution test
049: */
050:
051: // TODO:
052: // * include the generated srouce root
053: //
054: public class JdeeMojo extends AbstractMojo {
055: /**
056: * The Maven Project.
057: *
058: * @parameter expression="${project}"
059: * @required
060: * @readonly
061: */
062: protected MavenProject project;
063:
064: /**
065: * @component
066: */
067: protected ArtifactFactory artifactFactory;
068:
069: /**
070: * Build directory
071: *
072: * @parameter expression="${project.build.outputDirectory}"
073: * @required
074: */
075: private File buildDirectory;
076:
077: /**
078: * The set of dependencies required by the project
079: * @parameter default-value="${project.artifacts}"
080: * @required
081: * @readonly
082: */
083: private java.util.Set dependencies;
084:
085: private String getVelocityLogFile(String log) {
086: return new File(buildDirectory, log).toString();
087: }
088:
089: @SuppressWarnings("unchecked")
090: private Set<String> getSourceDirs() throws Exception {
091: Set<String> results = new HashSet<String>();
092: File file = new File(".jdee_sources");
093: if (file.exists()) {
094: results.addAll(FileUtils.readLines(file));
095: } else {
096: file.createNewFile();
097: }
098:
099: results.addAll(emitPaths(project.getCompileSourceRoots()));
100: results.addAll(emitPaths(project.getTestCompileSourceRoots()));
101: FileUtils.writeLines(file, results);
102: return results;
103: }
104:
105: @SuppressWarnings("unchecked")
106: private Set<String> getGlobalClasspath() throws Exception {
107: Set<String> results = new HashSet<String>();
108: File file = new File(".jdee_classpath");
109: if (file.exists()) {
110: results.addAll(FileUtils.readLines(file));
111: } else {
112: file.createNewFile();
113: }
114:
115: results
116: .addAll(emitPaths(project.getCompileClasspathElements()));
117: results.addAll(emitPaths(project.getTestClasspathElements()));
118: results.addAll(emitPaths(project.getSystemClasspathElements()));
119: results.addAll(getDependencies());
120: FileUtils.writeLines(file, results);
121: return results;
122: }
123:
124: private Set<String> getDependencies() throws Exception {
125: Set<String> results = new HashSet<String>();
126: if (dependencies != null && !dependencies.isEmpty()) {
127: for (Iterator it = dependencies.iterator(); it.hasNext();) {
128: Artifact artifact = (Artifact) it.next();
129: results.add(artifact.getFile().toString().replace("\\",
130: "/"));
131: }
132: }
133: return results;
134: }
135:
136: private Set<String> emitPaths(List paths) throws IOException {
137: Set<String> np = new HashSet<String>();
138: for (Object path : paths) {
139: np.add(path.toString().replace("\\", "/"));
140: }
141: return np;
142: }
143:
144: // TODO: Reuse the velocity in the tools
145: private void initVelocity() throws Exception {
146: Properties props = new Properties();
147: String clzName = "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader";
148: props.put("resource.loader", "class");
149: props.put("class.resource.loader.class", clzName);
150: props.put("runtime.log", getVelocityLogFile("velocity.log"));
151:
152: Velocity.init(props);
153: }
154:
155: private void generatePrj() throws Exception {
156: initVelocity();
157:
158: String templateFile = "/org/apache/cxf/maven_plugin/jdee/prj.vm";
159: Template tmpl = Velocity.getTemplate(templateFile);
160: if (tmpl == null) {
161: throw new RuntimeException("Can not load template file: "
162: + templateFile);
163: }
164:
165: File outputFile = new File("prj.el");
166:
167: VelocityContext ctx = new VelocityContext();
168: ctx.put("SOURCE_DIRS", getSourceDirs());
169: ctx.put("GLOBAL_CP", getGlobalClasspath());
170:
171: Writer outputs = new OutputStreamWriter(
172: new BufferedOutputStream(new FileOutputStream(
173: outputFile)), "UTF-8");
174: VelocityWriter writer = new VelocityWriter(outputs);
175:
176: tmpl.merge(ctx, writer);
177: writer.close();
178: }
179:
180: public void execute() throws MojoExecutionException {
181: try {
182: generatePrj();
183: } catch (Exception e) {
184: getLog().debug(e);
185: throw new MojoExecutionException(e.getMessage(), e);
186: }
187: }
188: }
|