Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
DependencyUtil |
|
| 3.4;3.4 |
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.StringUtils; | |
31 | ||
32 | /** | |
33 | * Utility class with static helper methods | |
34 | * | |
35 | * @author <a href="mailto:brianf@apache.org">Brian Fox</a> | |
36 | * @version $Id: DependencyUtil.java 728546 2008-12-21 22:56:51Z bentmann $ | |
37 | */ | |
38 | 0 | public final class DependencyUtil |
39 | { | |
40 | ||
41 | /** | |
42 | * Builds the file name. If removeVersion is set, then the file name must be | |
43 | * reconstructed from the artifactId, Classifier (if used) and Type. | |
44 | * Otherwise, this method returns the artifact file name. | |
45 | * | |
46 | * @param artifact | |
47 | * File to be formatted. | |
48 | * @param removeVersion | |
49 | * Specifies if the version should be removed from the file name. | |
50 | * @return Formatted file name in the format | |
51 | * artifactId-[version]-[classifier].[type] | |
52 | */ | |
53 | public static String getFormattedFileName( Artifact artifact, boolean removeVersion ) | |
54 | { | |
55 | 546 | String destFileName = null; |
56 | ||
57 | // if there is a file and we aren't stripping the version, just get the | |
58 | // name directly | |
59 | 546 | if ( artifact.getFile() != null && !removeVersion ) |
60 | { | |
61 | 423 | destFileName = artifact.getFile().getName(); |
62 | } | |
63 | else | |
64 | // if offline | |
65 | { | |
66 | 123 | String versionString = null; |
67 | 123 | if ( !removeVersion ) |
68 | { | |
69 | 47 | versionString = "-" + artifact.getVersion(); |
70 | } | |
71 | else | |
72 | { | |
73 | 76 | versionString = ""; |
74 | } | |
75 | ||
76 | 123 | String classifierString = ""; |
77 | ||
78 | 123 | if ( StringUtils.isNotEmpty( artifact.getClassifier() ) ) |
79 | { | |
80 | 38 | classifierString = "-" + artifact.getClassifier(); |
81 | } | |
82 | ||
83 | 123 | destFileName = artifact.getArtifactId() + versionString + classifierString + "." |
84 | + artifact.getArtifactHandler().getExtension(); | |
85 | } | |
86 | 546 | return destFileName; |
87 | } | |
88 | ||
89 | /** | |
90 | * Formats the outputDirectory based on type. | |
91 | * | |
92 | * @param useSubdirsPerType | |
93 | * if a new sub directory should be used for each type. | |
94 | * @param useSubdirPerArtifact | |
95 | * if a new sub directory should be used for each artifact. | |
96 | * @param useRepositoryLayout | |
97 | * if dependendies must be moved into a Maven repository layout, if | |
98 | * set, other settings will be ignored. | |
99 | * @param removeVersion | |
100 | * if the version must not be mentioned in the filename | |
101 | * @param outputDirectory | |
102 | * base outputDirectory. | |
103 | * @param artifact | |
104 | * information about the artifact. | |
105 | * | |
106 | * @return a formatted File object to use for output. | |
107 | */ | |
108 | public static File getFormattedOutputDirectory( boolean useSubdirsPerType, boolean useSubdirPerArtifact, | |
109 | boolean useRepositoryLayout, boolean removeVersion, | |
110 | File outputDirectory, Artifact artifact ) | |
111 | { | |
112 | 634 | StringBuffer sb = new StringBuffer( 128 ); |
113 | 634 | if ( useRepositoryLayout ) |
114 | { | |
115 | // group id | |
116 | 11 | sb.append( artifact.getGroupId().replace( '.', File.separatorChar ) ).append( File.separatorChar ); |
117 | // artifact id | |
118 | 11 | sb.append( artifact.getArtifactId() ).append( File.separatorChar ); |
119 | // version | |
120 | 11 | sb.append( artifact.getBaseVersion() ).append( File.separatorChar ); |
121 | } | |
122 | else | |
123 | { | |
124 | 623 | if ( useSubdirsPerType ) |
125 | { | |
126 | 79 | sb.append( artifact.getType() ).append( "s" ).append( File.separatorChar ); |
127 | } | |
128 | 623 | if ( useSubdirPerArtifact ) |
129 | { | |
130 | 130 | String artifactString = getDependencyId( artifact, removeVersion ); |
131 | 130 | sb.append( artifactString ).append( File.separatorChar ); |
132 | } | |
133 | } | |
134 | 634 | return new File( outputDirectory, sb.toString() ); |
135 | } | |
136 | ||
137 | private static String getDependencyId( Artifact artifact, boolean removeVersion ) | |
138 | { | |
139 | 130 | StringBuffer sb = new StringBuffer(); |
140 | ||
141 | 130 | sb.append( artifact.getArtifactId() ); |
142 | ||
143 | 130 | if ( StringUtils.isNotEmpty( artifact.getClassifier() ) ) |
144 | { | |
145 | 6 | sb.append( "-" ); |
146 | 6 | sb.append( artifact.getClassifier() ); |
147 | } | |
148 | ||
149 | 130 | if ( !removeVersion ) |
150 | { | |
151 | 67 | sb.append( "-" ); |
152 | 67 | sb.append( artifact.getVersion() ); |
153 | 67 | sb.append( "-" ); |
154 | 67 | sb.append( artifact.getType() ); |
155 | } | |
156 | else | |
157 | { | |
158 | // if the classifier and type are the same (sources), then don't | |
159 | // repeat. | |
160 | // avoids names like foo-sources-sources | |
161 | 63 | if ( !StringUtils.equals( artifact.getClassifier(), artifact.getType() ) ) |
162 | { | |
163 | 62 | sb.append( "-" ); |
164 | 62 | sb.append( artifact.getType() ); |
165 | } | |
166 | } | |
167 | 130 | return sb.toString(); |
168 | } | |
169 | ||
170 | /** | |
171 | * Writes the specified string to the specified file. | |
172 | * | |
173 | * @param string | |
174 | * the string to write | |
175 | * @param file | |
176 | * the file to write to | |
177 | * @throws IOException | |
178 | * if an I/O error occurs | |
179 | */ | |
180 | public synchronized static void write( String string, File file, Log log ) throws IOException | |
181 | { | |
182 | 0 | file.getParentFile().mkdirs(); |
183 | ||
184 | 0 | FileWriter writer = null; |
185 | ||
186 | try | |
187 | { | |
188 | 0 | writer = new FileWriter( file ); |
189 | ||
190 | 0 | writer.write( string ); |
191 | } | |
192 | finally | |
193 | { | |
194 | 0 | if ( writer != null ) |
195 | { | |
196 | try | |
197 | { | |
198 | 0 | writer.close(); |
199 | } | |
200 | 0 | catch ( IOException exception ) |
201 | { | |
202 | 0 | log.error( "Cannot close file", exception ); |
203 | 0 | } |
204 | } | |
205 | } | |
206 | 0 | } |
207 | ||
208 | /** | |
209 | * Writes the specified string to the log at info level. | |
210 | * | |
211 | * @param string | |
212 | * the string to write | |
213 | * @throws IOException | |
214 | * if an I/O error occurs | |
215 | */ | |
216 | public synchronized static void log( String string, Log log ) throws IOException | |
217 | { | |
218 | 3 | BufferedReader reader = new BufferedReader( new StringReader( string ) ); |
219 | ||
220 | String line; | |
221 | ||
222 | 21 | while ( ( line = reader.readLine() ) != null ) |
223 | { | |
224 | 18 | log.info( line ); |
225 | } | |
226 | ||
227 | 3 | reader.close(); |
228 | 3 | } |
229 | } |