1 package org.apache.maven.plugin.dependency.utils;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.BufferedReader;
23 import java.io.File;
24 import java.io.FileWriter;
25 import java.io.IOException;
26 import java.io.StringReader;
27
28 import org.apache.maven.artifact.Artifact;
29 import org.apache.maven.plugin.logging.Log;
30 import org.codehaus.plexus.util.StringUtils;
31
32
33
34
35
36
37
38 public final class DependencyUtil
39 {
40
41
42
43
44
45
46
47
48
49
50
51
52
53 public static String getFormattedFileName( Artifact artifact, boolean removeVersion )
54 {
55 String destFileName = null;
56
57
58
59 if ( artifact.getFile() != null && !removeVersion )
60 {
61 destFileName = artifact.getFile().getName();
62 }
63 else
64
65 {
66 String versionString = null;
67 if ( !removeVersion )
68 {
69 versionString = "-" + artifact.getVersion();
70 }
71 else
72 {
73 versionString = "";
74 }
75
76 String classifierString = "";
77
78 if ( StringUtils.isNotEmpty( artifact.getClassifier() ) )
79 {
80 classifierString = "-" + artifact.getClassifier();
81 }
82
83 destFileName = artifact.getArtifactId() + versionString + classifierString + "."
84 + artifact.getArtifactHandler().getExtension();
85 }
86 return destFileName;
87 }
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108 public static File getFormattedOutputDirectory( boolean useSubdirsPerType, boolean useSubdirPerArtifact,
109 boolean useRepositoryLayout, boolean removeVersion,
110 File outputDirectory, Artifact artifact )
111 {
112 StringBuffer sb = new StringBuffer( 128 );
113 if ( useRepositoryLayout )
114 {
115
116 sb.append( artifact.getGroupId().replace( '.', File.separatorChar ) ).append( File.separatorChar );
117
118 sb.append( artifact.getArtifactId() ).append( File.separatorChar );
119
120 sb.append( artifact.getBaseVersion() ).append( File.separatorChar );
121 }
122 else
123 {
124 if ( useSubdirsPerType )
125 {
126 sb.append( artifact.getType() ).append( "s" ).append( File.separatorChar );
127 }
128 if ( useSubdirPerArtifact )
129 {
130 String artifactString = getDependencyId( artifact, removeVersion );
131 sb.append( artifactString ).append( File.separatorChar );
132 }
133 }
134 return new File( outputDirectory, sb.toString() );
135 }
136
137 private static String getDependencyId( Artifact artifact, boolean removeVersion )
138 {
139 StringBuffer sb = new StringBuffer();
140
141 sb.append( artifact.getArtifactId() );
142
143 if ( StringUtils.isNotEmpty( artifact.getClassifier() ) )
144 {
145 sb.append( "-" );
146 sb.append( artifact.getClassifier() );
147 }
148
149 if ( !removeVersion )
150 {
151 sb.append( "-" );
152 sb.append( artifact.getVersion() );
153 sb.append( "-" );
154 sb.append( artifact.getType() );
155 }
156 else
157 {
158
159
160
161 if ( !StringUtils.equals( artifact.getClassifier(), artifact.getType() ) )
162 {
163 sb.append( "-" );
164 sb.append( artifact.getType() );
165 }
166 }
167 return sb.toString();
168 }
169
170
171
172
173
174
175
176
177
178
179
180 public synchronized static void write( String string, File file, Log log ) throws IOException
181 {
182 file.getParentFile().mkdirs();
183
184 FileWriter writer = null;
185
186 try
187 {
188 writer = new FileWriter( file );
189
190 writer.write( string );
191 }
192 finally
193 {
194 if ( writer != null )
195 {
196 try
197 {
198 writer.close();
199 }
200 catch ( IOException exception )
201 {
202 log.error( "Cannot close file", exception );
203 }
204 }
205 }
206 }
207
208
209
210
211
212
213
214
215
216 public synchronized static void log( String string, Log log ) throws IOException
217 {
218 BufferedReader reader = new BufferedReader( new StringReader( string ) );
219
220 String line;
221
222 while ( ( line = reader.readLine() ) != null )
223 {
224 log.info( line );
225 }
226
227 reader.close();
228 }
229 }