Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
DependencyUtil |
|
| 2.4444444444444446;2.444 |
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 1399024 2012-10-16 22:23:33Z hboutemy $ | |
38 | */ | |
39 | 0 | 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 | 433 | 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 | 442 | return getFormattedFileName( artifact, removeVersion, prependGroupId, false ); |
74 | } | |
75 | ||
76 | /** | |
77 | * Builds the file name. If removeVersion is set, then the file name must be | |
78 | * reconstructed from the groupId (if <b>prependGroupId</b> is true) artifactId, | |
79 | * Classifier (if used) and Type. | |
80 | * Otherwise, this method returns the artifact file name. | |
81 | * | |
82 | * @param artifact | |
83 | * File to be formatted. | |
84 | * @param removeVersion | |
85 | * Specifies if the version should be removed from the file name. | |
86 | * @param prependGroupId | |
87 | * Specifies if the groupId should be prepended to the file name. | |
88 | * @param useBaseVersion | |
89 | * Specifies if the baseVersion of the artifact should be used instead of the version. | |
90 | * @return Formatted file name in the format | |
91 | * [groupId].artifactId-[version]-[classifier].[type] | |
92 | */ | |
93 | public static String getFormattedFileName( Artifact artifact, boolean removeVersion, boolean prependGroupId, | |
94 | boolean useBaseVersion ) | |
95 | { | |
96 | 612 | StringBuilder destFileName = new StringBuilder(); |
97 | ||
98 | 612 | if ( prependGroupId ) |
99 | { | |
100 | 16 | destFileName.append( artifact.getGroupId() ).append( "." ); |
101 | } | |
102 | ||
103 | 612 | String versionString = null; |
104 | 612 | if ( !removeVersion ) |
105 | { | |
106 | 536 | if ( useBaseVersion ) |
107 | { | |
108 | 149 | versionString = "-" + artifact.getBaseVersion(); |
109 | } | |
110 | else | |
111 | { | |
112 | 387 | versionString = "-" + artifact.getVersion(); |
113 | } | |
114 | } | |
115 | else | |
116 | { | |
117 | 76 | versionString = ""; |
118 | } | |
119 | ||
120 | 612 | String classifierString = ""; |
121 | ||
122 | 612 | if ( StringUtils.isNotEmpty( artifact.getClassifier() ) ) |
123 | { | |
124 | 150 | classifierString = "-" + artifact.getClassifier(); |
125 | } | |
126 | 612 | destFileName.append( artifact.getArtifactId() ).append( versionString ); |
127 | 612 | destFileName.append( classifierString ).append( "." ); |
128 | 612 | destFileName.append( artifact.getArtifactHandler().getExtension() ); |
129 | ||
130 | 612 | return destFileName.toString(); |
131 | } | |
132 | ||
133 | /** | |
134 | * Formats the outputDirectory based on type. | |
135 | * | |
136 | * @param useSubdirsPerType if a new sub directory should be used for each type. | |
137 | * @param useSubdirPerArtifact if a new sub directory should be used for each artifact. | |
138 | * @param useRepositoryLayout if dependencies must be moved into a Maven repository layout, if set, other settings | |
139 | * will be ignored. | |
140 | * @param removeVersion if the version must not be mentioned in the filename | |
141 | * @param outputDirectory base outputDirectory. | |
142 | * @param artifact information about the artifact. | |
143 | * @return a formatted File object to use for output. | |
144 | */ | |
145 | public static File getFormattedOutputDirectory( boolean useSubdirsPerScope, boolean useSubdirsPerType, | |
146 | boolean useSubdirPerArtifact, boolean useRepositoryLayout, | |
147 | boolean removeVersion, File outputDirectory, Artifact artifact ) | |
148 | { | |
149 | 679 | StringBuilder sb = new StringBuilder( 128 ); |
150 | 679 | if ( useRepositoryLayout ) |
151 | { | |
152 | // group id | |
153 | 11 | sb.append( artifact.getGroupId().replace( '.', File.separatorChar ) ).append( File.separatorChar ); |
154 | // artifact id | |
155 | 11 | sb.append( artifact.getArtifactId() ).append( File.separatorChar ); |
156 | // version | |
157 | 11 | sb.append( artifact.getBaseVersion() ).append( File.separatorChar ); |
158 | } | |
159 | else | |
160 | { | |
161 | 668 | if ( useSubdirsPerScope ) |
162 | { | |
163 | 17 | sb.append( artifact.getScope() ).append( File.separatorChar ); |
164 | } | |
165 | 668 | if ( useSubdirsPerType ) |
166 | { | |
167 | 79 | sb.append( artifact.getType() ).append( "s" ).append( File.separatorChar ); |
168 | } | |
169 | 668 | if ( useSubdirPerArtifact ) |
170 | { | |
171 | 146 | String artifactString = getDependencyId( artifact, removeVersion ); |
172 | 146 | sb.append( artifactString ).append( File.separatorChar ); |
173 | } | |
174 | } | |
175 | 679 | return new File( outputDirectory, sb.toString() ); |
176 | } | |
177 | ||
178 | private static String getDependencyId( Artifact artifact, boolean removeVersion ) | |
179 | { | |
180 | 146 | StringBuilder sb = new StringBuilder(); |
181 | ||
182 | 146 | sb.append( artifact.getArtifactId() ); |
183 | ||
184 | 146 | if ( StringUtils.isNotEmpty( artifact.getClassifier() ) ) |
185 | { | |
186 | 7 | sb.append( "-" ); |
187 | 7 | sb.append( artifact.getClassifier() ); |
188 | } | |
189 | ||
190 | 146 | if ( !removeVersion ) |
191 | { | |
192 | 82 | sb.append( "-" ); |
193 | 82 | sb.append( artifact.getVersion() ); |
194 | 82 | sb.append( "-" ); |
195 | 82 | sb.append( artifact.getType() ); |
196 | } | |
197 | else | |
198 | { | |
199 | // if the classifier and type are the same (sources), then don't | |
200 | // repeat. | |
201 | // avoids names like foo-sources-sources | |
202 | 64 | if ( !StringUtils.equals( artifact.getClassifier(), artifact.getType() ) ) |
203 | { | |
204 | 63 | sb.append( "-" ); |
205 | 63 | sb.append( artifact.getType() ); |
206 | } | |
207 | } | |
208 | 146 | return sb.toString(); |
209 | } | |
210 | ||
211 | /** | |
212 | * Writes the specified string to the specified file. | |
213 | * | |
214 | * @param string the string to write | |
215 | * @param file the file to write to | |
216 | * @throws IOException if an I/O error occurs | |
217 | */ | |
218 | public static synchronized void write( String string, File file, boolean append, Log log ) | |
219 | throws IOException | |
220 | { | |
221 | 0 | file.getParentFile().mkdirs(); |
222 | ||
223 | 0 | FileWriter writer = null; |
224 | ||
225 | try | |
226 | { | |
227 | 0 | writer = new FileWriter( file, append ); |
228 | ||
229 | 0 | writer.write( string ); |
230 | } | |
231 | finally | |
232 | { | |
233 | 0 | IOUtil.close( writer ); |
234 | 0 | } |
235 | 0 | } |
236 | ||
237 | /** | |
238 | * Writes the specified string to the log at info level. | |
239 | * | |
240 | * @param string the string to write | |
241 | * @throws IOException if an I/O error occurs | |
242 | */ | |
243 | public static synchronized void log( String string, Log log ) | |
244 | throws IOException | |
245 | { | |
246 | 2 | BufferedReader reader = new BufferedReader( new StringReader( string ) ); |
247 | ||
248 | String line; | |
249 | ||
250 | 17 | while ( ( line = reader.readLine() ) != null ) |
251 | { | |
252 | 15 | log.info( line ); |
253 | } | |
254 | ||
255 | 2 | reader.close(); |
256 | 2 | } |
257 | ||
258 | // | |
259 | // mainly used to parse excludes,includes configuration | |
260 | // | |
261 | public static String[] tokenizer( String str ) | |
262 | { | |
263 | 4 | return StringUtils.split( cleanToBeTokenizedString( str ), "," ); |
264 | } | |
265 | ||
266 | // | |
267 | // clean up configuration string before it can be tokenized | |
268 | // | |
269 | public static String cleanToBeTokenizedString( String str ) | |
270 | { | |
271 | 1891 | String ret = ""; |
272 | 1891 | if ( !StringUtils.isEmpty( str ) ) |
273 | { | |
274 | // remove initial and ending spaces, plus all spaces next to commas | |
275 | 262 | ret = str.trim().replaceAll( "[\\s]*,[\\s]*", "," ); |
276 | } | |
277 | ||
278 | 1891 | return ret; |
279 | } | |
280 | } |