This project has retired. For details please refer to its Attic page.
CreateDescriptorMojo xref
View Javadoc

1   /*
2    * $Id: CreateDescriptorMojo.java 1656976 2015-02-04 02:27:40Z nlebas $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  package org.apache.tiles.autotag.plugin;
22  
23  /*
24   * Copyright 2001-2005 The Apache Software Foundation.
25   *
26   * Licensed under the Apache License, Version 2.0 (the "License");
27   * you may not use this file except in compliance with the License.
28   * You may obtain a copy of the License at
29   *
30   *      http://www.apache.org/licenses/LICENSE-2.0
31   *
32   * Unless required by applicable law or agreed to in writing, software
33   * distributed under the License is distributed on an "AS IS" BASIS,
34   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
35   * See the License for the specific language governing permissions and
36   * limitations under the License.
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   * Creates a descriptor for the template model in XML format.
65   */
66  @Mojo(name = "create-descriptor", defaultPhase = LifecyclePhase.GENERATE_RESOURCES)
67  public class CreateDescriptorMojo extends AbstractMojo {
68      /**
69       * Location of the file.
70       */
71  	@Parameter(defaultValue = "${project.build.directory}/autotag-template-suite", required = true)
72      File outputDirectory;
73  
74      /**
75       * Location of the file.
76       */
77  	@Parameter(property = "project.build.sourceDirectory", required = true)
78      File sourceDirectory;
79  
80      /**
81       * included files.
82       */
83  	@Parameter
84      Set<String> includes;
85  
86      /**
87       * The name of the template.
88       */
89  	@Parameter(required = true)
90      String name;
91  
92      /**
93       * The documentation of the suite.
94       */
95  	@Parameter
96      String documentation;
97  
98      /**
99       * Excluded files.
100      */
101 	@Parameter
102     Set<String> excludes;
103 
104     /**
105      * Name of the request class.
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     /** {@inheritDoc} */
117     public void execute() throws MojoExecutionException {
118         try {
119             String[] fileNames = getSourceInclusionScanner().getIncludedFiles();
120             File dir = new File(outputDirectory, "META-INF");
121             if(!dir.exists()) {
122             	dir.mkdirs();
123             	buildContext.refresh(dir);
124             }
125             File outputFile = new File(dir, "template-suite.xml");
126             boolean uptodate = outputFile.exists();
127             File[] files = new File[fileNames.length];
128             for(int i=0; i<fileNames.length; i++) {
129             	files[i] = new File(sourceDirectory, fileNames[i]);
130             	uptodate &= buildContext.isUptodate(outputFile, files[i]);
131             }
132             if(!uptodate) {
133                 createDescriptor(outputFile, files);
134 			}
135             addResourceDirectory(outputDirectory.getAbsolutePath());
136         } catch (IOException e) {
137             throw new MojoExecutionException("error", e);
138         }
139     }
140 
141 	private void createDescriptor(File outputFile, File[] files)
142 			throws IOException {
143 		QDoxTemplateSuiteFactory factory = new QDoxTemplateSuiteFactory(files);
144 		factory.setSuiteName(name);
145 		factory.setSuiteDocumentation(documentation);
146 		factory.setRequestClass(requestClass);
147 		TemplateSuite suite = factory.createTemplateSuite();
148 		XStream xstream = new XStream();
149 		OutputStream os = buildContext.newFileOutputStream(outputFile);
150 		Writer writer = new OutputStreamWriter(os);
151 		xstream.toXML(suite, writer);
152 		writer.close();
153 		os.close();
154 	}
155 
156 	private void addResourceDirectory(String directory) {
157 		boolean addResource = true;
158 		@SuppressWarnings("unchecked")
159 		List<Resource> resources = project.getResources();
160 		for(Resource resource: resources) {
161 			if(directory.equals(resource.getDirectory())) {
162 				addResource = false;
163 			}
164 		}
165 		if(addResource) {
166 		    Resource resource = new Resource();
167 		    resource.setDirectory(directory);
168 		    project.addResource(resource);
169 		}
170 	}
171 
172     /**
173      * Creates a source inclusion scanner.
174      *
175      * @return The inclusion scanner.
176      */
177     private Scanner getSourceInclusionScanner() {
178     	Scanner scanner = buildContext.newScanner( sourceDirectory );
179         if (includes == null) {
180             includes = new HashSet<String>();
181         }
182         if (excludes == null) {
183             excludes = new HashSet<String>();
184         }
185 
186         if (includes.isEmpty()) {
187             scanner.setIncludes(new String[] {"**/*Model.java"});
188         }
189         else {
190         	scanner.setIncludes(includes.toArray(new String[includes.size()]));
191         }
192         if (!excludes.isEmpty()) {
193         	scanner.setExcludes(excludes.toArray(new String[excludes.size()]));
194         }
195         scanner.scan();
196         return scanner;
197     }
198 }