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.IOException;
29  
30  /**
31   * Creates a text file in the project base directory.
32   * 
33   * @goal install
34   * @phase install
35   * 
36   * @author Benjamin Bentmann
37   * @version $Id$
38   */
39  public class InstallMojo
40      extends AbstractMojo
41  {
42  
43      /**
44       * The current Maven project.
45       * 
46       * @parameter default-value="${project}"
47       * @required
48       * @readonly
49       */
50      private MavenProject project;
51  
52      /**
53       * The path to the output file, relative to the project base directory directory.
54       * 
55       * @parameter
56       */
57      private String pathname = "target/install-install.txt";
58  
59      /**
60       * Runs this mojo.
61       * 
62       * @throws MojoExecutionException If the output file could not be created.
63       * @throws MojoFailureException If the output file has not been set.
64       */
65      public void execute()
66          throws MojoExecutionException, MojoFailureException
67      {
68          getLog().info( "[MAVEN-CORE-IT-LOG] Using output file path: " + pathname );
69  
70          if ( pathname == null || pathname.length() <= 0 )
71          {
72              throw new MojoFailureException( "Path name for output file has not been specified" );
73          }
74  
75          File outputFile = new File( pathname );
76          if ( !outputFile.isAbsolute() )
77          {
78              outputFile = new File( project.getBasedir(), pathname ).getAbsoluteFile();
79          }
80  
81          getLog().info( "[MAVEN-CORE-IT-LOG] Creating output file: " + outputFile );
82  
83          try
84          {
85               outputFile.getParentFile().mkdirs();
86               outputFile.createNewFile();
87          }
88          catch ( IOException e )
89          {
90              throw new MojoExecutionException( "Output file could not be created: " + pathname, e );
91          }
92  
93          getLog().info( "[MAVEN-CORE-IT-LOG] Created output file: " + outputFile );
94      }
95  
96  }