View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.codehaus.modello.plugin.velocity;
20  
21  import java.io.File;
22  import java.util.Collections;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.Objects;
26  import java.util.Properties;
27  import java.util.stream.Collectors;
28  import org.apache.maven.plugins.annotations.LifecyclePhase;
29  import org.apache.maven.plugins.annotations.Mojo;
30  import org.apache.maven.plugins.annotations.Parameter;
31  import org.codehaus.modello.maven.AbstractModelloGeneratorMojo;
32  
33  /**
34   * Creates an XML schema from the model.
35   *
36   * @author <a href="mailto:brett@codehaus.org">Brett Porter</a>
37   */
38  @Mojo(name = "velocity", defaultPhase = LifecyclePhase.GENERATE_SOURCES, threadSafe = true)
39  public class ModelloVelocityMojo extends AbstractModelloGeneratorMojo {
40      /**
41       * The output directory of the generated XML Schema.
42       */
43      @Parameter(defaultValue = "${project.build.directory}/generated-sources/modello", required = true)
44      private File outputDirectory;
45  
46      @Parameter
47      private List<String> templates;
48  
49      @Parameter
50      private List<String> params;
51  
52      protected String getGeneratorType() {
53          return "velocity";
54      }
55  
56      protected void customizeParameters(Properties parameters) {
57          super.customizeParameters(parameters);
58          Map<String, String> params = this.params != null
59                  ? this.params.stream()
60                          .collect(Collectors.toMap(
61                                  s -> s.substring(0, s.indexOf('=')), s -> s.substring(s.indexOf('=') + 1)))
62                  : Collections.emptyMap();
63          parameters.put("basedir", Objects.requireNonNull(getBasedir(), "basedir is null"));
64          parameters.put(VelocityGenerator.VELOCITY_TEMPLATES, String.join(",", templates));
65          parameters.put(VelocityGenerator.VELOCITY_PARAMETERS, params);
66      }
67  
68      protected boolean producesCompilableResult() {
69          return true;
70      }
71  
72      public File getOutputDirectory() {
73          return outputDirectory;
74      }
75  
76      public void setOutputDirectory(File outputDirectory) {
77          this.outputDirectory = outputDirectory;
78      }
79  }