1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
38
39
40
41
42
43 @Mojo(name = "nar-process-libraries", defaultPhase = LifecyclePhase.PROCESS_CLASSES, requiresProject = true)
44 public class NarProcessLibraries extends AbstractCompileMojo {
45
46
47
48
49 @Parameter
50 private List<ProcessLibraryCommand> commands;
51
52 private final Log log = getLog();
53
54
55
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
66 for (final Library library : getLibraries()) {
67 this.log.info("Processing library " + library);
68 final String type = library.getType();
69 File outFile;
70
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
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
110
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 }