View Javadoc
1   package org.codehaus.modello.maven;
2   
3   /*
4    * Copyright (c) 2004, Codehaus.org
5    *
6    * Permission is hereby granted, free of charge, to any person obtaining a copy of
7    * this software and associated documentation files (the "Software"), to deal in
8    * the Software without restriction, including without limitation the rights to
9    * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10   * of the Software, and to permit persons to whom the Software is furnished to do
11   * so, subject to the following conditions:
12   *
13   * The above copyright notice and this permission notice shall be included in all
14   * copies or substantial portions of the Software.
15   *
16   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22   * SOFTWARE.
23   */
24  
25  import java.io.File;
26  import java.util.Optional;
27  import java.util.Properties;
28  import java.util.function.Supplier;
29  import java.util.stream.Stream;
30  
31  import org.apache.maven.plugins.annotations.Parameter;
32  import org.codehaus.modello.ModelloParameterConstants;
33  
34  /**
35   * @author Hervé Boutemy
36   */
37  public abstract class AbstractModelloSourceGeneratorMojo extends AbstractModelloGeneratorMojo {
38      /**
39       * The output directory of the generated Java beans.
40       */
41      @Parameter(defaultValue = "${project.build.directory}/generated-sources/modello", required = true)
42      private File outputDirectory;
43  
44      /**
45       * The encoding to use when generating Java source files.
46       *
47       * @since 1.0-alpha-19
48       */
49      @Parameter(defaultValue = "${project.build.sourceEncoding}")
50      private String encoding;
51  
52      /**
53       * The java source level used for generating outputs classes.
54       * <p/>
55       * Will be discovered from project properties, in order:
56       * <ul>
57       *     <li><code>maven.compiler.release</code></li>
58       *     <li><code>maven.compiler.source</code></li>
59       *     <li><code>maven.compiler.target</code></li>
60       * </ul>
61       *
62       * If all of above properties was not be set, default value as <b>8</b> will be used.
63       *
64       * @since 1.0
65       */
66      @Parameter
67      private String javaSource;
68  
69      /**
70       * Generate DOM content as plexus-utils <code>Xpp3Dom</code> objects instead of <code>org.w3c.dom.Element</code>.
71       * @since 1.6
72       */
73      @Parameter(defaultValue = "true")
74      private boolean domAsXpp3;
75  
76      @Override
77      protected boolean producesCompilableResult() {
78          return true;
79      }
80  
81      public File getOutputDirectory() {
82          return outputDirectory;
83      }
84  
85      public void setOutputDirectory(File outputDirectory) {
86          this.outputDirectory = outputDirectory;
87      }
88  
89      @Override
90      protected void customizeParameters(Properties parameters) {
91          super.customizeParameters(parameters);
92  
93          if (encoding != null) {
94              parameters.setProperty(ModelloParameterConstants.ENCODING, encoding);
95          }
96  
97          if (javaSource == null) {
98              javaSource = discoverJavaSource();
99          }
100         if (javaSource.startsWith("1.")) {
101             javaSource = javaSource.substring("1.".length());
102         }
103         getLog().debug("javaSource=" + javaSource);
104         parameters.setProperty(ModelloParameterConstants.OUTPUT_JAVA_SOURCE, javaSource);
105 
106         parameters.setProperty(ModelloParameterConstants.DOM_AS_XPP3, Boolean.toString(domAsXpp3));
107     }
108 
109     private String discoverJavaSource() {
110         Properties projectProperties = getProject().getProperties();
111 
112         Supplier<String> release = () -> projectProperties.getProperty("maven.compiler.release");
113         Supplier<String> source = () -> projectProperties.getProperty("maven.compiler.source");
114         Supplier<String> target = () -> projectProperties.getProperty("maven.compiler.target");
115 
116         Optional<String> jSource = Stream.of(release, source, target)
117                 .map(Supplier::get)
118                 .filter(s -> s != null && !s.isEmpty())
119                 .findFirst();
120 
121         if (jSource.isPresent()) {
122             return jSource.get();
123         } else {
124             getLog().warn("javaSource was not discovered - use default value "
125                     + ModelloParameterConstants.OUTPUT_JAVA_SOURCE_DEFAULT);
126             return ModelloParameterConstants.OUTPUT_JAVA_SOURCE_DEFAULT;
127         }
128     }
129 }