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.util.Properties;
27
28 import org.apache.maven.execution.MavenSession;
29 import org.apache.maven.plugin.AbstractMojo;
30 import org.apache.maven.plugin.MojoExecutionException;
31 import org.apache.maven.toolchain.Toolchain;
32 import org.apache.maven.toolchain.ToolchainManager;
33
34 /**
35 * Finds a tool from a previously selected toolchain. This tests the public API just like toolchain-enabled plugins
36 * would do.
37 *
38 * @goal find-tool
39 * @phase validate
40 */
41 public class FindToolMojo
42 extends AbstractMojo
43 {
44
45 /**
46 * @component
47 */
48 private ToolchainManager toolchainManager;
49
50 /**
51 * The current Maven session holding the selected toolchain.
52 *
53 * @parameter default-value="${session}"
54 * @required
55 * @readonly
56 */
57 private MavenSession session;
58
59 /**
60 * The path to the output file for the properties.
61 *
62 * @parameter property="toolchain.outputFile" default-value="${project.build.directory}/tool.properties"
63 */
64 private File outputFile;
65
66 /**
67 * The type identifier of the toolchain, e.g. "jdk".
68 *
69 * @parameter property="toolchain.type"
70 */
71 private String type;
72
73 /**
74 * The name of the tool, e.g. "javac".
75 *
76 * @parameter property="toolchain.tool"
77 */
78 private String tool;
79
80 public void execute()
81 throws MojoExecutionException
82 {
83 Toolchain toolchain = toolchainManager.getToolchainFromBuildContext( type, session );
84
85 getLog().info( "[MAVEN-CORE-IT-LOG] Toolchain in session: " + toolchain );
86
87 Properties properties = new Properties();
88
89 if ( toolchain != null )
90 {
91 properties.setProperty( "toolchain.type", toolchain.getType() );
92
93 String path = toolchain.findTool( tool );
94 if ( path != null )
95 {
96 properties.setProperty( "tool." + tool, path );
97 }
98 }
99
100 OutputStream out = null;
101 try
102 {
103 outputFile.getParentFile().mkdirs();
104 out = new FileOutputStream( outputFile );
105 properties.store( out, "MAVEN-CORE-IT-LOG" );
106 }
107 catch ( IOException e )
108 {
109 throw new MojoExecutionException( e.getMessage(), e );
110 }
111 finally
112 {
113 if ( out != null )
114 {
115 try
116 {
117 out.close();
118 }
119 catch ( IOException e )
120 {
121 // ignore
122 }
123 }
124 }
125 }
126 }