View Javadoc
1   package org.codehaus.plexus.compiler;
2   
3   /**
4    * The MIT License
5    *
6    * Copyright (c) 2004, The Codehaus
7    *
8    * Permission is hereby granted, free of charge, to any person obtaining a copy of
9    * this software and associated documentation files (the "Software"), to deal in
10   * the Software without restriction, including without limitation the rights to
11   * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12   * of the Software, and to permit persons to whom the Software is furnished to do
13   * so, subject to the following conditions:
14   *
15   * The above copyright notice and this permission notice shall be included in all
16   * copies or substantial portions of the Software.
17   *
18   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24   * SOFTWARE.
25   */
26  
27  import java.io.File;
28  import java.util.Collections;
29  import java.util.HashSet;
30  import java.util.LinkedHashMap;
31  import java.util.LinkedList;
32  import java.util.List;
33  import java.util.Map;
34  import java.util.Set;
35  
36  /**
37   * @author jdcasey
38   */
39  public class CompilerConfiguration
40  {
41      private String outputLocation;
42  
43      private List<String> classpathEntries = new LinkedList<String>();
44  
45      private List<String> modulepathEntries = new LinkedList<String>();
46  
47      // ----------------------------------------------------------------------
48      // Source Files
49      // ----------------------------------------------------------------------
50  
51      private Set<File> sourceFiles = new HashSet<File>();
52  
53      private List<String> sourceLocations = new LinkedList<String>();
54  
55      private Set<String> includes = new HashSet<String>();
56  
57      private Set<String> excludes = new HashSet<String>();
58  
59      // ----------------------------------------------------------------------
60      // Compiler Settings
61      // ----------------------------------------------------------------------
62  
63      private boolean debug;
64  
65      private String debugLevel;
66  
67      private boolean showWarnings = true;
68      
69      /**
70       * -Werror argument as supported since Java 1.7
71       */
72      private boolean failOnWarning;
73  
74      private boolean showDeprecation;
75  
76      private String sourceVersion;
77  
78      private String targetVersion;
79      
80      /**
81       * value of -release parameter in java 9+
82       */
83      private String releaseVersion;
84  
85      private String sourceEncoding;
86  
87      private Map<String, String> customCompilerArguments = new LinkedHashMap<String, String>();
88  
89      private boolean fork;
90  
91      private boolean optimize;
92  
93      private String meminitial;
94  
95      private String maxmem;
96  
97      private String executable;
98  
99      private File workingDirectory;
100 
101     private String compilerVersion;
102 
103     private boolean verbose = false;
104 
105     /**
106      * A build temporary directory, eg target/.
107      * <p/>
108      * Used by the compiler implementation to put temporary files.
109      */
110     private File buildDirectory;
111 
112     /**
113      * Used to control the name of the output file when compiling a set of
114      * sources to a single file.
115      */
116     private String outputFileName;
117 
118     /**
119      * in jdk 1.6+, used to hold value of the -s path parameter.
120      */
121     private File generatedSourcesDirectory;
122 
123     /**
124      * value of the -proc parameter in jdk 1.6+
125      */
126     private String proc;
127 
128     /**
129      * -processor parameters in jdk 1.6+
130      */
131     private String[] annotationProcessors;
132 
133     /**
134      * -processorpath parameter in jdk 1.6+. If specified, annotation processors are only searched in the processor
135      * path. Otherwise they are searched in the classpath.
136      */
137     private List<String> processorPathEntries;
138 
139     /**
140      * default value {@link CompilerReuseStrategy.ReuseCreated}
141      *
142      * @since 1.9
143      */
144     private CompilerReuseStrategy compilerReuseStrategy = CompilerReuseStrategy.ReuseCreated;
145 
146     /**
147      * force usage of old JavacCompiler even if javax.tools is detected
148      * @since 2.0
149      */
150     private boolean forceJavacCompilerUse=false;
151 
152     // ----------------------------------------------------------------------
153     //
154     // ----------------------------------------------------------------------
155 
156     public void setOutputLocation( String outputLocation )
157     {
158         this.outputLocation = outputLocation;
159     }
160 
161     public String getOutputLocation()
162     {
163         return outputLocation;
164     }
165 
166     // ----------------------------------------------------------------------
167     // Class path
168     // ----------------------------------------------------------------------
169 
170     public void addClasspathEntry( String classpathEntry )
171     {
172         classpathEntries.add( classpathEntry );
173     }
174 
175     public void setClasspathEntries( List<String> classpathEntries )
176     {
177         if ( classpathEntries == null )
178         {
179             this.classpathEntries = Collections.emptyList();
180         }
181         else
182         {
183             this.classpathEntries = new LinkedList<String>( classpathEntries );
184         }
185     }
186 
187     public List<String> getClasspathEntries()
188     {
189         return Collections.unmodifiableList( classpathEntries );
190     }
191 
192     // ----------------------------------------------------------------------
193     // Module path
194     // ----------------------------------------------------------------------
195 
196     public void addModulepathEntry( String modulepathEntry )
197     {
198         modulepathEntries.add( modulepathEntry );
199     }
200 
201     public void setModulepathEntries( List<String> modulepathEntries )
202     {
203         if ( modulepathEntries == null )
204         {
205             this.modulepathEntries = Collections.emptyList();
206         }
207         else
208         {
209             this.modulepathEntries = new LinkedList<String>( modulepathEntries );
210         }
211     }
212 
213     public List<String> getModulepathEntries()
214     {
215         return Collections.unmodifiableList( modulepathEntries );
216     }
217 
218     // ----------------------------------------------------------------------
219     // Source files
220     // ----------------------------------------------------------------------
221 
222     public void setSourceFiles( Set<File> sourceFiles )
223     {
224         if ( sourceFiles == null )
225         {
226             this.sourceFiles = Collections.emptySet();
227         }
228         else
229         {
230             this.sourceFiles = new HashSet<File>( sourceFiles );
231         }
232     }
233 
234     public Set<File> getSourceFiles()
235     {
236         return sourceFiles;
237     }
238 
239     public void addSourceLocation( String sourceLocation )
240     {
241         sourceLocations.add( sourceLocation );
242     }
243 
244     public void setSourceLocations( List<String> sourceLocations )
245     {
246         if ( sourceLocations == null )
247         {
248             this.sourceLocations = Collections.emptyList();
249         }
250         else
251         {
252             this.sourceLocations = new LinkedList<String>( sourceLocations );
253         }
254     }
255 
256     public List<String> getSourceLocations()
257     {
258         return Collections.unmodifiableList( sourceLocations );
259     }
260 
261     public void addInclude( String include )
262     {
263         includes.add( include );
264     }
265 
266     public void setIncludes( Set<String> includes )
267     {
268         if ( includes == null )
269         {
270             this.includes = Collections.emptySet();
271         }
272         else
273         {
274             this.includes = new HashSet<String>( includes );
275         }
276     }
277 
278     public Set<String> getIncludes()
279     {
280         return Collections.unmodifiableSet( includes );
281     }
282 
283     public void addExclude( String exclude )
284     {
285         excludes.add( exclude );
286     }
287 
288     public void setExcludes( Set<String> excludes )
289     {
290         if ( excludes == null )
291         {
292             this.excludes = Collections.emptySet();
293         }
294         else
295         {
296             this.excludes = new HashSet<String>( excludes );
297         }
298     }
299 
300     public Set<String> getExcludes()
301     {
302         return Collections.unmodifiableSet( excludes );
303     }
304 
305     // ----------------------------------------------------------------------
306     // Compiler Settings
307     // ----------------------------------------------------------------------
308 
309     public void setDebug( boolean debug )
310     {
311         this.debug = debug;
312     }
313 
314     public boolean isDebug()
315     {
316         return debug;
317     }
318 
319     public void setDebugLevel( String debugLevel )
320     {
321         this.debugLevel = debugLevel;
322     }
323 
324     public String getDebugLevel()
325     {
326         return debugLevel;
327     }
328 
329     public void setShowWarnings( boolean showWarnings )
330     {
331         this.showWarnings = showWarnings;
332     }
333 
334     public boolean isShowWarnings()
335     {
336         return showWarnings;
337     }
338 
339     public boolean isShowDeprecation()
340     {
341         return showDeprecation;
342     }
343 
344     public void setShowDeprecation( boolean showDeprecation )
345     {
346         this.showDeprecation = showDeprecation;
347     }
348     
349     public boolean isFailOnWarning()
350     {
351         return failOnWarning;
352     }
353     
354     public void setFailOnWarning( boolean failOnWarnings )
355     {
356         this.failOnWarning = failOnWarnings;
357     }
358 
359     public String getSourceVersion()
360     {
361         return sourceVersion;
362     }
363 
364     public void setSourceVersion( String sourceVersion )
365     {
366         this.sourceVersion = sourceVersion;
367     }
368 
369     public String getTargetVersion()
370     {
371         return targetVersion;
372     }
373 
374     public void setTargetVersion( String targetVersion )
375     {
376         this.targetVersion = targetVersion;
377     }
378     
379     public String getReleaseVersion()
380     {
381         return releaseVersion;
382     }
383 
384     public void setReleaseVersion( String releaseVersion )
385     {
386         this.releaseVersion = releaseVersion;
387     }
388 
389     public String getSourceEncoding()
390     {
391         return sourceEncoding;
392     }
393 
394     public void setSourceEncoding( String sourceEncoding )
395     {
396         this.sourceEncoding = sourceEncoding;
397     }
398 
399     public void addCompilerCustomArgument( String customArgument, String value )
400     {
401         customCompilerArguments.put( customArgument, value );
402     }
403 
404     /**
405      * @deprecated will be removed in 2.X use #getCustomCompilerArgumentsAsMap
406      * @return
407      */
408     @Deprecated
409     public LinkedHashMap<String, String> getCustomCompilerArguments()
410     {
411         return new LinkedHashMap<String, String>( customCompilerArguments );
412     }
413 
414     /**
415      * @deprecated will be removed in 2.X use #setCustomCompilerArgumentsAsMap
416      * @param customCompilerArguments
417      */
418     @Deprecated
419     public void setCustomCompilerArguments( LinkedHashMap<String, String> customCompilerArguments )
420     {
421         if ( customCompilerArguments == null )
422         {
423             this.customCompilerArguments = new LinkedHashMap<String, String>();
424         }
425         else
426         {
427             this.customCompilerArguments = customCompilerArguments;
428         }
429     }
430 
431     public Map<String, String> getCustomCompilerArgumentsAsMap()
432     {
433         return new LinkedHashMap<String, String>( customCompilerArguments );
434     }
435 
436     public void setCustomCompilerArgumentsAsMap( Map<String, String> customCompilerArguments )
437     {
438         if ( customCompilerArguments == null )
439         {
440             this.customCompilerArguments = new LinkedHashMap<String, String>();
441         }
442         else
443         {
444             this.customCompilerArguments = customCompilerArguments;
445         }
446     }
447 
448     public boolean isFork()
449     {
450         return fork;
451     }
452 
453     public void setFork( boolean fork )
454     {
455         this.fork = fork;
456     }
457 
458     public String getMeminitial()
459     {
460         return meminitial;
461     }
462 
463     public void setMeminitial( String meminitial )
464     {
465         this.meminitial = meminitial;
466     }
467 
468     public String getMaxmem()
469     {
470         return maxmem;
471     }
472 
473     public void setMaxmem( String maxmem )
474     {
475         this.maxmem = maxmem;
476     }
477 
478     public String getExecutable()
479     {
480         return executable;
481     }
482 
483     public void setExecutable( String executable )
484     {
485         this.executable = executable;
486     }
487 
488     public File getWorkingDirectory()
489     {
490         return workingDirectory;
491     }
492 
493     public void setWorkingDirectory( File workingDirectory )
494     {
495         this.workingDirectory = workingDirectory;
496     }
497 
498     public File getBuildDirectory()
499     {
500         return buildDirectory;
501     }
502 
503     public void setBuildDirectory( File buildDirectory )
504     {
505         this.buildDirectory = buildDirectory;
506     }
507 
508     public String getOutputFileName()
509     {
510         return outputFileName;
511     }
512 
513     public void setOutputFileName( String outputFileName )
514     {
515         this.outputFileName = outputFileName;
516     }
517 
518     public boolean isOptimize()
519     {
520         return optimize;
521     }
522 
523     public void setOptimize( boolean optimize )
524     {
525         this.optimize = optimize;
526     }
527 
528     public String getCompilerVersion()
529     {
530         return compilerVersion;
531     }
532 
533     public void setCompilerVersion( String compilerVersion )
534     {
535         this.compilerVersion = compilerVersion;
536     }
537 
538     public boolean isVerbose()
539     {
540         return verbose;
541     }
542 
543     public void setVerbose( boolean verbose )
544     {
545         this.verbose = verbose;
546     }
547 
548     public void setProc( String proc )
549     {
550         this.proc = proc;
551     }
552 
553     public void setGeneratedSourcesDirectory( File generatedSourcesDirectory )
554     {
555         this.generatedSourcesDirectory = generatedSourcesDirectory;
556     }
557 
558     public File getGeneratedSourcesDirectory()
559     {
560         return generatedSourcesDirectory;
561     }
562 
563     public String getProc()
564     {
565         return proc;
566     }
567 
568     public void setAnnotationProcessors( String[] annotationProcessors )
569     {
570         this.annotationProcessors = annotationProcessors;
571     }
572 
573     public String[] getAnnotationProcessors()
574     {
575         return annotationProcessors;
576     }
577 
578     /**
579      * -processorpath parameter in jdk 1.6+. If specified, annotation processors are only searched in the processor
580      * path. Otherwise they are searched in the classpath.
581      *
582      * @param entry processor path entry to add
583      */
584     public void addProcessorPathEntry(String entry) {
585         if ( processorPathEntries == null ) {
586             processorPathEntries = new LinkedList<String>();
587         }
588 
589         processorPathEntries.add( entry );
590     }
591 
592     /**
593      * -processorpath parameter in jdk 1.6+. If specified, annotation processors are only searched in the processor
594      * path. Otherwise they are searched in the classpath.
595      *
596      * @return the processorPathEntries
597      */
598     public List<String> getProcessorPathEntries() {
599         return processorPathEntries;
600     }
601 
602     /**
603      * -processorpath parameter in jdk 1.6+. If specified, annotation processors are only searched in the processor
604      * path. Otherwise they are searched in the classpath.
605      *
606      * @param processorPathEntries the processorPathEntries to set
607      */
608     public void setProcessorPathEntries(List<String> processorPathEntries) {
609         this.processorPathEntries = processorPathEntries;
610     }
611 
612     public CompilerReuseStrategy getCompilerReuseStrategy()
613     {
614         return compilerReuseStrategy;
615     }
616 
617     public void setCompilerReuseStrategy( CompilerReuseStrategy compilerReuseStrategy )
618     {
619         this.compilerReuseStrategy = compilerReuseStrategy;
620     }
621 
622     /**
623      * Re-use strategy of the compiler (implement for java only).
624      */
625     public enum CompilerReuseStrategy
626     {
627         /**
628          * Always reuse the same.
629          * <b>Default strategy.</b>
630          */
631         ReuseSame( "reuseSame" ),
632         /**
633          * Re-create a new compiler for each use.
634          */
635         AlwaysNew( "alwaysNew" ),
636         /**
637          * Re-use already created compiler, create new one if non already exists.
638          * <b>Will mimic a kind of pool to prevent different threads use the same.</b>
639          */
640         ReuseCreated( "reuseCreated" );
641 
642         private String strategy;
643 
644         CompilerReuseStrategy( String strategy )
645         {
646             this.strategy = strategy;
647         }
648 
649         public String getStrategy()
650         {
651             return strategy;
652         }
653 
654         @Override
655         public String toString()
656         {
657             return "CompilerReuseStrategy:" + this.strategy;
658         }
659     }
660 
661     public boolean isForceJavacCompilerUse()
662     {
663         return forceJavacCompilerUse;
664     }
665 
666     public void setForceJavacCompilerUse( boolean forceJavacCompilerUse )
667     {
668         this.forceJavacCompilerUse = forceJavacCompilerUse;
669     }
670 }