View Javadoc
1   /*
2    * Copyright (c) 2012-present Sonatype, Inc. All rights reserved.
3    *
4    * This program is licensed to you under the Apache License Version 2.0,
5    * and you may not use this file except in compliance with the Apache License Version 2.0.
6    * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
7    *
8    * Unless required by applicable law or agreed to in writing,
9    * software distributed under the Apache License Version 2.0 is distributed on an
10   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
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   * Support for install4jc-based tasks.
26   *
27   * Compatible with install4j version 6.0.0 or higher.
28   *
29   * @since 1.0
30   */
31  public abstract class Install4jcMojoSupport
32      extends MojoSupport
33  {
34    /**
35     * Skip execution.
36     */
37    @Parameter(property = "install4j.skip", defaultValue = "false")
38    protected boolean skip;
39  
40    /**
41     * The location of the install4j installation.
42     */
43    @Parameter(property = "install4j.home")
44    protected File installDir;
45  
46    /**
47     * Fail if the installation is missing.
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      // ensure the binary is executable
86      // FIXME: This can fail (well complain really, if we don't have perms to change the file, should check)
87      ant.chmod(install4jc, "u+x");
88  
89      // ensure version is compatible
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 }