View Javadoc
1   package org.apache.maven.plugins.jmod;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.io.IOException;
24  import java.io.PrintStream;
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.codehaus.plexus.util.cli.Commandline;
33  
34  /**
35   * This goal is to support the usage of <code>jmod list</code> to show the content of a <code>jmod</code> file.
36   * 
37   * @author Karl Heinz Marbaise <a href="mailto:khmarbaise@apache.org">khmarbaise@apache.org</a>
38   */
39  @Mojo( name = "list", requiresDependencyResolution = ResolutionScope.NONE, defaultPhase = LifecyclePhase.NONE )
40  public class JModListMojo
41      extends AbstractJModMojo
42  {
43  
44      @Parameter( defaultValue = "${project.build.directory}", required = true, readonly = true )
45      private File outputDirectory;
46  
47      /**
48       * The name of the jmod file which is used to be examined via <code>jmod list jmodFile</code>
49       */
50      //@formatter:off
51      @Parameter( 
52          defaultValue = "${project.build.directory}/jmods/${project.artifactId}.jmod", 
53          property = "jmodfile", 
54          required = true 
55      )
56      //@formatter:on
57      private File jmodFile;
58  
59      public void execute()
60          throws MojoExecutionException, MojoFailureException
61      {
62  
63          String jModExecutable;
64          try
65          {
66              jModExecutable = getJModExecutable();
67          }
68          catch ( IOException e )
69          {
70              throw new MojoFailureException( "Unable to find jmod command: " + e.getMessage(), e );
71          }
72  
73          getLog().info( "Toolchain in maven-jmod-plugin: jmod [ " + jModExecutable + " ]" );
74  
75          if ( !jmodFile.exists() || !jmodFile.isFile() )
76          {
77              throw new MojoFailureException( "Unable to find " + jmodFile.getAbsolutePath() );
78          }
79  
80          Commandline cmd;
81          try
82          {
83              cmd = createJModListCommandLine( jmodFile );
84          }
85          catch ( IOException e )
86          {
87              throw new MojoExecutionException( e.getMessage() );
88          }
89          cmd.setExecutable( jModExecutable );
90  
91          getLog().info( "The following files are contained in the module file " + jmodFile.getAbsolutePath() );
92          executeCommand( cmd, outputDirectory );
93  
94      }
95  
96      private Commandline createJModListCommandLine( File resultingJModFile )
97          throws IOException
98      {
99          File file = new File( outputDirectory, "jmodListArgs" );
100         if ( !getLog().isDebugEnabled() )
101         {
102             file.deleteOnExit();
103         }
104         file.getParentFile().mkdirs();
105         file.createNewFile();
106 
107         PrintStream argsFile = new PrintStream( file );
108 
109         argsFile.println( "list" );
110 
111         argsFile.println( resultingJModFile.getAbsolutePath() );
112         argsFile.close();
113 
114         Commandline cmd = new Commandline();
115         cmd.createArg().setValue( '@' + file.getAbsolutePath() );
116 
117         return cmd;
118     }
119 
120 }