1 package org.apache.maven.plugin.coreit;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
37
38
39
40
41 public abstract class AbstractDependencyMojo
42 extends AbstractMojo
43 {
44
45
46
47
48
49
50
51
52 protected MavenProject project;
53
54
55
56
57
58
59
60
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
107 }
108 }
109 }
110 }
111
112
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 }