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  import java.util.List;
25  
26  import org.apache.maven.plugin.MojoExecutionException;
27  import org.apache.maven.plugin.MojoFailureException;
28  import org.apache.maven.plugins.annotations.Component;
29  import org.apache.maven.plugins.annotations.Parameter;
30  import org.codehaus.plexus.archiver.ArchiverException;
31  import org.codehaus.plexus.archiver.UnArchiver;
32  import org.codehaus.plexus.archiver.manager.ArchiverManager;
33  import org.codehaus.plexus.archiver.manager.NoSuchArchiverException;
34  import org.codehaus.plexus.util.FileUtils;
35  
36  /**
37   * Keeps track of resources
38   *
39   * @author Mark Donszelmann
40   */
41  public abstract class AbstractResourcesMojo extends AbstractNarMojo {
42    /**
43     * Binary directory
44     */
45    @Parameter(defaultValue = "bin", required = true)
46    private String resourceBinDir;
47  
48    /**
49     * Include directory
50     */
51    @Parameter(defaultValue = "include", required = true)
52    private String resourceIncludeDir;
53  
54    /**
55     * Library directory
56     */
57    @Parameter(defaultValue = "lib", required = true)
58    private String resourceLibDir;
59  
60    /**
61     * To look up Archiver/UnArchiver implementations
62     */
63    @Component(role = org.codehaus.plexus.archiver.manager.ArchiverManager.class)
64    private ArchiverManager archiverManager;
65  
66    protected final int copyBinaries(final File srcDir, final String aol)
67        throws IOException, MojoExecutionException, MojoFailureException {
68      int copied = 0;
69  
70      // copy binaries
71      final File binDir = new File(srcDir, this.resourceBinDir);
72      if (binDir.exists()) {
73        final File binDstDir = getLayout().getBinDirectory(getTargetDirectory(), getMavenProject().getArtifactId(),
74            getMavenProject().getVersion(), aol);
75        getLog().debug("Copying binaries from " + binDir + " to " + binDstDir);
76        copied += NarUtil.copyDirectoryStructure(binDir, binDstDir, null, NarUtil.DEFAULT_EXCLUDES);
77      }
78  
79      return copied;
80    }
81  
82    protected final int copyIncludes(final File srcDir) throws IOException, MojoExecutionException, MojoFailureException {
83      int copied = 0;
84  
85      // copy includes
86      final File includeDir = new File(srcDir, this.resourceIncludeDir);
87      if (includeDir.exists()) {
88        final File includeDstDir = getLayout().getIncludeDirectory(getTargetDirectory(),
89            getMavenProject().getArtifactId(), getMavenProject().getVersion());
90        getLog().debug("Copying includes from " + includeDir + " to " + includeDstDir);
91        copied += NarUtil.copyDirectoryStructure(includeDir, includeDstDir, null, NarUtil.DEFAULT_EXCLUDES);
92      }
93  
94      return copied;
95    }
96  
97    protected final int copyLibraries(final File srcDir, final String aol)
98        throws MojoFailureException, IOException, MojoExecutionException {
99      int copied = 0;
100 
101     // copy libraries
102     File libDir = new File(srcDir, this.resourceLibDir);
103     if (libDir.exists()) {
104       // TODO: copyLibraries is used on more than just this artifact - this
105       // check needs to be placed elsewhere
106       if (getLibraries().isEmpty()) {
107         getLog().warn("Appear to have library resources, but not Libraries are defined");
108       }
109       // create all types of libs
110       for (final Object element : getLibraries()) {
111         final Library library = (Library) element;
112         final String type = library.getType();
113 
114         final File typedLibDir = new File(libDir, type);
115         if (typedLibDir.exists()) {
116           libDir = typedLibDir;
117         }
118 
119         final File libDstDir = getLayout().getLibDirectory(getTargetDirectory(), getMavenProject().getArtifactId(),
120             getMavenProject().getVersion(), aol, type);
121         getLog().debug("Copying libraries from " + libDir + " to " + libDstDir);
122 
123         // filter files for lib
124         String includes = "**/*."
125             + NarProperties.getInstance(getMavenProject()).getProperty(
126                 NarUtil.getAOLKey(aol) + "." + type + ".extension");
127 
128         // add import lib for Windows shared libraries
129         if (new AOL(aol).getOS().equals(OS.WINDOWS) && type.equals(Library.SHARED)) {
130           includes += ",**/*.lib";
131         }
132         copied += NarUtil.copyDirectoryStructure(libDir, libDstDir, includes, NarUtil.DEFAULT_EXCLUDES);
133       }
134     }
135 
136     return copied;
137   }
138 
139   protected final void copyResources(final File srcDir, final String aol)
140       throws MojoExecutionException, MojoFailureException {
141     int copied = 0;
142     try {
143       copied += copyIncludes(srcDir);
144 
145       copied += copyBinaries(srcDir, aol);
146 
147       copied += copyLibraries(srcDir, aol);
148 
149       // unpack jar files
150       final File classesDirectory = new File(getOutputDirectory(), "classes");
151       classesDirectory.mkdirs();
152       final List<File> jars = FileUtils.getFiles(srcDir, "**/*.jar", null);
153       for (final File jar : jars) {
154         getLog().debug("Unpacking jar " + jar);
155         UnArchiver unArchiver;
156         unArchiver = this.archiverManager.getUnArchiver(NarConstants.NAR_ROLE_HINT);
157         unArchiver.setSourceFile(jar);
158         unArchiver.setDestDirectory(classesDirectory);
159         unArchiver.extract();
160       }
161     } catch (final IOException e) {
162       throw new MojoExecutionException("NAR: Could not copy resources for " + aol, e);
163     } catch (final NoSuchArchiverException e) {
164       throw new MojoExecutionException("NAR: Could not find archiver for " + aol, e);
165     } catch (final ArchiverException e) {
166       throw new MojoExecutionException("NAR: Could not unarchive jar file for " + aol, e);
167     }
168     getLog().info("Copied " + copied + " resources for " + aol);
169   }
170 
171 }