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