1
2
3
4
5
6
7
8
9
10
11
12
13 package org.sonatype.install4j.maven;
14
15 import java.io.File;
16
17 import org.apache.maven.plugin.MojoExecutionException;
18 import org.apache.maven.plugins.annotations.Component;
19 import org.apache.maven.plugins.annotations.Parameter;
20 import org.apache.maven.project.MavenProject;
21 import org.apache.tools.ant.taskdefs.ExecTask;
22 import org.codehaus.plexus.util.Os;
23
24
25
26
27
28
29
30
31 public abstract class Install4jcMojoSupport
32 extends MojoSupport
33 {
34
35
36
37 @Parameter(property = "install4j.skip", defaultValue = "false")
38 protected boolean skip;
39
40
41
42
43 @Parameter(property = "install4j.home")
44 protected File installDir;
45
46
47
48
49 @Parameter(property = "install4j.failIfMissing", defaultValue = "false")
50 protected boolean failIfMissing;
51
52 @Component
53 protected MavenProject project;
54
55 @Override
56 protected void doExecute() throws Exception {
57 if (skip) {
58 log.warn("Skipping execution");
59 return;
60 }
61
62 AntHelper ant = new AntHelper(this, project);
63
64 if (installDir == null) {
65 maybeFailIfMissing("Install directory not configured");
66 return;
67 }
68 if (!installDir.exists()) {
69 maybeFailIfMissing("Invalid install directory: " + installDir);
70 return;
71 }
72
73 log.debug("install4j installation directory: " + installDir);
74
75 boolean windows = Os.isFamily(Os.FAMILY_WINDOWS);
76 File install4jc = new File(installDir, "bin/install4jc" + (windows ? ".exe" : ""));
77
78 if (!install4jc.exists()) {
79 maybeFailIfMissing("Missing install4j compiler executable: " + install4jc);
80 return;
81 }
82
83 log.debug("install4jc: " + install4jc);
84
85
86
87 ant.chmod(install4jc, "u+x");
88
89
90 VersionHelper versionHelper = new VersionHelper(log);
91 String versionProperty = versionHelper.fetchVersion(ant, install4jc);
92 versionHelper.ensureVersionCompatible(ant.getProperty(versionProperty));
93
94 ExecTask task = ant.createTask(ExecTask.class);
95 task.setExecutable(install4jc.getAbsolutePath());
96 execute(ant, task);
97 }
98
99 protected abstract void execute(final AntHelper ant, final ExecTask task) throws Exception;
100
101 private void maybeFailIfMissing(final String message) throws MojoExecutionException {
102 if (failIfMissing) {
103 log.error(message);
104 throw new MojoExecutionException(message);
105 }
106 else {
107 log.warn(message);
108 }
109 }
110 }