View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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      // MojoSupport Hooks
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     // Mojo
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         // TODO: Expose a list of packagings
170         //
171         
172         return !"pom".equals(getProject().getPackaging());
173     }
174 }