View Javadoc
1   package org.apache.maven.plugin.coreit;
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 org.apache.maven.execution.MavenSession;
23  import org.apache.maven.plugin.AbstractMojo;
24  import org.apache.maven.plugin.MojoExecutionException;
25  import org.apache.maven.toolchain.ToolchainManagerPrivate;
26  import org.apache.maven.toolchain.ToolchainPrivate;
27  
28  import java.io.File;
29  import java.io.FileOutputStream;
30  import java.io.IOException;
31  import java.io.OutputStream;
32  import java.lang.reflect.InvocationTargetException;
33  import java.lang.reflect.Method;
34  import java.util.Arrays;
35  import java.util.Iterator;
36  import java.util.Properties;
37  
38  /**
39   * @goal toolchain
40   * @phase validate
41   */
42  public class CoreItMojo
43      extends AbstractMojo
44  {
45  
46      /**
47       * @component
48       */
49      private ToolchainManagerPrivate toolchainManager;
50  
51      /**
52       * The current Maven session holding the selected toolchain.
53       *
54       * @parameter default-value="${session}"
55       * @required
56       * @readonly
57       */
58      private MavenSession session;
59  
60      /**
61       * The path to the output file for the properties.
62       *
63       * @parameter property="toolchain.outputFile" default-value="${project.build.directory}/toolchains.properties"
64       */
65      private File outputFile;
66  
67      /**
68       * The type identifier of the toolchain, e.g. "jdk".
69       *
70       * @parameter property="toolchain.type"
71       */
72      private String type;
73  
74      /**
75       * The name of the tool, e.g. "javac".
76       *
77       * @parameter property="toolchain.tool"
78       */
79      private String tool;
80  
81      /**
82       * The zero-based index of the toolchain to select and store in the build context.
83       *
84       * @parameter property="toolchain.selected"
85       */
86      private int selected;
87  
88      public void execute()
89          throws MojoExecutionException
90      {
91          ToolchainPrivate[] tcs = getToolchains();
92  
93          getLog().info( "[MAVEN-CORE-IT-LOG] Toolchains in plugin: " + Arrays.asList( tcs ) );
94  
95          if ( selected >= 0 )
96          {
97              if ( selected < tcs.length )
98              {
99                  ToolchainPrivate toolchain = tcs[selected];
100                 toolchainManager.storeToolchainToBuildContext( toolchain, session );
101             }
102             else
103             {
104                 getLog().warn(
105                     "[MAVEN-CORE-IT-LOG] Toolchain #" + selected + " can't be selected, found only " + tcs.length );
106             }
107         }
108 
109         Properties properties = new Properties();
110 
111         int count = 1;
112         for ( Iterator<ToolchainPrivate> i = Arrays.<ToolchainPrivate>asList( tcs ).iterator(); i.hasNext(); count++ )
113         {
114             ToolchainPrivate toolchain = i.next();
115 
116             String foundTool = toolchain.findTool( tool );
117             if ( foundTool != null )
118             {
119                 properties.setProperty( "tool." + count, foundTool );
120             }
121         }
122 
123         OutputStream out = null;
124         try
125         {
126             outputFile.getParentFile().mkdirs();
127             out = new FileOutputStream( outputFile );
128             properties.store( out, "MAVEN-CORE-IT-LOG" );
129         }
130         catch ( IOException e )
131         {
132             throw new MojoExecutionException( e.getMessage(), e );
133         }
134         finally
135         {
136             if ( out != null )
137             {
138                 try
139                 {
140                     out.close();
141                 }
142                 catch ( IOException e )
143                 {
144                     // ignore
145                 }
146             }
147         }
148     }
149 
150     private ToolchainPrivate[] getToolchains()
151         throws MojoExecutionException
152     {
153         Class<? extends ToolchainManagerPrivate> managerClass = toolchainManager.getClass();
154 
155         try
156         {
157             try
158             {
159                 // try 2.x style API
160                 Method oldMethod = managerClass.getMethod( "getToolchainsForType", new Class[]{ String.class } );
161 
162                 return (ToolchainPrivate[]) oldMethod.invoke( toolchainManager, new Object[]{ type } );
163             }
164             catch ( NoSuchMethodException e )
165             {
166                 // try 3.x style API
167                 Method newMethod =
168                     managerClass.getMethod( "getToolchainsForType", new Class[]{ String.class, MavenSession.class } );
169 
170                 return (ToolchainPrivate[]) newMethod.invoke( toolchainManager, new Object[]{ type, session } );
171             }
172         }
173         catch ( NoSuchMethodException | InvocationTargetException | IllegalAccessException e )
174         {
175             throw new MojoExecutionException( "Incompatible toolchain API", e );
176         }
177     }
178 
179 }