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.BufferedInputStream;
23  import java.io.File;
24  import java.io.IOException;
25  import java.util.ArrayList;
26  import java.util.List;
27  
28  import org.apache.maven.plugin.MojoExecutionException;
29  import org.apache.maven.plugin.MojoFailureException;
30  import org.apache.maven.plugin.logging.Log;
31  import org.apache.maven.plugins.annotations.LifecyclePhase;
32  import org.apache.maven.plugins.annotations.Mojo;
33  import org.apache.maven.plugins.annotations.Parameter;
34  import org.apache.maven.shared.artifact.filter.collection.ScopeFilter;
35  
36  /**
37   * Adds the ability to run arbitrary command line tools to post-process the
38   * compiled output (ie: ranlib/ar/etc)
39   *
40   * @author Richard Kerr
41   * @author Richard Kerr
42   */
43  @Mojo(name = "nar-process-libraries", defaultPhase = LifecyclePhase.PROCESS_CLASSES, requiresProject = true)
44  public class NarProcessLibraries extends AbstractCompileMojo {
45  
46    /**
47     * List of commands to execute
48     */
49    @Parameter
50    private List<ProcessLibraryCommand> commands;
51  
52    private final Log log = getLog();
53  
54    /**
55     * The method must be implemented but will not be called.
56     */
57    @Override
58    protected ScopeFilter getArtifactScopeFilter() {
59      return null;
60    }
61  
62    @Override
63    public void narExecute() throws MojoFailureException, MojoExecutionException {
64      this.log.info("Running process libraries");
65      // For each of the libraries defined for this build
66      for (final Library library : getLibraries()) {
67        this.log.info("Processing library " + library);
68        final String type = library.getType();
69        File outFile;
70        // Find what the output directory is
71        if (type.equalsIgnoreCase(Library.EXECUTABLE)) {
72          final File outDir = getLayout().getBinDirectory(getTargetDirectory(), getMavenProject().getArtifactId(),
73              getMavenProject().getVersion(), getAOL().toString());
74          outFile = new File(outDir, getOutput(false));
75        } else {
76          final File outDir = getLayout().getLibDirectory(getTargetDirectory(), getMavenProject().getArtifactId(),
77              getMavenProject().getVersion(), getAOL().toString(), type);
78          outFile = new File(outDir, getOutput(true));
79        }
80  
81        // Then run the commands that are applicable for this library type
82        for (final ProcessLibraryCommand command : this.commands == null ? new ArrayList<ProcessLibraryCommand>()
83            : this.commands) {
84          if (command.getType().equalsIgnoreCase(type)) {
85            runCommand(command, outFile);
86          }
87        }
88      }
89  
90    }
91  
92    private void runCommand(final ProcessLibraryCommand command, final File outputFile)
93        throws MojoFailureException, MojoExecutionException {
94      final ProcessBuilder p = new ProcessBuilder(command.getCommandList());
95      p.command().add(outputFile.toString());
96      p.redirectErrorStream(true);
97      this.log.info("Running command \"" + p.command() + "\"");
98      try {
99        final Process process = p.start();
100       final BufferedInputStream bis = new BufferedInputStream(process.getInputStream());
101       final byte[] buffer = new byte[1024];
102       int endOfStream = 0;
103       do {
104         endOfStream = bis.read(buffer);
105         this.log.debug(new String(buffer, 0, endOfStream == -1 ? 0 : endOfStream));
106       } while (endOfStream != -1);
107 
108       if (process.waitFor() != 0) {
109         // TODO: Maybe this shouldn't be an exception, it might have
110         // still worked?!
111         throw new MojoFailureException("Process exited abnormally");
112       }
113     } catch (final IOException | InterruptedException e) {
114       e.printStackTrace();
115       throw new MojoFailureException("Failed to run the command \"" + p.command() + "\"", e);
116     }
117   }
118 
119 }