View Javadoc
1   package org.codehaus.plexus.maven.plugin;
2   
3   /*
4    * The MIT License
5    *
6    * Copyright (c) 2004-2006, The Codehaus
7    *
8    * Permission is hereby granted, free of charge, to any person obtaining a copy of
9    * this software and associated documentation files (the "Software"), to deal in
10   * the Software without restriction, including without limitation the rights to
11   * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12   * of the Software, and to permit persons to whom the Software is furnished to do
13   * so, subject to the following conditions:
14   *
15   * The above copyright notice and this permission notice shall be included in all
16   * copies or substantial portions of the Software.
17   *
18   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24   * SOFTWARE.
25   */
26  
27  import java.io.BufferedWriter;
28  import java.io.File;
29  import java.io.FileWriter;
30  import java.util.ArrayList;
31  import java.util.Collections;
32  import java.util.List;
33  
34  import org.apache.maven.artifact.handler.ArtifactHandler;
35  import org.apache.maven.plugin.AbstractMojo;
36  import org.apache.maven.plugin.MojoExecutionException;
37  import org.apache.maven.project.MavenProject;
38  import org.apache.maven.project.MavenProjectHelper;
39  import org.codehaus.plexus.cdc.ComponentDescriptorWriter;
40  import org.codehaus.plexus.component.repository.cdc.ComponentDescriptor;
41  import org.codehaus.plexus.component.repository.cdc.ComponentSetDescriptor;
42  import org.codehaus.plexus.util.FileUtils;
43  import org.codehaus.plexus.util.IOUtil;
44  import org.slf4j.Logger;
45  import org.slf4j.LoggerFactory;
46  
47  /**
48   * @author <a href='mailto:rahul.thakur.xdev@gmail.com'>Rahul Thakur</a>
49   * @since 1.3.4
50   */
51  public abstract class AbstractDescriptorMojo
52      extends AbstractMojo
53  {
54      protected static final String COMPILE_SCOPE = "compile";
55  
56      protected static final String TEST_SCOPE = "test";
57  
58      protected final Logger log = LoggerFactory.getLogger(getClass());
59      
60      /**
61       * @parameter expression="META-INF/plexus/components.xml"
62       * @required
63       */
64      protected String fileName;
65  
66      /**
67       * Whether to generate a Plexus Container descriptor instead of a component descriptor.
68       *
69       * @parameter default-value="false"
70       * @required
71       */
72      private boolean containerDescriptor;
73  
74      /**
75       * @parameter expression="${project}"
76       * @required
77       */
78      private MavenProject mavenProject;
79  
80      /**
81       * @parameter
82       */
83      private ComponentDescriptor[] roleDefaults;
84  
85      /**
86       * @parameter
87       */
88      private ComponentDescriptorExtractor[] extractors;
89  
90      /**
91       * @component
92       */
93      private ComponentDescriptorWriter writer;
94  
95      /**
96       * @component
97       */
98      private MavenProjectHelper mavenProjectHelper;
99  
100     protected MavenProject getMavenProject()
101     {
102         return mavenProject;
103     }
104 
105     protected MavenProjectHelper getMavenProjectHelper()
106     {
107         return mavenProjectHelper;
108     }
109 
110     // -----------------------------------------------------------------------
111     //
112     // -----------------------------------------------------------------------
113 
114     protected void generateDescriptor(final String scope, final File outputFile) throws MojoExecutionException {
115         assert scope != null;
116         assert outputFile != null;
117 
118         // If no extractors are configured then use a default (javadoc-style source extraction)
119         if (extractors == null || extractors.length == 0) {
120             extractors = new ComponentDescriptorExtractor[] {
121                 new SourceComponentDescriptorExtractor(),
122             };
123         }
124 
125         List descriptors = new ArrayList();
126 
127         for (int i=0; i<extractors.length; i++) {
128             getLog().debug("Using extractor: " + extractors[i]);
129             
130             try {
131                 List list = extractors[i].extract(getMavenProject(), scope, roleDefaults);
132                 if (list != null && !list.isEmpty()) {
133                     descriptors.addAll(list);
134                 }
135             }
136             catch (Exception e) {
137                 throw new MojoExecutionException("Failed to extract descriptors", e);
138             }
139         }
140 
141         if (descriptors.size() == 0) {
142             getLog().debug("No components found");
143         }
144         else {
145             getLog().info("Discovered " + descriptors.size() + " component descriptors(s)");
146 
147             ComponentSetDescriptor set = new ComponentSetDescriptor();
148             set.setComponents(descriptors);
149             set.setDependencies(Collections.EMPTY_LIST);
150 
151             try {
152                 writeDescriptor(set, outputFile);
153             }
154             catch (Exception e) {
155                 throw new MojoExecutionException("Failed to write output file", e);
156             }
157         }
158     }
159 
160     private void writeDescriptor(final ComponentSetDescriptor desc, final File outputFile) throws Exception {
161         assert desc != null;
162         assert outputFile != null;
163 
164         FileUtils.forceMkdir(outputFile.getParentFile());
165 
166         BufferedWriter output = new BufferedWriter(new FileWriter(outputFile));
167 
168         try {
169             writer.writeDescriptorSet(output, desc, containerDescriptor);
170             output.flush();
171         }
172         finally {
173             IOUtil.close(output);
174         }
175 
176         getLog().debug("Wrote: " + outputFile);
177     }
178 }