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.File;
23 import java.io.IOException;
24 import java.lang.reflect.Constructor;
25 import java.lang.reflect.InvocationTargetException;
26
27 import org.apache.maven.plugin.MojoExecutionException;
28 import org.apache.maven.plugin.MojoFailureException;
29 import org.apache.maven.plugin.logging.Log;
30 import org.apache.maven.project.MavenProject;
31 import org.apache.maven.project.MavenProjectHelper;
32 import org.codehaus.plexus.archiver.Archiver;
33 import org.codehaus.plexus.archiver.ArchiverException;
34 import org.codehaus.plexus.archiver.UnArchiver;
35 import org.codehaus.plexus.archiver.manager.ArchiverManager;
36 import org.codehaus.plexus.archiver.manager.NoSuchArchiverException;
37
38
39
40
41
42 public abstract class AbstractNarLayout implements NarLayout, NarConstants {
43
44
45
46
47 public static NarLayout getLayout(final String layoutName, final Log log) throws MojoExecutionException {
48 final String className = layoutName.indexOf('.') < 0 ? NarLayout21.class.getPackage().getName() + "." + layoutName
49 : layoutName;
50 log.debug("Using " + className);
51 Class cls;
52 try {
53 cls = Class.forName(className);
54 final Constructor ctor = cls.getConstructor(Log.class);
55 return (NarLayout) ctor.newInstance(log);
56 } catch (final ClassNotFoundException e) {
57 throw new MojoExecutionException("Cannot find class for layout " + className, e);
58 } catch (final InstantiationException e) {
59 throw new MojoExecutionException("Cannot instantiate class for layout " + className, e);
60 } catch (final IllegalAccessException | SecurityException e) {
61 throw new MojoExecutionException("Cannot access class for layout " + className, e);
62 } catch (final NoSuchMethodException e) {
63 throw new MojoExecutionException("Cannot find ctor(Log) for layout " + className, e);
64 } catch (final IllegalArgumentException e) {
65 throw new MojoExecutionException("Wrong arguments ctor(Log) for layout " + className, e);
66 } catch (final InvocationTargetException e) {
67 throw new MojoExecutionException("Cannot invokector(Log) for layout " + className, e);
68 }
69 }
70
71 private final Log log;
72
73 protected AbstractNarLayout(final Log log) {
74 this.log = log;
75 }
76
77 protected final void attachNar(final ArchiverManager archiverManager, final MavenProjectHelper projectHelper,
78 final MavenProject project, final String classifier, final File dir, final String include)
79 throws MojoExecutionException {
80 final File narFile = new File(project.getBuild().getDirectory(), project.getBuild().getFinalName() + "-"
81 + classifier + "." + NarConstants.NAR_EXTENSION);
82 if (narFile.exists()) {
83 narFile.delete();
84 }
85 try {
86 final Archiver archiver = archiverManager.getArchiver(NarConstants.NAR_ROLE_HINT);
87 archiver.addDirectory(dir, new String[] {
88 include
89 }, null);
90 archiver.setDestFile(narFile);
91 archiver.createArchive();
92 } catch (final NoSuchArchiverException e) {
93 throw new MojoExecutionException("NAR: cannot find archiver", e);
94 } catch (final ArchiverException | IOException e) {
95 throw new MojoExecutionException("NAR: cannot create NAR archive '" + narFile + "'", e);
96 }
97 projectHelper.attachArtifact(project, NarConstants.NAR_TYPE, classifier, narFile);
98 }
99
100 protected Log getLog() {
101 return this.log;
102 }
103
104 protected void unpackNarAndProcess(final ArchiverManager archiverManager, final File file, final File narLocation,
105 final String os, final String linkerName, final AOL defaultAOL)
106 throws MojoExecutionException, MojoFailureException {
107
108 final String gpp = "g++";
109 final String gcc = "gcc";
110
111 narLocation.mkdirs();
112
113
114 try {
115 UnArchiver unArchiver;
116 unArchiver = archiverManager.getUnArchiver(NarConstants.NAR_ROLE_HINT);
117 unArchiver.setSourceFile(file);
118 unArchiver.setDestDirectory(narLocation);
119 unArchiver.extract();
120 } catch (final NoSuchArchiverException | ArchiverException e) {
121 throw new MojoExecutionException("Error unpacking file: " + file + " to: " + narLocation, e);
122 }
123
124
125 if (!NarUtil.getOS(os).equals(OS.WINDOWS)) {
126 NarUtil.makeExecutable(new File(narLocation, "bin/" + defaultAOL), this.log);
127
128 if (defaultAOL.hasLinker(gpp)) {
129 NarUtil.makeExecutable(new File(narLocation, "bin/" + NarUtil.replace(gpp, gcc, defaultAOL.toString())),
130 this.log);
131 }
132
133 NarUtil.makeLink(new File(narLocation, "lib/" + defaultAOL), this.log);
134 }
135 if (linkerName.equals(gcc) || linkerName.equals(gpp)) {
136 NarUtil.runRanlib(new File(narLocation, "lib/" + defaultAOL), this.log);
137
138 if (defaultAOL.hasLinker(gpp)) {
139 NarUtil.runRanlib(new File(narLocation, "lib/" + NarUtil.replace(gpp, gcc, defaultAOL.toString())), this.log);
140 }
141 }
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159 }
160
161 }