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