1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.geronimo.genesis.plugins.tools;
21
22 import java.io.File;
23
24 import org.codehaus.plexus.util.DirectoryScanner;
25
26 import org.apache.maven.plugin.MojoExecutionException;
27 import org.apache.maven.plugin.MojoFailureException;
28 import org.apache.maven.project.MavenProject;
29 import org.apache.tools.ant.taskdefs.Copy;
30 import org.apache.tools.ant.types.FileSet;
31
32 import org.apache.geronimo.genesis.MojoSupport;
33 import org.apache.geronimo.genesis.ant.AntHelper;
34
35 /**
36 * Copy legal files for inclusion into generated jars.
37 *
38 * @goal copy-legal-files
39 * @phase validate
40 *
41 * @version $Rev: 486420 $ $Date: 2006-12-12 15:51:25 -0800 (Tue, 12 Dec 2006) $
42 */
43 public class CopyLegalFilesMojo
44 extends MojoSupport
45 {
46 /**
47 * The default includes when no fileset is configured.
48 */
49 private static final String[] DEFAULT_INCLUDES = {
50 "LICENSE.txt",
51 "LICENSE",
52 "NOTICE.txt",
53 "NOTICE",
54 "DISCLAIMER.txt",
55 "DISCLAIMER"
56 };
57
58 /**
59 * Directory to copy legal files into.
60 *
61 * @parameter expression="${project.build.outputDirectory}/META-INF"
62 * @required
63 */
64 private File outputDirectory = null;
65
66 /**
67 * The basedir of the project.
68 *
69 * @parameter expression="${basedir}"
70 * @required
71 * @readonly
72 */
73 protected File basedir;
74
75 /**
76 * The set of legal files to be copied. Default fileset includes: LICENSE[.txt], NOTICE[.txt] and DISCLAIMER[.txt].
77 *
78 * @parameter
79 */
80 private DirectoryScanner fileset;
81
82 /**
83 * When set to true, fail the build when no legal files are found.
84 *
85 * @parameter default-value="false"
86 */
87 private boolean strict;
88
89 /**
90 * @component
91 */
92 private AntHelper ant;
93
94
95
96
97
98 /**
99 * The maven project.
100 *
101 * @parameter expression="${project}"
102 * @required
103 * @readonly
104 */
105 protected MavenProject project = null;
106
107 protected MavenProject getProject() {
108 return project;
109 }
110
111
112
113
114
115 protected void init() throws MojoExecutionException, MojoFailureException {
116 super.init();
117
118 ant.setProject(getProject());
119 }
120
121 protected void doExecute() throws Exception {
122 if (!shouldInstallLegalFiles(getProject())) {
123 return;
124 }
125
126 if (fileset == null) {
127 fileset = new DirectoryScanner();
128 fileset.setBasedir(basedir);
129 fileset.setIncludes(DEFAULT_INCLUDES);
130 }
131
132 fileset.addDefaultExcludes();
133 fileset.scan();
134
135 String[] filenames = fileset.getIncludedFiles();
136
137 if (filenames.length == 0) {
138 if (strict) {
139 throw new MojoExecutionException("No legal files found to copy");
140 }
141 else {
142 log.warn("No legal files found to copy");
143 }
144
145 return;
146 }
147
148 ant.mkdir(outputDirectory);
149
150 Copy copy = (Copy)ant.createTask("copy");
151 copy.setTodir(outputDirectory);
152
153 FileSet files = ant.createFileSet();
154 files.setDir(basedir);
155
156 for (int i=0; i<filenames.length; i++) {
157 files.createInclude().setName(filenames[i]);
158 }
159
160 copy.addFileset(files);
161
162 copy.execute();
163 }
164
165 private boolean shouldInstallLegalFiles(final MavenProject project) {
166 assert project != null;
167
168
169
170
171
172 return !"pom".equals(getProject().getPackaging());
173 }
174 }