View Javadoc
1   package org.apache.maven.plugin.compiler.stubs;
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.codehaus.plexus.compiler.CompilerConfiguration;
23  import org.codehaus.plexus.compiler.CompilerError;
24  import org.codehaus.plexus.compiler.CompilerException;
25  import org.codehaus.plexus.compiler.CompilerMessage;
26  import org.codehaus.plexus.compiler.CompilerOutputStyle;
27  import org.codehaus.plexus.compiler.CompilerResult;
28  
29  import java.io.File;
30  import java.io.IOException;
31  import java.util.Collections;
32  import java.util.List;
33  
34  /**
35   * @author Edwin Punzalan
36   */
37  public class CompilerStub
38      implements org.codehaus.plexus.compiler.Compiler
39  {
40      private boolean shouldFail;
41  
42      public CompilerStub()
43      {
44          this( false );
45      }
46  
47      public CompilerStub( boolean shouldFail )
48      {
49          this.shouldFail = shouldFail;
50      }
51  
52      public CompilerOutputStyle getCompilerOutputStyle()
53      {
54          return CompilerOutputStyle.ONE_OUTPUT_FILE_FOR_ALL_INPUT_FILES;
55      }
56  
57      public String getInputFileEnding( CompilerConfiguration compilerConfiguration )
58      {
59          return "java";
60      }
61  
62      public String getOutputFileEnding( CompilerConfiguration compilerConfiguration )
63      {
64          return "class";
65      }
66  
67      public String getOutputFile( CompilerConfiguration compilerConfiguration )
68      {
69          return "output-file";
70      }
71  
72      public boolean canUpdateTarget( CompilerConfiguration compilerConfiguration )
73      {
74          return false;
75      }
76  
77      public List<CompilerError> compile( CompilerConfiguration compilerConfiguration )
78          throws CompilerException
79      {
80          File outputDir = new File( compilerConfiguration.getOutputLocation() );
81  
82          try
83          {
84              outputDir.mkdirs();
85  
86              File outputFile = new File( outputDir, "compiled.class" );
87              if ( !outputFile.exists() && !outputFile.createNewFile() )
88              {
89                  throw new CompilerException( "could not create output file: " + outputFile.getAbsolutePath() );
90              }
91          }
92          catch ( IOException e )
93          {
94              throw new CompilerException( "An exception occurred while creating output file", e );
95          }
96  
97          return Collections.singletonList( new CompilerError( "message 1", shouldFail ) );
98      }
99  
100     public CompilerResult performCompile( CompilerConfiguration compilerConfiguration )
101         throws CompilerException
102     {
103         File outputDir = new File( compilerConfiguration.getOutputLocation() );
104 
105         try
106         {
107             outputDir.mkdirs();
108 
109             File outputFile = new File( outputDir, "compiled.class" );
110             if ( !outputFile.exists() && !outputFile.createNewFile() )
111             {
112                 throw new CompilerException( "could not create output file: " + outputFile.getAbsolutePath() );
113             }
114         }
115         catch ( IOException e )
116         {
117             throw new CompilerException( "An exception occurred while creating output file", e );
118         }
119         
120         return new CompilerResult( !shouldFail,
121             Collections.singletonList( new CompilerMessage( "message 1", CompilerMessage.Kind.OTHER ) ) );
122     }
123 
124     public String[] createCommandLine( CompilerConfiguration compilerConfiguration )
125     {
126         return new String[0];
127     }
128 }