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          throws CompilerException
59      {
60          return "java";
61      }
62  
63      public String getOutputFileEnding( CompilerConfiguration compilerConfiguration )
64          throws CompilerException
65      {
66          return "class";
67      }
68  
69      public String getOutputFile( CompilerConfiguration compilerConfiguration )
70          throws CompilerException
71      {
72          return "output-file";
73      }
74  
75      public boolean canUpdateTarget( CompilerConfiguration compilerConfiguration )
76          throws CompilerException
77      {
78          return false;
79      }
80  
81      public List<CompilerError> compile( CompilerConfiguration compilerConfiguration )
82          throws CompilerException
83      {
84          File outputDir = new File( compilerConfiguration.getOutputLocation() );
85  
86          try
87          {
88              outputDir.mkdirs();
89  
90              File outputFile = new File( outputDir, "compiled.class" );
91              if ( !outputFile.exists() && !outputFile.createNewFile() )
92              {
93                  throw new CompilerException( "could not create output file: " + outputFile.getAbsolutePath() );
94              }
95          }
96          catch ( IOException e )
97          {
98              throw new CompilerException( "An exception occurred while creating output file", e );
99          }
100 
101         return Collections.singletonList( new CompilerError( "message 1", shouldFail ) );
102     }
103 
104     public CompilerResult performCompile( CompilerConfiguration compilerConfiguration )
105         throws CompilerException
106     {
107         File outputDir = new File( compilerConfiguration.getOutputLocation() );
108 
109         try
110         {
111             outputDir.mkdirs();
112 
113             File outputFile = new File( outputDir, "compiled.class" );
114             if ( !outputFile.exists() && !outputFile.createNewFile() )
115             {
116                 throw new CompilerException( "could not create output file: " + outputFile.getAbsolutePath() );
117             }
118         }
119         catch ( IOException e )
120         {
121             throw new CompilerException( "An exception occurred while creating output file", e );
122         }
123         
124         return new CompilerResult( !shouldFail,
125             Collections.singletonList( new CompilerMessage( "message 1", CompilerMessage.Kind.OTHER ) ) );
126     }
127 
128     public String[] createCommandLine( CompilerConfiguration compilerConfiguration )
129         throws CompilerException
130     {
131         return new String[0];
132     }
133 }