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  import org.apache.maven.plugin.MojoFailureException;
25  import org.apache.maven.project.MavenProject;
26  
27  import java.io.File;
28  import java.io.FileOutputStream;
29  import java.io.IOException;
30  import java.io.OutputStreamWriter;
31  
32  /**
33   * Creates a text file in the project base directory.
34   * 
35   * @goal resources
36   * @phase process-resources
37   * 
38   * @author Benjamin Bentmann
39   * @version $Id: ResourcesMojo.java 809571 2009-08-31 13:03:37Z bentmann $
40   */
41  public class ResourcesMojo
42      extends AbstractMojo
43  {
44  
45      /**
46       * The current Maven project.
47       * 
48       * @parameter default-value="${project}"
49       * @required
50       * @readonly
51       */
52      private MavenProject project;
53  
54      /**
55       * The path to the output file, relative to the project base directory directory.
56       * 
57       * @parameter
58       */
59      private String pathname = "target/resources-resources.txt";
60  
61      /**
62       * An optional message line to write to the output file (using UTF-8 encoding). If given, the output file will be
63       * opened in append mode.
64       * 
65       * @parameter
66       */
67      private String message;
68  
69      /**
70       * Runs this mojo.
71       * 
72       * @throws MojoExecutionException If the output file could not be created.
73       * @throws MojoFailureException If the output file has not been set.
74       */
75      public void execute()
76          throws MojoExecutionException, MojoFailureException
77      {
78          getLog().info( "[MAVEN-CORE-IT-LOG] Using output file path: " + pathname );
79  
80          if ( pathname == null || pathname.length() <= 0 )
81          {
82              throw new MojoFailureException( "Path name for output file has not been specified" );
83          }
84  
85          File outputFile = new File( pathname );
86          if ( !outputFile.isAbsolute() )
87          {
88              outputFile = new File( project.getBasedir(), pathname ).getAbsoluteFile();
89          }
90  
91          getLog().info( "[MAVEN-CORE-IT-LOG] Creating output file: " + outputFile );
92  
93          try
94          {
95              outputFile.getParentFile().mkdirs();
96  
97              if ( message != null && message.length() > 0 )
98              {
99                  getLog().info( "[MAVEN-CORE-IT-LOG]   " + message );
100 
101                 OutputStreamWriter writer = new OutputStreamWriter( new FileOutputStream( outputFile, true ), "UTF-8" );
102                 try
103                 {
104                     writer.write( message );
105                     writer.write( "\n" );
106                 }
107                 finally
108                 {
109                     writer.close();
110                 }
111             }
112             else
113             {
114                 outputFile.createNewFile();
115             }
116         }
117         catch ( IOException e )
118         {
119             throw new MojoExecutionException( "Output file could not be created: " + pathname, e );
120         }
121 
122         getLog().info( "[MAVEN-CORE-IT-LOG] Created output file: " + outputFile );
123     }
124 
125 }