1 package org.apache.maven.plugin.resources.remote;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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.IOUtil;
31 import org.codehaus.plexus.util.ReaderFactory;
32 import org.codehaus.plexus.util.StringUtils;
33
34 import java.io.File;
35 import java.io.FileWriter;
36 import java.io.IOException;
37 import java.io.Writer;
38 import java.util.Arrays;
39 import java.util.List;
40
41
42
43
44
45 @Mojo( name = "bundle", defaultPhase = LifecyclePhase.GENERATE_RESOURCES, threadSafe = true )
46 public class BundleRemoteResourcesMojo
47 extends AbstractMojo
48 {
49 public static final String RESOURCES_MANIFEST = "META-INF/maven/remote-resources.xml";
50
51 private static final String[] DEFAULT_INCLUDES = new String[]{ "**/*.txt", "**/*.vm", };
52
53
54
55
56
57 @Parameter( defaultValue = "${basedir}/src/main/resources" )
58 private File resourcesDirectory;
59
60
61
62
63 @Parameter( defaultValue = "${project.build.outputDirectory}", required = true )
64 private File outputDirectory;
65
66
67
68
69
70
71
72
73 @Parameter
74 private String[] includes;
75
76
77
78
79
80
81 @Parameter
82 private String[] excludes;
83
84
85
86
87
88
89 @Parameter( defaultValue = "${project.build.sourceEncoding}" )
90 private String sourceEncoding;
91
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
109
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 Writer writer = null;
148 try
149 {
150 File f = new File( outputDirectory, RESOURCES_MANIFEST );
151
152 FileUtils.mkdir( f.getParentFile()
153 .getAbsolutePath() );
154
155 writer = new FileWriter( f );
156
157 w.write( writer, remoteResourcesBundle );
158
159 writer.close();
160 writer = null;
161 }
162 catch ( IOException e )
163 {
164 throw new MojoExecutionException( "Error creating remote resources manifest.", e );
165 }
166 finally
167 {
168 IOUtil.close( writer );
169 }
170 }
171 }