1 package org.codehaus.plexus.maven.plugin;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
49
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
62
63
64 protected String fileName;
65
66
67
68
69
70
71
72 private boolean containerDescriptor;
73
74
75
76
77
78 private MavenProject mavenProject;
79
80
81
82
83 private ComponentDescriptor[] roleDefaults;
84
85
86
87
88 private ComponentDescriptorExtractor[] extractors;
89
90
91
92
93 private ComponentDescriptorWriter writer;
94
95
96
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
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 }