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.eclipse;
019:
020: import java.io.File;
021: import java.util.ArrayList;
022: import java.util.Iterator;
023: import java.util.List;
024: import java.util.Set;
025:
026: import org.apache.cxf.common.util.ReflectionUtil;
027: import org.apache.cxf.common.util.StringUtils;
028: import org.apache.cxf.tools.common.VelocityGenerator;
029: import org.apache.cxf.tools.util.FileWriterUtil;
030: import org.apache.maven.artifact.Artifact;
031: import org.apache.maven.plugin.AbstractMojo;
032: import org.apache.maven.plugin.MojoExecutionException;
033: import org.apache.maven.plugin.MojoFailureException;
034: import org.apache.maven.project.MavenProject;
035:
036: /**
037: * @goal eclipseplugin
038: * @description CXF eclipse plugin generator
039: */
040: public class EclipsePluginMojo extends AbstractMojo {
041: private static final String LIB_PATH = "lib";
042: private static final String ECLIPSE_VERSION = "3.2";
043:
044: /**
045: * @parameter expression="${project}"
046: * @required
047: */
048: MavenProject project;
049:
050: /**
051: * The set of dependencies required by the project
052: * @parameter default-value="${project.artifacts}"
053: * @required
054: * @readonly
055: */
056: Set dependencies;
057:
058: /**
059: * @parameter expression="${project.build.directory}";
060: * @required
061: */
062: File targetDir;
063:
064: private String getTemplateFile(String version) {
065: return "/org/apache/cxf/maven_plugin/eclipse/" + version
066: + "/MANIFEST.vm";
067: }
068:
069: private List<File> listJars() throws Exception {
070: List<File> jars = new ArrayList<File>();
071: if (dependencies != null && !dependencies.isEmpty()) {
072: for (Iterator it = dependencies.iterator(); it.hasNext();) {
073: Artifact artifact = (Artifact) it.next();
074: File oldJar = artifact.getFile();
075: jars.add(oldJar);
076: }
077: }
078: return jars;
079: }
080:
081: public void execute() throws MojoExecutionException,
082: MojoFailureException {
083:
084: try {
085: generatePluginXML(listJars());
086: } catch (Exception e) {
087: e.printStackTrace();
088: throw new MojoExecutionException(e.getMessage(), e);
089: }
090: }
091:
092: private String getVersion() {
093: return StringUtils.formatVersionNumber(project.getVersion());
094: }
095:
096: private List<String> getExportedPackages(List<File> jars)
097: throws Exception {
098: List<String> packages = new ArrayList<String>();
099: for (File jarFile : jars) {
100: packages.addAll(ReflectionUtil.getPackagesFromJar(jarFile));
101: }
102: return packages;
103: }
104:
105: private void generatePluginXML(List<File> jars) throws Exception {
106: VelocityGenerator velocity = new VelocityGenerator();
107:
108: String templateFile = getTemplateFile(ECLIPSE_VERSION);
109:
110: velocity.setAttributes("ECLIPSE_VERSION", ECLIPSE_VERSION);
111: velocity.setAttributes("PLUGIN_VERSION", getVersion());
112: velocity.setAttributes("GROUP_ID", project.getGroupId());
113: velocity.setAttributes("libPath", LIB_PATH);
114: velocity.setAttributes("jars", jars);
115:
116: velocity.setAttributes("exportedPackages",
117: getExportedPackages(jars));
118: File outputFile = new File(targetDir, "MANIFEST.MF");
119:
120: velocity.doWrite(templateFile, FileWriterUtil
121: .getWriter(outputFile));
122: }
123: }
|