View Javadoc

1   /*
2    * Copyright (C) 2000 - 2008 Grid Systems, S.A.
3    *
4    * This program is free software; you can redistribute it and/or modify
5    * it under the terms of the GNU General Public License, version 2, as
6    * published by the Free Software Foundation.
7    *
8    * This program is distributed in the hope that it will be useful,
9    * but WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11   * GNU General Public License for more details.
12   *
13   * You should have received a copy of the GNU General Public License
14   * along with this program; if not, write to the Free Software
15   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16   */
17  package com.gridsystems.maven.furabuild;
18  
19  import java.io.File;
20  import java.io.IOException;
21  import java.io.PrintStream;
22  import java.util.Collections;
23  import java.util.HashSet;
24  import java.util.Iterator;
25  import java.util.List;
26  import java.util.Properties;
27  import java.util.Set;
28  import java.util.jar.Attributes;
29  import java.util.jar.JarFile;
30  import java.util.jar.Manifest;
31  
32  import org.apache.maven.artifact.Artifact;
33  import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
34  import org.apache.maven.artifact.resolver.ArtifactResolutionException;
35  import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
36  import org.apache.maven.artifact.versioning.VersionRange;
37  import org.apache.maven.model.Dependency;
38  import org.apache.maven.plugin.MojoExecutionException;
39  import org.codehaus.plexus.util.IOUtil;
40  
41  /**
42   * Goal for parsing the tag file.
43   *
44   * @goal generate-plugin-descriptor
45   * @phase generate-resources
46   *
47   * @author Rodrigo Ruiz
48   */
49  public class PluginGenMojo extends AbstractFuraBuildMojo {
50  
51    /**
52     * {@inheritDoc}
53     */
54    public void execute() throws MojoExecutionException {
55      Properties props = project.getProperties();
56  
57      String pluginName = props.getProperty("pluginName");
58      String pluginClass = props.getProperty("pluginClass");
59      String codeName = props.getProperty("codeName");
60      String eula = props.getProperty("eula");
61  
62      if (pluginName == null || pluginClass == null) {
63        getLog().info("No plugin name and/or class found. Descriptor not generated");
64        return;
65      }
66  
67      if (props.getProperty("tag.version") == null) {
68        processLocation(props, location, "tag.version", defaultTag);
69      }
70  
71      String year = project.getInceptionYear();
72      String company = project.getOrganization().getName();
73      String cright = "Copyright (c)" + year + " " + company;
74  
75      String descr = project.getDescription();
76      if (descr == null) {
77        descr = "";
78      }
79  
80      getLog().debug("Generating plugin descriptor at " + outputDirectory);
81  
82      outputDirectory.mkdirs();
83      File f = new File(outputDirectory, "plugin.xml");
84  
85      PrintStream out = null;
86      try {
87        out = new PrintStream(f);
88        out.println("<?xml version='1.0' encoding='UTF-8'?>");
89        out.println("<plugin>");
90        printTag(out, "  name", pluginName);
91        printTag(out, "  description", descr);
92        printTag(out, "  copyright", cright);
93        out.println("  <version>");
94        printTag(out, "    major", props.getProperty("tag.version.major"));
95        printTag(out, "    minor", props.getProperty("tag.version.minor"));
96        printTag(out, "    release", props.getProperty("tag.version.release"));
97        printTag(out, "    build", props.getProperty("tag.version.build"));
98        printTag(out, "    codeName", codeName);
99        out.println("  </version>");
100       printTag(out, "  class", pluginClass);
101 
102       printDependencies(out);
103 
104       printTag(out, "  eula", eula);
105       out.println("</plugin>");
106     } catch (IOException e) {
107       throw new MojoExecutionException("Error writing plugin.xml", e);
108     } finally {
109       IOUtil.close(out);
110     }
111 
112     projectHelper.addResource(
113       project,
114       outputDirectory.getAbsolutePath(),
115       Collections.singletonList("plugin.xml"),
116       Collections.EMPTY_LIST
117     );
118   }
119 
120   /**
121    * Gets the set of artifacts in the project.
122    *
123    * @return A Set containing all dependency artifacts
124    */
125   @SuppressWarnings("unchecked")
126   private Set<Artifact> getArtifacts() {
127     Set<Artifact> artifacts = project.getArtifacts();
128     if (artifacts == null || artifacts.isEmpty()) {
129       artifacts = createArtifacts();
130       project.setArtifacts(artifacts);
131     }
132     return artifacts;
133   }
134 
135   /**
136    * Creates the set of artifacts for the project.
137    *
138    * @return A Set containing all dependency artifacts
139    */
140   @SuppressWarnings("unchecked")
141   private Set<Artifact> createArtifacts() {
142 
143     List<?> dependencies = project.getDependencies();
144     List<?> remoteRepositories = Collections.EMPTY_LIST;
145     Set<Artifact> dependencyArtifacts = new HashSet<Artifact>();
146 
147     for (Iterator<?> it = dependencies.iterator(); it.hasNext();) {
148       Dependency dep = (Dependency)it.next();
149 
150       VersionRange vr = VersionRange.createFromVersion(dep.getVersion());
151 
152       Artifact artifact = factory.createDependencyArtifact(
153         dep.getGroupId(), dep.getArtifactId(), vr,
154         dep.getType(), dep.getClassifier(), dep.getScope()
155       );
156       dependencyArtifacts.add(artifact);
157     }
158 
159     try {
160       ArtifactResolutionResult result = resolver.resolveTransitively(
161         dependencyArtifacts, project.getArtifact(), remoteRepositories,
162         local, source);
163 
164       return result.getArtifacts();
165     } catch (ArtifactResolutionException e) {
166       getLog().warn("Skipping: " + e.getArtifactId() + ". It cannot be resolved.");
167     } catch (ArtifactNotFoundException e) {
168       getLog().warn("Skipping: " + e.getArtifactId() + ". It cannot be resolved.");
169     }
170 
171     return dependencyArtifacts;
172   }
173 
174   /**
175    * Prints the list of plugin dependencies.
176    *
177    * @param out Stream to print to
178    */
179   private void printDependencies(PrintStream out) throws MojoExecutionException {
180     out.println("  <dependencies>");
181 
182     Set<?> artifacts = getArtifacts();
183     if (artifacts == null || artifacts.isEmpty()) {
184       getLog().info("No dependencies found in project " + project.getName());
185 
186     } else {
187       getLog().debug("Found " + artifacts.size() + " artifacts");
188       for (Iterator<?> it = artifacts.iterator(); it.hasNext();) {
189         Artifact art = (Artifact)it.next();
190         getLog().debug("Artifact found: " + art);
191         try {
192           JarFile f = new JarFile(art.getFile());
193           Manifest manifest = f.getManifest();
194           Attributes attrs = (manifest == null) ? null : manifest.getMainAttributes();
195           if (attrs != null) {
196             String pluginName = attrs.getValue("Plugin-Name");
197             String version = attrs.getValue("Plugin-Version");
198 
199             if (pluginName != null && pluginName.length() > 0) {
200               getLog().info("Found dependency on " + pluginName + ":" + version);
201               out.print("    <plugin name='");
202               out.print(pluginName);
203               out.print("' version='");
204               out.print(version);
205               out.println("'/>");
206             }
207           }
208           f.close();
209         } catch (IOException e) {
210           // ignore
211         }
212       }
213     }
214 
215     out.println("  </dependencies>");
216   }
217 
218   /**
219    * Prints a tag into <tt>out</tt>.
220    *
221    * @param out  Stream to print to
222    * @param tag  Indented tag name
223    * @param text Tag contents
224    */
225   private void printTag(PrintStream out, String tag, String text) {
226     if (text != null) {
227       int pos = tag.lastIndexOf(' ');
228       String indent = tag.substring(0, pos + 1);
229       String tagName = tag.substring(pos + 1);
230 
231       out.print(indent);
232       out.print('<');
233       out.print(tagName);
234       out.print('>');
235       out.print(text);
236       out.print("</");
237       out.print(tagName);
238       out.println('>');
239     }
240   }
241 
242 }