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.FileInputStream;
27  import java.io.FileOutputStream;
28  import java.io.IOException;
29  
30  /**
31   * Mojo which makes a copy of the POM using MavenProject.getFile() to locate the file.
32   *
33   * @goal copy-pom
34   * @phase generate-sources
35   */
36  public class CopyPomMojo
37      extends AbstractMojo
38  {
39      /**
40       * @parameter default-value="${project.file}"
41       */
42      private File pomFile;
43  
44      /**
45       * @parameter default-value="${project.build.directory}/pom-copy.xml"
46       * @required
47       */
48      private String outputFile;
49  
50      public void execute()
51          throws MojoExecutionException
52      {
53          try
54          {
55              File dest = new File( outputFile );
56              File dir = dest.getParentFile();
57  
58              if ( !dir.exists() )
59              {
60                  dir.mkdirs();
61              }
62  
63              getLog().info( "Copying POM to file: " + dest.getAbsolutePath() );
64  
65              FileInputStream in = new FileInputStream( pomFile );
66              FileOutputStream out = new FileOutputStream( dest );
67  
68              int read = -1;
69              byte[] buf = new byte[4 * 1024];
70              while ( ( read = in.read( buf ) ) > -1 )
71              {
72                  out.write( buf, 0, read );
73              }
74  
75              in.close();
76              out.close();
77          }
78          catch ( IOException e )
79          {
80              throw new MojoExecutionException( "Error copying POM", e );
81          }
82      }
83  }