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