View Javadoc
1   package org.codehaus.modello.maven;
2   
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  
22  import java.io.File;
23  import java.util.Collections;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.Properties;
27  import java.util.stream.Collectors;
28  
29  import org.apache.maven.plugins.annotations.LifecyclePhase;
30  import org.apache.maven.plugins.annotations.Mojo;
31  import org.apache.maven.plugins.annotations.Parameter;
32  import org.codehaus.modello.plugin.velocity.VelocityGenerator;
33  
34  /**
35   * Creates files from the model using Velocity templates.
36   * <p>
37   * This mojo can be given a list of templates and a list of parameters.
38   * Each template from the {@link #templates} property will be run with the following context:
39   * <ul>
40   *    <li>{@code version}: the version of the model to generate</li>
41   *    <li>{@code model}: the modello model</li>
42   *    <li>{@code Helper}: a {@link org.codehaus.modello.plugin.velocity.Helper} object instance</li>
43   *    <li>any additional parameters specified using the {@link #params} property</li>
44   * </ul>
45   * The output file is controlled from within the template using the {@code #MODELLO-VELOCITY#SAVE-OUTPUT-TO}
46   * <a href="https://velocity.apache.org/engine/2.3/vtl-reference.html#directives">VTL directive</a>.
47   * This allows a single template to generate multiple files. For example, the following
48   * directive will redirect further output from the template to a file named
49   * {@code org/apache/maven/api/model/Plugin.java} if the variable {@code package} is set to
50   * {@code org.apache.maven.api.model} and the variable {@code className} is set to {@code Plugin}.
51   * <p>
52   *     {@code #MODELLO-VELOCITY#SAVE-OUTPUT-TO ${package.replace('.','/')}/${className}.java}
53   */
54  @Mojo(name = "velocity", defaultPhase = LifecyclePhase.GENERATE_SOURCES, threadSafe = true)
55  public class ModelloVelocityMojo extends AbstractModelloGeneratorMojo {
56      /**
57       * The output directory of the generated files.
58       */
59      @Parameter(defaultValue = "${project.build.directory}/generated-sources/modello")
60      private File outputDirectory;
61  
62      /**
63       * The directory where Velocity templates are looked for.
64       */
65      @Parameter(defaultValue = "${project.basedir}")
66      private File velocityBasedir;
67  
68      /**
69       * A list of template paths to be run against the loaded Modello model.
70       * Those are {@code .vm} files as described in the
71       * <a href="https://velocity.apache.org/engine/devel/user-guide.html">Velocity Users Guide</a>
72       * relative to {@code velocityBasedir}.
73       */
74      @Parameter
75      private List<String> templates;
76  
77      /**
78       * A list of parameters, using the syntax {@code key=value}.
79       * Those parameters will be made accessible to the templates.
80       */
81      @Parameter
82      private List<String> params;
83  
84      protected String getGeneratorType() {
85          return "velocity";
86      }
87  
88      protected void customizeParameters(Properties parameters) {
89          super.customizeParameters(parameters);
90  
91          Map<String, String> params = this.params == null
92                  ? Collections.emptyMap()
93                  : this.params.stream()
94                          .collect(Collectors.toMap(
95                                  s -> s.substring(0, s.indexOf('=')), s -> s.substring(s.indexOf('=') + 1)));
96  
97          parameters.put(VelocityGenerator.VELOCITY_BASEDIR, velocityBasedir.getAbsolutePath());
98  
99          parameters.put(VelocityGenerator.VELOCITY_TEMPLATES, templates.stream().collect(Collectors.joining(",")));
100         parameters.put(VelocityGenerator.VELOCITY_PARAMETERS, params);
101     }
102 
103     protected boolean producesCompilableResult() {
104         return true;
105     }
106 
107     @Override
108     public File getOutputDirectory() {
109         return outputDirectory;
110     }
111 }