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
34
35
36
37
38
39
40 public abstract class AbstractDependencyMojo
41 extends AbstractMojo
42 {
43
44
45
46
47
48
49
50
51 protected MavenProject project;
52
53
54
55
56
57
58
59
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
112 }
113 }
114 }
115 }
116
117
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 }