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.plugin.compiler;
20
21 import javax.annotation.processing.Processor;
22 import javax.tools.DiagnosticListener;
23 import javax.tools.JavaCompiler;
24 import javax.tools.JavaFileManager;
25 import javax.tools.JavaFileObject;
26
27 import java.io.IOException;
28 import java.io.UncheckedIOException;
29 import java.io.Writer;
30 import java.util.Locale;
31
32 /**
33 * A compiler which is executed by invoking a command-line tool.
34 *
35 * @author Martin Desruisseaux
36 */
37 final class ForkedCompiler extends ForkedTool implements JavaCompiler {
38 /**
39 * Creates a new forked compiler.
40 *
41 * @param mojo the MOJO from which to get the configuration
42 */
43 ForkedCompiler(final AbstractCompilerMojo mojo) {
44 super(mojo);
45 }
46
47 /**
48 * Creates a task for launching the compilation.
49 *
50 * @param out where to send additional compiler output
51 * @param fileManager the {@link ForkedToolSources} instance created by {@link #getStandardFileManager}
52 * @param diagnosticListener currently ignored
53 * @param options compiler options (should be {@link Options#options})
54 * @param classes names of classes to be processed by annotation processing (currently ignored)
55 * @param compilationUnits the source files to compile
56 * @return the compilation task to run
57 */
58 @Override
59 public CompilationTask getTask(
60 Writer out,
61 JavaFileManager fileManager,
62 DiagnosticListener<? super JavaFileObject> diagnosticListener,
63 Iterable<String> options,
64 Iterable<String> classes,
65 Iterable<? extends JavaFileObject> compilationUnits) {
66 return new CompilationTask() {
67 /**
68 * Adds root modules to be taken into account during module resolution.
69 * Currently ignored, caller should use compiler options instead.
70 */
71 @Override
72 public void addModules(Iterable<String> moduleNames) {}
73
74 /**
75 * Sets processors for annotation processing, bypassing the normal discovery mechanism.
76 * Ignored because we cannot pass an instance of a Java object to a command-line.
77 */
78 @Override
79 public void setProcessors(Iterable<? extends Processor> processors) {}
80
81 /**
82 * Sets the locale to be applied when formatting diagnostics and other localized data.
83 * Currently ignored.
84 */
85 @Override
86 public void setLocale(Locale locale) {}
87
88 /**
89 * Performs this compilation task.
90 *
91 * @return true if all the files compiled without errors
92 */
93 @Override
94 public Boolean call() {
95 try {
96 return run(out, (ForkedToolSources) fileManager, options, compilationUnits);
97 } catch (IOException e) {
98 throw new UncheckedIOException(e);
99 }
100 }
101 };
102 }
103 }