View Javadoc
1   package org.apache.maven.plugin.coreit;
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 org.apache.maven.plugin.AbstractMojo;
23  import org.apache.maven.plugin.MojoExecutionException;
24  
25  import java.io.File;
26  import java.io.FileWriter;
27  import java.io.IOException;
28  import java.io.Writer;
29  
30  /**
31   * "Catch" a parameter "thrown" by the ThrowMojo through the plugin context, and
32   * write a file based on it's value to the build output directory.
33   *
34   * @goal catch
35   */
36  public class CatchMojo
37      extends AbstractMojo
38  {
39  
40      /**
41       * @parameter default-value="${project.build.directory}"
42       * @required
43       * @readonly
44       */
45      private File outDir;
46  
47      public File getOutDir()
48      {
49          return outDir;
50      }
51  
52      public void setOutDir( File outDir )
53      {
54          this.outDir = outDir;
55      }
56  
57      public void execute()
58          throws MojoExecutionException
59      {
60          String value = (String) getPluginContext().get( ThrowMojo.THROWN_PARAMETER );
61  
62          if ( !outDir.exists() )
63          {
64              outDir.mkdirs();
65          }
66  
67          File outfile = new File( outDir, value );
68  
69          Writer writer = null;
70          try
71          {
72              writer = new FileWriter( outfile );
73  
74              writer.write( value );
75  
76              writer.flush();
77          }
78          catch ( IOException e )
79          {
80              throw new MojoExecutionException( "Cannot write output file: " + outfile, e );
81          }
82          finally
83          {
84              if ( writer != null )
85              {
86                  try
87                  {
88                      writer.close();
89                  }
90                  catch ( IOException e )
91                  {
92                      // ignore
93                  }
94              }
95          }
96      }
97  
98  }