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 java.io.File;
23  import java.io.FileOutputStream;
24  import java.io.IOException;
25  import java.io.OutputStream;
26  import java.lang.reflect.InvocationTargetException;
27  import java.lang.reflect.Method;
28  import java.util.Arrays;
29  import java.util.Iterator;
30  import java.util.Properties;
31  
32  import org.apache.maven.execution.MavenSession;
33  import org.apache.maven.plugin.AbstractMojo;
34  import org.apache.maven.plugin.MojoExecutionException;
35  import org.apache.maven.toolchain.MisconfiguredToolchainException;
36  import org.apache.maven.toolchain.ToolchainManagerPrivate;
37  import org.apache.maven.toolchain.ToolchainPrivate;
38  
39  /**
40   * @goal toolchain
41   * @phase validate
42   */
43  public class CoreItMojo
44      extends AbstractMojo
45  {
46  
47      /**
48       * @component
49       */
50      private ToolchainManagerPrivate toolchainManager;
51  
52      /**
53       * The current Maven session holding the selected toolchain.
54       * 
55       * @parameter expression="${session}"
56       * @required
57       * @readonly
58       */
59      private MavenSession session;
60  
61      /**
62       * The path to the output file for the properties.
63       * 
64       * @parameter expression="${toolchain.outputFile}" default-value="${project.build.directory}/toolchains.properties"
65       */
66      private File outputFile;
67  
68      /**
69       * The type identifier of the toolchain, e.g. "jdk".
70       * 
71       * @parameter expression="${toolchain.type}"
72       */
73      private String type;
74  
75      /**
76       * The name of the tool, e.g. "javac".
77       * 
78       * @parameter expression="${toolchain.tool}"
79       */
80      private String tool;
81  
82      /**
83       * The zero-based index of the toolchain to select and store in the build context.
84       * 
85       * @parameter expression="${toolchain.selected}"
86       */
87      private int selected;
88  
89      public void execute()
90          throws MojoExecutionException
91      {
92          ToolchainPrivate[] tcs = getToolchains();
93  
94          getLog().info( "[MAVEN-CORE-IT-LOG] Toolchains in plugin: " + Arrays.asList( tcs ) );
95  
96          if ( selected >= 0 )
97          {
98              if ( selected < tcs.length )
99              {
100                 ToolchainPrivate toolchain = tcs[selected];
101                 toolchainManager.storeToolchainToBuildContext( toolchain, session );
102             }
103             else
104             {
105                 getLog().warn( "[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 i = Arrays.asList( tcs ).iterator(); i.hasNext(); count++ )
113         {
114             ToolchainPrivate toolchain = (ToolchainPrivate) 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 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 e )
174         {
175             throw new MojoExecutionException( "Incompatible toolchain API", e );
176         }
177         catch ( IllegalAccessException e )
178         {
179             throw new MojoExecutionException( "Incompatible toolchain API", e );
180         }
181         catch ( InvocationTargetException e )
182         {
183             throw new MojoExecutionException( "Incompatible toolchain API", e );
184         }
185     }
186 
187 }