1 package org.apache.maven.plugin.dependency.utils;
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 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.IOUtil;
31 import org.codehaus.plexus.util.StringUtils;
32
33 /**
34 * Utility class with static helper methods
35 *
36 * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
37 * @version $Id: DependencyUtil.java 1200108 2011-11-10 03:05:47Z carlos $
38 */
39 public final class DependencyUtil
40 {
41
42 /**
43 * Builds the file name. If removeVersion is set, then the file name must be reconstructed from the artifactId,
44 * Classifier (if used) and Type. Otherwise, this method returns the artifact file name.
45 *
46 * @param artifact File to be formatted.
47 * @param removeVersion Specifies if the version should be removed from the file name.
48 * @return Formatted file name in the format artifactId-[version]-[classifier].[type]
49 * @see {@link #getFormattedFileName(Artifact, boolean, boolean)}.
50 */
51 public static String getFormattedFileName( Artifact artifact, boolean removeVersion )
52 {
53 return getFormattedFileName( artifact, removeVersion, false );
54 }
55
56 /**
57 * Builds the file name. If removeVersion is set, then the file name must be
58 * reconstructed from the groupId (if <b>prependGroupId</b> is true) artifactId,
59 * Classifier (if used) and Type.
60 * Otherwise, this method returns the artifact file name.
61 *
62 * @param artifact
63 * File to be formatted.
64 * @param removeVersion
65 * Specifies if the version should be removed from the file name.
66 * @param prependGroupId
67 * Specifies if the groupId should be prepended to the file name.
68 * @return Formatted file name in the format
69 * [groupId].artifactId-[version]-[classifier].[type]
70 */
71 public static String getFormattedFileName( Artifact artifact, boolean removeVersion, boolean prependGroupId )
72 {
73 StringBuffer destFileName = new StringBuffer();
74
75 if ( prependGroupId )
76 {
77 destFileName.append( artifact.getGroupId() ).append( "." );
78 }
79
80 String versionString = null;
81 if ( !removeVersion )
82 {
83 versionString = "-" + artifact.getVersion();
84 }
85 else
86 {
87 versionString = "";
88 }
89
90 String classifierString = "";
91
92 if ( StringUtils.isNotEmpty( artifact.getClassifier() ) )
93 {
94 classifierString = "-" + artifact.getClassifier();
95 }
96 destFileName.append( artifact.getArtifactId() ).append( versionString );
97 destFileName.append( classifierString ).append( "." );
98 destFileName.append( artifact.getArtifactHandler().getExtension() );
99
100 return destFileName.toString();
101 }
102
103 /**
104 * Formats the outputDirectory based on type.
105 *
106 * @param useSubdirsPerType if a new sub directory should be used for each type.
107 * @param useSubdirPerArtifact if a new sub directory should be used for each artifact.
108 * @param useRepositoryLayout if dependencies must be moved into a Maven repository layout, if set, other settings
109 * will be ignored.
110 * @param removeVersion if the version must not be mentioned in the filename
111 * @param outputDirectory base outputDirectory.
112 * @param artifact information about the artifact.
113 * @return a formatted File object to use for output.
114 */
115 public static File getFormattedOutputDirectory( boolean useSubdirsPerScope, boolean useSubdirsPerType,
116 boolean useSubdirPerArtifact, boolean useRepositoryLayout,
117 boolean removeVersion, File outputDirectory, Artifact artifact )
118 {
119 StringBuffer sb = new StringBuffer( 128 );
120 if ( useRepositoryLayout )
121 {
122 // group id
123 sb.append( artifact.getGroupId().replace( '.', File.separatorChar ) ).append( File.separatorChar );
124 // artifact id
125 sb.append( artifact.getArtifactId() ).append( File.separatorChar );
126 // version
127 sb.append( artifact.getBaseVersion() ).append( File.separatorChar );
128 }
129 else
130 {
131 if ( useSubdirsPerScope )
132 {
133 sb.append( artifact.getScope() ).append( File.separatorChar );
134 }
135 if ( useSubdirsPerType )
136 {
137 sb.append( artifact.getType() ).append( "s" ).append( File.separatorChar );
138 }
139 if ( useSubdirPerArtifact )
140 {
141 String artifactString = getDependencyId( artifact, removeVersion );
142 sb.append( artifactString ).append( File.separatorChar );
143 }
144 }
145 return new File( outputDirectory, sb.toString() );
146 }
147
148 private static String getDependencyId( Artifact artifact, boolean removeVersion )
149 {
150 StringBuffer sb = new StringBuffer();
151
152 sb.append( artifact.getArtifactId() );
153
154 if ( StringUtils.isNotEmpty( artifact.getClassifier() ) )
155 {
156 sb.append( "-" );
157 sb.append( artifact.getClassifier() );
158 }
159
160 if ( !removeVersion )
161 {
162 sb.append( "-" );
163 sb.append( artifact.getVersion() );
164 sb.append( "-" );
165 sb.append( artifact.getType() );
166 }
167 else
168 {
169 // if the classifier and type are the same (sources), then don't
170 // repeat.
171 // avoids names like foo-sources-sources
172 if ( !StringUtils.equals( artifact.getClassifier(), artifact.getType() ) )
173 {
174 sb.append( "-" );
175 sb.append( artifact.getType() );
176 }
177 }
178 return sb.toString();
179 }
180
181 /**
182 * Writes the specified string to the specified file.
183 *
184 * @param string the string to write
185 * @param file the file to write to
186 * @throws IOException if an I/O error occurs
187 */
188 public static synchronized void write( String string, File file, boolean append, Log log )
189 throws IOException
190 {
191 file.getParentFile().mkdirs();
192
193 FileWriter writer = null;
194
195 try
196 {
197 writer = new FileWriter( file, append );
198
199 writer.write( string );
200 }
201 finally
202 {
203 IOUtil.close( writer );
204 }
205 }
206
207 /**
208 * Writes the specified string to the log at info level.
209 *
210 * @param string the string to write
211 * @throws IOException if an I/O error occurs
212 */
213 public static synchronized void log( String string, Log log )
214 throws IOException
215 {
216 BufferedReader reader = new BufferedReader( new StringReader( string ) );
217
218 String line;
219
220 while ( ( line = reader.readLine() ) != null )
221 {
222 log.info( line );
223 }
224
225 reader.close();
226 }
227
228 //
229 // mainly used to parse excludes,includes configuration
230 //
231 public static String[] tokenizer( String str )
232 {
233 return StringUtils.split( cleanToBeTokenizedString( str ), "," );
234 }
235
236 //
237 // clean up configuration string before it can be tokenized
238 //
239 public static String cleanToBeTokenizedString( String str )
240 {
241 String ret = "";
242 if ( !StringUtils.isEmpty( str ) )
243 {
244 // remove initial and ending spaces, plus all spaces next to commas
245 ret = str.trim().replaceAll( "[\\s]*,[\\s]*", "," );
246 }
247
248 return ret;
249 }
250 }