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