Coverage Report - org.apache.maven.plugin.dependency.utils.DependencyUtil
 
Classes in this File Line Coverage Branch Coverage Complexity
DependencyUtil
86 %
52/60
100 %
24/24
2,5
 
 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  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  426
         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  589
         StringBuffer destFileName = new StringBuffer();
 74  
         
 75  589
         if ( prependGroupId )
 76  
         {
 77  16
             destFileName.append( artifact.getGroupId() ).append( "." );
 78  
         }
 79  
         
 80  589
         String versionString = null;
 81  589
         if ( !removeVersion )
 82  
         {
 83  513
             versionString = "-" + artifact.getVersion();
 84  
         }
 85  
         else
 86  
         {
 87  76
             versionString = "";
 88  
         }
 89  
 
 90  589
         String classifierString = "";
 91  
 
 92  589
         if ( StringUtils.isNotEmpty( artifact.getClassifier() ) )
 93  
         {
 94  150
             classifierString = "-" + artifact.getClassifier();
 95  
         }
 96  589
         destFileName.append( artifact.getArtifactId() ).append( versionString );
 97  589
         destFileName.append( classifierString ).append( "." );
 98  589
         destFileName.append( artifact.getArtifactHandler().getExtension() );
 99  
         
 100  589
         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  665
         StringBuffer sb = new StringBuffer( 128 );
 120  665
         if ( useRepositoryLayout )
 121  
         {
 122  
             // group id
 123  11
             sb.append( artifact.getGroupId().replace( '.', File.separatorChar ) ).append( File.separatorChar );
 124  
             // artifact id
 125  11
             sb.append( artifact.getArtifactId() ).append( File.separatorChar );
 126  
             // version
 127  11
             sb.append( artifact.getBaseVersion() ).append( File.separatorChar );
 128  
         }
 129  
         else
 130  
         {
 131  654
             if ( useSubdirsPerScope )
 132  
             {
 133  17
                 sb.append( artifact.getScope() ).append( File.separatorChar );
 134  
             }
 135  654
             if ( useSubdirsPerType )
 136  
             {
 137  79
                 sb.append( artifact.getType() ).append( "s" ).append( File.separatorChar );
 138  
             }
 139  654
             if ( useSubdirPerArtifact )
 140  
             {
 141  146
                 String artifactString = getDependencyId( artifact, removeVersion );
 142  146
                 sb.append( artifactString ).append( File.separatorChar );
 143  
             }
 144  
         }
 145  665
         return new File( outputDirectory, sb.toString() );
 146  
     }
 147  
 
 148  
     private static String getDependencyId( Artifact artifact, boolean removeVersion )
 149  
     {
 150  146
         StringBuffer sb = new StringBuffer();
 151  
 
 152  146
         sb.append( artifact.getArtifactId() );
 153  
 
 154  146
         if ( StringUtils.isNotEmpty( artifact.getClassifier() ) )
 155  
         {
 156  7
             sb.append( "-" );
 157  7
             sb.append( artifact.getClassifier() );
 158  
         }
 159  
 
 160  146
         if ( !removeVersion )
 161  
         {
 162  82
             sb.append( "-" );
 163  82
             sb.append( artifact.getVersion() );
 164  82
             sb.append( "-" );
 165  82
             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  64
             if ( !StringUtils.equals( artifact.getClassifier(), artifact.getType() ) )
 173  
             {
 174  63
                 sb.append( "-" );
 175  63
                 sb.append( artifact.getType() );
 176  
             }
 177  
         }
 178  146
         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  0
         file.getParentFile().mkdirs();
 192  
 
 193  0
         FileWriter writer = null;
 194  
 
 195  
         try
 196  
         {
 197  0
             writer = new FileWriter( file, append );
 198  
 
 199  0
             writer.write( string );
 200  
         }
 201  
         finally
 202  
         {
 203  0
             IOUtil.close( writer );
 204  0
         }
 205  0
     }
 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  2
         BufferedReader reader = new BufferedReader( new StringReader( string ) );
 217  
 
 218  
         String line;
 219  
 
 220  17
         while ( ( line = reader.readLine() ) != null )
 221  
         {
 222  15
             log.info( line );
 223  
         }
 224  
 
 225  2
         reader.close();
 226  2
     }
 227  
 
 228  
     //
 229  
     // mainly used to parse excludes,includes configuration
 230  
     //
 231  
     public static String[] tokenizer( String str )
 232  
     {
 233  4
         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  1881
         String ret = "";
 242  1881
         if ( !StringUtils.isEmpty( str ) )
 243  
         {
 244  
             // remove initial and ending spaces, plus all spaces next to commas 
 245  262
             ret = str.trim().replaceAll( "[\\s]*,[\\s]*", "," );
 246  
         }
 247  
 
 248  1881
         return ret;
 249  
     }
 250  
 }