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