Coverage Report - org.apache.maven.plugin.resources.remote.BundleRemoteResourcesMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
BundleRemoteResourcesMojo
81%
22/27
42%
5/12
0
 
 1  
 package org.apache.maven.plugin.resources.remote;
 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 org.apache.maven.plugin.AbstractMojo;
 23  
 import org.apache.maven.plugin.MojoExecutionException;
 24  
 import org.apache.maven.plugin.resources.remote.io.xpp3.RemoteResourcesBundleXpp3Writer;
 25  
 import org.codehaus.plexus.util.DirectoryScanner;
 26  
 import org.codehaus.plexus.util.FileUtils;
 27  
 import org.codehaus.plexus.util.StringUtils;
 28  
 
 29  
 import java.io.File;
 30  
 import java.io.FileWriter;
 31  
 import java.io.IOException;
 32  
 import java.io.Writer;
 33  
 import java.util.Arrays;
 34  
 import java.util.List;
 35  
 
 36  
 /**
 37  
  * Bundle up resources that should be considered as a remote-resource.
 38  
  *
 39  
  * @goal bundle
 40  
  * @phase generate-resources
 41  
  */
 42  15
 public class BundleRemoteResourcesMojo
 43  
     extends AbstractMojo
 44  
 {
 45  
     public static final String RESOURCES_MANIFEST = "META-INF/maven/remote-resources.xml";
 46  
 
 47  3
     private static final String[] DEFAULT_INCLUDES = new String [] {
 48  
                                                               "**/*.txt",
 49  
                                                               "**/*.vm",
 50  
                                                    };
 51  
 
 52  
 
 53  
     /**
 54  
      * The directory which contains the resources you want packaged up in this resource bundle.
 55  
      *
 56  
      * @parameter expression="${basedir}/src/main/resources"
 57  
      */
 58  
     private File resourcesDirectory;
 59  
 
 60  
     /**
 61  
      * The directory where you want the resource bundle manifest written to.
 62  
      *
 63  
      * @parameter expression="${project.build.outputDirectory}"
 64  
      */
 65  
     private File outputDirectory;
 66  
 
 67  
     /**
 68  
      * A list of files to include. Can contain ant-style wildcards and double wildcards.
 69  
      * The default includes are
 70  
      * <code>**&#47;*.txt   **&#47;*.vm</code>
 71  
      *
 72  
      * @parameter
 73  
      * @since 1.0-alpha-5
 74  
      */
 75  
     private String[] includes;
 76  
 
 77  
     /**
 78  
      * A list of files to exclude. Can contain ant-style wildcards and double wildcards.
 79  
      *
 80  
      * @parameter
 81  
      * @since 1.0-alpha-5
 82  
      */
 83  
     private String[] excludes;
 84  
 
 85  
     /**
 86  
      * Encoding of the bundle.
 87  
      *
 88  
      * @since 1.1
 89  
      * @optional
 90  
      * @parameter expression="${project.build.sourceEncoding}"
 91  
      */
 92  
     private String sourceEncoding;
 93  
 
 94  
     public void execute()
 95  
         throws MojoExecutionException
 96  
     {
 97  15
         if ( !resourcesDirectory.exists() )
 98  
         {
 99  0
             return;
 100  
         }
 101  
 
 102  
         // Look at the content of the resourcesDirectory and create a manifest of the files
 103  
         // so that velocity can easily process any resources inside the JAR that need to be processed.
 104  
 
 105  15
         RemoteResourcesBundle remoteResourcesBundle = new RemoteResourcesBundle();
 106  15
         remoteResourcesBundle.setSourceEncoding( sourceEncoding );
 107  
 
 108  15
         DirectoryScanner scanner = new DirectoryScanner();
 109  
 
 110  15
         scanner.setBasedir( resourcesDirectory );
 111  15
         if ( includes != null && includes.length != 0 )
 112  
         {
 113  0
             scanner.setIncludes( includes );
 114  
         }
 115  
         else
 116  
         {
 117  15
             scanner.setIncludes( DEFAULT_INCLUDES );
 118  
         }
 119  
 
 120  15
         if ( excludes != null && excludes.length != 0 )
 121  
         {
 122  0
             scanner.setExcludes( excludes );
 123  
         }
 124  
 
 125  15
         scanner.addDefaultExcludes();
 126  15
         scanner.scan();
 127  
 
 128  15
         List<String> includedFiles = Arrays.asList( scanner.getIncludedFiles() );
 129  
 
 130  15
         for ( String resource : includedFiles )
 131  
         {
 132  15
             remoteResourcesBundle.addRemoteResource( StringUtils.replace( resource, '\\', '/' ) );
 133  
         }
 134  
 
 135  
 
 136  15
         RemoteResourcesBundleXpp3Writer w = new RemoteResourcesBundleXpp3Writer();
 137  
 
 138  
         try
 139  
         {
 140  15
             File f = new File( outputDirectory, RESOURCES_MANIFEST );
 141  
 
 142  15
             FileUtils.mkdir( f.getParentFile().getAbsolutePath() );
 143  
 
 144  15
             Writer writer = new FileWriter( f );
 145  
 
 146  15
             w.write( writer, remoteResourcesBundle );
 147  
         }
 148  0
         catch ( IOException e )
 149  
         {
 150  0
             throw new MojoExecutionException( "Error creating remote resources manifest.", e );
 151  15
         }
 152  15
     }
 153  
 }