View Javadoc
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.stubs;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.util.Collections;
24  
25  import org.codehaus.plexus.compiler.CompilerConfiguration;
26  import org.codehaus.plexus.compiler.CompilerException;
27  import org.codehaus.plexus.compiler.CompilerMessage;
28  import org.codehaus.plexus.compiler.CompilerOutputStyle;
29  import org.codehaus.plexus.compiler.CompilerResult;
30  
31  /**
32   * @author Edwin Punzalan
33   */
34  public class CompilerStub implements org.codehaus.plexus.compiler.Compiler {
35      private boolean shouldFail;
36  
37      public CompilerStub() {
38          this(false);
39      }
40  
41      public CompilerStub(boolean shouldFail) {
42          this.shouldFail = shouldFail;
43      }
44  
45      public CompilerOutputStyle getCompilerOutputStyle() {
46          return CompilerOutputStyle.ONE_OUTPUT_FILE_FOR_ALL_INPUT_FILES;
47      }
48  
49      public String getInputFileEnding(CompilerConfiguration compilerConfiguration) {
50          return "java";
51      }
52  
53      public String getOutputFileEnding(CompilerConfiguration compilerConfiguration) {
54          return "class";
55      }
56  
57      public String getOutputFile(CompilerConfiguration compilerConfiguration) {
58          return "output-file";
59      }
60  
61      public boolean canUpdateTarget(CompilerConfiguration compilerConfiguration) {
62          return false;
63      }
64  
65      public CompilerResult performCompile(CompilerConfiguration compilerConfiguration) throws CompilerException {
66          File outputDir = new File(compilerConfiguration.getOutputLocation());
67  
68          try {
69              outputDir.mkdirs();
70  
71              File outputFile = new File(outputDir, "compiled.class");
72              if (!outputFile.exists() && !outputFile.createNewFile()) {
73                  throw new CompilerException("could not create output file: " + outputFile.getAbsolutePath());
74              }
75          } catch (IOException e) {
76              throw new CompilerException("An exception occurred while creating output file", e);
77          }
78  
79          return new CompilerResult(
80                  !shouldFail, Collections.singletonList(new CompilerMessage("message 1", CompilerMessage.Kind.OTHER)));
81      }
82  
83      public String[] createCommandLine(CompilerConfiguration compilerConfiguration) {
84          return new String[0];
85      }
86  }