View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.jmod;
20  
21  import javax.inject.Inject;
22  
23  import java.io.File;
24  import java.io.IOException;
25  
26  import org.apache.maven.plugin.MojoExecutionException;
27  import org.apache.maven.plugin.MojoFailureException;
28  import org.apache.maven.plugins.annotations.LifecyclePhase;
29  import org.apache.maven.plugins.annotations.Mojo;
30  import org.apache.maven.plugins.annotations.Parameter;
31  import org.apache.maven.plugins.annotations.ResolutionScope;
32  import org.apache.maven.shared.utils.cli.Commandline;
33  import org.apache.maven.toolchain.ToolchainManager;
34  
35  /**
36   * This goal is to support the usage of <code>jmod list</code> to show the content of a <code>jmod</code> file.
37   *
38   * @author Karl Heinz Marbaise <a href="mailto:khmarbaise@apache.org">khmarbaise@apache.org</a>
39   */
40  @Mojo(name = "list", requiresDependencyResolution = ResolutionScope.NONE, defaultPhase = LifecyclePhase.NONE)
41  public class JModListMojo extends AbstractJModMojo {
42  
43      @Parameter(defaultValue = "${project.build.directory}", required = true, readonly = true)
44      private File outputDirectory;
45  
46      /**
47       * The name of the jmod file which is examined via <code>jmod list jmodFile</code>.
48       */
49      // @formatter:off
50      @Parameter(
51              defaultValue = "${project.build.directory}/jmods/${project.artifactId}.jmod",
52              property = "jmodfile",
53              required = true)
54      // @formatter:on
55      private File jmodFile;
56  
57      @Inject
58      public JModListMojo(ToolchainManager toolchainManager) {
59          super(toolchainManager);
60      }
61  
62      public void execute() throws MojoExecutionException, MojoFailureException {
63          try {
64              String jModExecutable = getJModExecutable();
65              getLog().debug("Toolchain in maven-jmod-plugin: jmod [ " + jModExecutable + " ]");
66  
67              Commandline cmd = createJModListCommandLine();
68              cmd.setExecutable(jModExecutable);
69  
70              getLog().info("The following files are contained in the module file " + jmodFile.getAbsolutePath());
71              executeCommand(cmd, outputDirectory);
72          } catch (IOException e) {
73              throw new MojoFailureException("Unable to find jmod command: " + e.getMessage(), e);
74          }
75      }
76  
77      private Commandline createJModListCommandLine() throws MojoFailureException {
78          if (!jmodFile.exists() || !jmodFile.isFile()) {
79              throw new MojoFailureException("Unable to find " + jmodFile.getAbsolutePath());
80          }
81  
82          Commandline commandLine = new Commandline();
83          commandLine.createArg().setValue("list");
84          commandLine.createArg().setValue(jmodFile.getAbsolutePath());
85          return commandLine;
86      }
87  }