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.apache.creadur.whisker.plugin.maven;
20  
21  import java.io.File;
22  
23  import org.apache.creadur.whisker.app.Act;
24  import org.apache.creadur.whisker.app.Whisker;
25  import org.apache.creadur.whisker.app.load.StreamableResourceFactory;
26  import org.apache.creadur.whisker.app.out.WriteResultsIntoDirectoryFactory;
27  import org.apache.creadur.whisker.out.velocity.VelocityEngine;
28  import org.apache.maven.plugin.AbstractMojo;
29  import org.apache.maven.plugin.MojoExecutionException;
30  import org.apache.maven.plugins.annotations.*;
31  
32  /**
33   * Generates licensing related materials such as LICENSE and NOTICE documents
34   * for assembled applications. The plugin is not bound to a specific lifecycle phase.
35   */
36  @Mojo(name = "generate", requiresProject = false)
37  public class GenerateMojo extends AbstractMojo {
38  
39      /**
40       * Destination for generated materials
41       */
42  	@Parameter(defaultValue = "${project.build.directory}")
43      private File outputDirectory;
44  
45      /**
46       * The licensing materials will be encoding thus.
47       */
48  	@Parameter(property = "outputEncoding", defaultValue = "${project.build.sourceEncoding}")
49      private String outputEncoding;
50  
51      /**
52       * This file contains a description of the licensing qualities of
53       * the expected contents of the assembled application.
54       */
55  	@Parameter(property = "apacheWhiskerDescriptor", required = true)
56      private File descriptor;
57  
58      /**
59       * Generate licensing related materials such as LICENSE and NOTICE documents.
60       * @throws MojoExecutionException when Whisker fails,
61       * or when configured cannot be executed
62       * @see org.apache.maven.plugin.Mojo#execute()
63       */
64      public void execute() throws MojoExecutionException {
65          if (descriptor.exists()) {
66              if (descriptor.canRead()) {
67                   try {
68                      new Whisker().setLicenseDescriptor(new StreamableResourceFactory().streamFromFileResource(descriptor))
69                          .setEngine(new VelocityEngine(new MojoToJCLLog(getLog())))
70                          .setWriterFactory(new WriteResultsIntoDirectoryFactory(outputDirectory, outputEncoding))
71                          .setAct(Act.GENERATE).act();
72                  } catch (Exception e) {
73                      throw new MojoExecutionException("Whisker failed to generate materials: " + e.getMessage(), e);
74                  }
75              } else {
76                  throw new MojoExecutionException("Read permission requires on Whisker descriptor file: " + descriptor);
77              }
78          } else {
79              throw new MojoExecutionException("Whisker descriptor file is missing: " + descriptor);
80          }
81      }
82  
83  }