View Javadoc
1   package org.codehaus.modello.plugin.velocity;
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.Objects;
27  import java.util.Properties;
28  import java.util.stream.Collectors;
29  
30  import org.apache.maven.plugins.annotations.LifecyclePhase;
31  import org.apache.maven.plugins.annotations.Mojo;
32  import org.apache.maven.plugins.annotations.Parameter;
33  import org.codehaus.modello.maven.AbstractModelloGeneratorMojo;
34  
35  /**
36   * Creates an XML schema from the model.
37   *
38   * @author <a href="mailto:brett@codehaus.org">Brett Porter</a>
39   */
40  @Mojo( name = "velocity", defaultPhase = LifecyclePhase.GENERATE_SOURCES, threadSafe = true )
41  public class ModelloVelocityMojo
42          extends AbstractModelloGeneratorMojo
43  {
44      /**
45       * The output directory of the generated XML Schema.
46       */
47      @Parameter( defaultValue = "${project.build.directory}/generated-sources/modello", required = true )
48      private File outputDirectory;
49  
50      @Parameter
51      private List<String> templates;
52  
53      @Parameter
54      private List<String> params;
55  
56      protected String getGeneratorType()
57      {
58          return "velocity";
59      }
60  
61      protected void customizeParameters( Properties parameters )
62      {
63          super.customizeParameters( parameters );
64          Map<String, String> params = this.params != null ? this.params.stream().collect( Collectors.toMap(
65                  s -> s.substring( 0, s.indexOf( '=' ) ), s -> s.substring( s.indexOf( '=' ) + 1 )
66          ) ) : Collections.emptyMap();
67          parameters.put( "basedir", Objects.requireNonNull( getBasedir(), "basedir is null" ) );
68          parameters.put( VelocityGenerator.VELOCITY_TEMPLATES, String.join( ",", templates ) );
69          parameters.put( VelocityGenerator.VELOCITY_PARAMETERS, params );
70      }
71  
72      protected boolean producesCompilableResult()
73      {
74          return true;
75      }
76  
77      public File getOutputDirectory()
78      {
79          return outputDirectory;
80      }
81  
82      public void setOutputDirectory( File outputDirectory )
83      {
84          this.outputDirectory = outputDirectory;
85      }
86  }