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