View Javadoc

1   /*
2    * #%L
3    * Native ARchive plugin for Maven
4    * %%
5    * Copyright (C) 2002 - 2014 NAR Maven Plugin developers.
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * 
11   * http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package com.github.maven_nar;
21  
22  import java.io.File;
23  import java.io.IOException;
24  
25  import org.apache.maven.plugin.MojoExecutionException;
26  import org.apache.maven.plugin.MojoFailureException;
27  import org.apache.maven.plugins.annotations.LifecyclePhase;
28  import org.apache.maven.plugins.annotations.Mojo;
29  import org.apache.maven.plugins.annotations.Parameter;
30  import org.codehaus.plexus.util.FileUtils;
31  import org.codehaus.plexus.util.SelectorUtils;
32  
33  /**
34   * Copies any resources, including AOL specific distributions, to the target
35   * area for packaging
36   * 
37   * @author Mark Donszelmann
38   */
39  @Mojo(name = "nar-resources", defaultPhase = LifecyclePhase.PROCESS_RESOURCES, requiresProject = true)
40  public class NarResourcesMojo extends AbstractResourcesMojo {
41    /**
42     * Use given AOL only. If false, copy for all available AOLs.
43     */
44    @Parameter(property = "nar.resources.copy.aol", defaultValue = "true", required = true)
45    private boolean resourcesCopyAOL;
46  
47    /**
48     * Directory for nar resources. Defaults to src/nar/resources
49     */
50    @Parameter(defaultValue = "${basedir}/src/nar/resources", required = true)
51    private File resourceDirectory;
52  
53    @Override
54    public final void narExecute() throws MojoExecutionException, MojoFailureException {
55      // noarch resources
56      try {
57        int copied = 0;
58        final File noarchDir = new File(this.resourceDirectory, NarConstants.NAR_NO_ARCH);
59        if (noarchDir.exists()) {
60          final File noarchDstDir = getLayout().getNoArchDirectory(getTargetDirectory(),
61              getMavenProject().getArtifactId(), getMavenProject().getVersion());
62          getLog().debug("Copying noarch from " + noarchDir + " to " + noarchDstDir);
63          copied += NarUtil.copyDirectoryStructure(noarchDir, noarchDstDir, null, NarUtil.DEFAULT_EXCLUDES);
64        }
65        getLog().info("Copied " + copied + " resources");
66      } catch (final IOException e) {
67        throw new MojoExecutionException("NAR: Could not copy resources", e);
68      }
69  
70      // scan resourceDirectory for AOLs
71      final File aolDir = new File(this.resourceDirectory, NarConstants.NAR_AOL);
72      if (aolDir.exists()) {
73        final String[] aol = aolDir.list();
74        for (final String anAol : aol) {
75          // copy only resources of current AOL
76          if (this.resourcesCopyAOL && !anAol.equals(getAOL().toString())) {
77            continue;
78          }
79  
80          boolean ignore = false;
81          for (final Object element : FileUtils.getDefaultExcludesAsList()) {
82            final String exclude = (String) element;
83            if (SelectorUtils.matchPath(exclude.replace('/', File.separatorChar), anAol)) {
84              ignore = true;
85              break;
86            }
87          }
88          if (!ignore) {
89            final File aolFile = new File(aolDir, anAol);
90            copyResources(aolFile, aolFile.getName());
91          }
92        }
93      }
94    }
95  }