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.artifact.Artifact;
23  import org.apache.maven.plugin.AbstractMojo;
24  import org.apache.maven.plugin.MojoExecutionException;
25  import org.apache.maven.project.MavenProject;
26  
27  import java.io.BufferedWriter;
28  import java.io.File;
29  import java.io.FileOutputStream;
30  import java.io.IOException;
31  import java.io.OutputStreamWriter;
32  import java.util.Collection;
33  import java.util.Iterator;
34  
35  /**
36   * Provides common services for all mojos of this plugin.
37   * 
38   * @author Benjamin Bentmann
39   * @version $Id: AbstractDependencyMojo.java 809429 2009-08-30 22:24:31Z bentmann $
40   */
41  public abstract class AbstractDependencyMojo
42      extends AbstractMojo
43  {
44  
45      /**
46       * The current Maven project.
47       * 
48       * @parameter default-value="${project}"
49       * @required
50       * @readonly
51       */
52      protected MavenProject project;
53  
54      /**
55       * Writes the specified artifacts to the given output file.
56       * 
57       * @param pathname The path to the output file, relative to the project base directory, may be <code>null</code> or
58       *            empty if the output file should not be written.
59       * @param artifacts The list of artifacts to write to the file, may be <code>null</code>.
60       * @throws MojoExecutionException If the output file could not be written.
61       */
62      protected void writeArtifacts( String pathname, Collection artifacts )
63          throws MojoExecutionException
64      {
65          if ( pathname == null || pathname.length() <= 0 )
66          {
67              return;
68          }
69  
70          File file = resolveFile( pathname );
71  
72          getLog().info( "[MAVEN-CORE-IT-LOG] Dumping artifact list: " + file );
73  
74          BufferedWriter writer = null;
75          try
76          {
77              file.getParentFile().mkdirs();
78  
79              writer = new BufferedWriter( new OutputStreamWriter( new FileOutputStream( file ), "UTF-8" ) );
80  
81              if ( artifacts != null )
82              {
83                  for ( Iterator it = artifacts.iterator(); it.hasNext(); )
84                  {
85                      Artifact artifact = (Artifact) it.next();
86                      writer.write( artifact.getId() );
87                      writer.newLine();
88                      getLog().info( "[MAVEN-CORE-IT-LOG]   " + artifact.getId() );
89                  }
90              }
91          }
92          catch ( IOException e )
93          {
94              throw new MojoExecutionException( "Failed to write artifact list", e );
95          }
96          finally
97          {
98              if ( writer != null )
99              {
100                 try
101                 {
102                     writer.close();
103                 }
104                 catch ( IOException e )
105                 {
106                     // just ignore
107                 }
108             }
109         }
110     }
111 
112     // NOTE: We don't want to test path translation here so resolve relative path manually for robustness
113     private File resolveFile( String pathname )
114     {
115         File file = null;
116 
117         if ( pathname != null )
118         {
119             file = new File( pathname );
120 
121             if ( !file.isAbsolute() )
122             {
123                 file = new File( project.getBasedir(), pathname );
124             }
125         }
126 
127         return file;
128     }
129 
130 }