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.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
42
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
55
56 @Parameter( defaultValue = "${basedir}/src/main/resources" )
57 private File resourcesDirectory;
58
59
60
61
62 @Parameter( defaultValue = "${project.build.outputDirectory}", required = true )
63 private File outputDirectory;
64
65
66
67
68
69
70
71
72 @Parameter
73 private String[] includes;
74
75
76
77
78
79
80 @Parameter
81 private String[] excludes;
82
83
84
85
86
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
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 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 }