1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
package org.apache.tiles.autotag.plugin; |
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
import java.io.File; |
40 | |
import java.io.IOException; |
41 | |
import java.io.OutputStream; |
42 | |
import java.io.OutputStreamWriter; |
43 | |
import java.io.Writer; |
44 | |
import java.util.HashSet; |
45 | |
import java.util.List; |
46 | |
import java.util.Set; |
47 | |
|
48 | |
import org.apache.maven.model.Resource; |
49 | |
import org.apache.maven.plugin.AbstractMojo; |
50 | |
import org.apache.maven.plugin.MojoExecutionException; |
51 | |
import org.apache.maven.plugins.annotations.Component; |
52 | |
import org.apache.maven.plugins.annotations.LifecyclePhase; |
53 | |
import org.apache.maven.plugins.annotations.Mojo; |
54 | |
import org.apache.maven.plugins.annotations.Parameter; |
55 | |
import org.apache.maven.project.MavenProject; |
56 | |
import org.apache.tiles.autotag.core.QDoxTemplateSuiteFactory; |
57 | |
import org.apache.tiles.autotag.model.TemplateSuite; |
58 | |
import org.codehaus.plexus.util.Scanner; |
59 | |
import org.sonatype.plexus.build.incremental.BuildContext; |
60 | |
|
61 | |
import com.thoughtworks.xstream.XStream; |
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
@Mojo(name = "create-descriptor", defaultPhase = LifecyclePhase.GENERATE_RESOURCES) |
67 | 1 | public class CreateDescriptorMojo extends AbstractMojo { |
68 | |
|
69 | |
|
70 | |
|
71 | |
@Parameter(defaultValue = "${project.build.directory}/autotag-template-suite", required = true) |
72 | |
File outputDirectory; |
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
@Parameter(property = "project.build.sourceDirectory", required = true) |
78 | |
File sourceDirectory; |
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
@Parameter |
84 | |
Set<String> includes; |
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
@Parameter(required = true) |
90 | |
String name; |
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
@Parameter |
96 | |
String documentation; |
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
@Parameter |
102 | |
Set<String> excludes; |
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
@Parameter(defaultValue="org.apache.tiles.request.Request", required = true) |
108 | |
String requestClass; |
109 | |
|
110 | |
@Parameter(property = "project", required = true, readonly = true) |
111 | |
MavenProject project; |
112 | |
|
113 | |
@Component |
114 | |
BuildContext buildContext; |
115 | |
|
116 | |
|
117 | |
public void execute() throws MojoExecutionException { |
118 | |
try { |
119 | 1 | String[] fileNames = getSourceInclusionScanner().getIncludedFiles(); |
120 | 1 | File dir = new File(outputDirectory, "META-INF"); |
121 | 1 | if(!dir.exists()) { |
122 | 0 | dir.mkdirs(); |
123 | 0 | buildContext.refresh(dir); |
124 | |
} |
125 | 1 | File outputFile = new File(dir, "template-suite.xml"); |
126 | 1 | boolean uptodate = outputFile.exists(); |
127 | 1 | File[] files = new File[fileNames.length]; |
128 | 5 | for(int i=0; i<fileNames.length; i++) { |
129 | 4 | files[i] = new File(sourceDirectory, fileNames[i]); |
130 | 4 | uptodate &= buildContext.isUptodate(outputFile, files[i]); |
131 | |
} |
132 | 1 | if(!uptodate) { |
133 | 1 | createDescriptor(outputFile, files); |
134 | |
} |
135 | 1 | addResourceDirectory(outputDirectory.getAbsolutePath()); |
136 | 0 | } catch (IOException e) { |
137 | 0 | throw new MojoExecutionException("error", e); |
138 | 1 | } |
139 | 1 | } |
140 | |
|
141 | |
private void createDescriptor(File outputFile, File[] files) |
142 | |
throws IOException { |
143 | 1 | QDoxTemplateSuiteFactory factory = new QDoxTemplateSuiteFactory(files); |
144 | 1 | factory.setSuiteName(name); |
145 | 1 | factory.setSuiteDocumentation(documentation); |
146 | 1 | factory.setRequestClass(requestClass); |
147 | 1 | TemplateSuite suite = factory.createTemplateSuite(); |
148 | 1 | XStream xstream = new XStream(); |
149 | 1 | OutputStream os = buildContext.newFileOutputStream(outputFile); |
150 | 1 | Writer writer = new OutputStreamWriter(os); |
151 | 1 | xstream.toXML(suite, writer); |
152 | 1 | writer.close(); |
153 | 1 | os.close(); |
154 | 1 | } |
155 | |
|
156 | |
private void addResourceDirectory(String directory) { |
157 | 1 | boolean addResource = true; |
158 | |
@SuppressWarnings("unchecked") |
159 | 1 | List<Resource> resources = project.getResources(); |
160 | 1 | for(Resource resource: resources) { |
161 | 0 | if(directory.equals(resource.getDirectory())) { |
162 | 0 | addResource = false; |
163 | |
} |
164 | 0 | } |
165 | 1 | if(addResource) { |
166 | 1 | Resource resource = new Resource(); |
167 | 1 | resource.setDirectory(directory); |
168 | 1 | project.addResource(resource); |
169 | |
} |
170 | 1 | } |
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
private Scanner getSourceInclusionScanner() { |
178 | 1 | Scanner scanner = buildContext.newScanner( sourceDirectory ); |
179 | 1 | if (includes == null) { |
180 | 1 | includes = new HashSet<String>(); |
181 | |
} |
182 | 1 | if (excludes == null) { |
183 | 1 | excludes = new HashSet<String>(); |
184 | |
} |
185 | |
|
186 | 1 | if (includes.isEmpty()) { |
187 | 1 | scanner.setIncludes(new String[] {"**/*Model.java"}); |
188 | |
} |
189 | |
else { |
190 | 0 | scanner.setIncludes(includes.toArray(new String[includes.size()])); |
191 | |
} |
192 | 1 | if (!excludes.isEmpty()) { |
193 | 0 | scanner.setExcludes(excludes.toArray(new String[excludes.size()])); |
194 | |
} |
195 | 1 | scanner.scan(); |
196 | 1 | return scanner; |
197 | |
} |
198 | |
} |