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.project.MavenProject;
25  
26  import java.io.File;
27  import java.io.FileOutputStream;
28  import java.io.OutputStreamWriter;
29  import java.io.IOException;
30  
31  /**
32   * @goal touch
33   *
34   * @phase process-sources
35   */
36  public class TouchMojo
37      extends AbstractMojo
38  {
39  
40      static final String FINAL_NAME = "coreitified";
41  
42      /**
43       * @parameter default-value="${project}"
44       */
45      private MavenProject project;
46  
47      /**
48       * @parameter default-value="${project.build.directory}"
49       * @required
50       */
51      private File outputDirectory;
52  
53      public void execute()
54          throws MojoExecutionException
55      {
56          getLog().info( "[MAVEN-CORE-IT-LOG] Project build directory " + project.getBuild().getDirectory() );
57  
58          touch( new File( project.getBuild().getDirectory() ), "touch.log" );
59  
60          getLog().info( "[MAVEN-CORE-IT-LOG] Using output directory " + outputDirectory );
61  
62          touch( outputDirectory, "touch.txt" );
63  
64          project.getBuild().setFinalName( FINAL_NAME );
65      }
66  
67      static void touch( File dir, String file )
68          throws MojoExecutionException
69      {
70          try
71          {
72               if ( !dir.exists() )
73               {
74                   dir.mkdirs();
75               }
76  
77               File touch = new File( dir, file );
78  
79               // NOTE: Using append mode to track execution count
80               OutputStreamWriter w = new OutputStreamWriter( new FileOutputStream( touch, true ), "UTF-8" );
81  
82               w.write( file );
83               w.write( "\n" );
84  
85               w.close();
86          }
87          catch ( IOException e )
88          {
89              throw new MojoExecutionException( "Error touching file", e );
90          }
91      }
92  
93  }