View Javadoc

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  public class BundleRemoteResourcesMojo
44      extends AbstractMojo
45  {
46      public static final String RESOURCES_MANIFEST = "META-INF/maven/remote-resources.xml";
47  
48      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          if ( !resourcesDirectory.exists() )
99          {
100             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         RemoteResourcesBundle remoteResourcesBundle = new RemoteResourcesBundle();
107         remoteResourcesBundle.setSourceEncoding( sourceEncoding );
108 
109         DirectoryScanner scanner = new DirectoryScanner();
110 
111         scanner.setBasedir( resourcesDirectory );
112         if ( includes != null && includes.length != 0 )
113         {
114             scanner.setIncludes( includes );
115         }
116         else
117         {
118             scanner.setIncludes( DEFAULT_INCLUDES );
119         }
120 
121         if ( excludes != null && excludes.length != 0 )
122         {
123             scanner.setExcludes( excludes );
124         }
125 
126         scanner.addDefaultExcludes();
127         scanner.scan();
128 
129         List<String> includedFiles = Arrays.asList( scanner.getIncludedFiles() );
130 
131         for ( String resource : includedFiles )
132         {
133             remoteResourcesBundle.addRemoteResource( StringUtils.replace( resource, '\\', '/' ) );
134         }
135 
136 
137         RemoteResourcesBundleXpp3Writer w = new RemoteResourcesBundleXpp3Writer();
138 
139         try
140         {
141             File f = new File( outputDirectory, RESOURCES_MANIFEST );
142 
143             FileUtils.mkdir( f.getParentFile().getAbsolutePath() );
144 
145             Writer writer = new FileWriter( f );
146 
147             w.write( writer, remoteResourcesBundle );
148         }
149         catch ( IOException e )
150         {
151             throw new MojoExecutionException( "Error creating remote resources manifest.", e );
152         }
153     }
154 }