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