View Javadoc
1   package org.spf4j.jdiff;
2   
3   import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
4   import java.io.File;
5   import java.io.IOException;
6   import java.util.ArrayList;
7   import java.util.Collections;
8   import java.util.List;
9   import java.util.Properties;
10  
11  import org.apache.commons.lang.SystemUtils;
12  import org.apache.maven.execution.MavenSession;
13  import org.apache.maven.plugin.AbstractMojo;
14  import org.apache.maven.plugin.MojoExecution;
15  import org.apache.maven.plugins.annotations.Component;
16  import org.apache.maven.plugins.annotations.Parameter;
17  import org.apache.maven.project.MavenProject;
18  import org.apache.maven.toolchain.Toolchain;
19  import org.apache.maven.toolchain.ToolchainManager;
20  import org.codehaus.plexus.util.StringUtils;
21  import org.codehaus.plexus.util.cli.CommandLineUtils;
22  import org.eclipse.aether.RepositorySystem;
23  import org.eclipse.aether.repository.RemoteRepository;
24  
25  @SuppressFBWarnings("AI_ANNOTATION_ISSUES_NEEDS_NULLABLE")
26  public abstract class BaseJDiffMojo
27          extends AbstractMojo {
28  
29    /**
30     * The working directory for this plugin.
31     */
32    @Parameter(defaultValue = "${project.build.directory}/jdiff", readonly = true)
33    private File workingDirectory;
34  
35    /**
36     * The javadoc executable.
37     */
38    @Parameter(property = "javadocExecutable")
39    private String javadocExecutable;
40  
41    /**
42     * List of packages.
43     */
44    @Parameter(property = "includePackageNames")
45    private ArrayList<String> includePackageNames = new ArrayList<>(2);
46  
47    @Component
48    private ToolchainManager toolchainManager;
49  
50    /**
51     * The current build mavenSession instance.
52     */
53    @Parameter(defaultValue = "${session}", required = true, readonly = true)
54    private MavenSession mavenSession;
55  
56    @Parameter(defaultValue = "${project}", required = true, readonly = true)
57    private MavenProject mavenProject;
58  
59    @Parameter(defaultValue = "${mojoExecution}", required = true, readonly = true)
60    private MojoExecution mojoExecution;
61  
62    /**
63     * The entry point to Aether, i.e. the component doing all the work.
64     *
65     * @component
66     */
67    @Component
68    private RepositorySystem repoSystem;
69  
70    final MavenSession getMavenSession() {
71      return mavenSession;
72    }
73  
74  
75    /**
76     * Get the path of the Javadoc tool executable depending the user entry or try to find it depending the OS or the
77     * <code>java.home</code> system property or the <code>JAVA_HOME</code> environment variable.
78     *
79     * @return the path of the Javadoc tool
80     * @throws IOException if not found
81     */
82    final String getJavadocExecutable()
83            throws IOException {
84      Toolchain tc = getToolchain();
85  
86      if (tc != null) {
87        getLog().info("Toolchain in jdiff-maven-plugin: " + tc);
88        if (javadocExecutable != null) {
89          getLog().warn("Toolchains are ignored, 'javadocExecutable' parameter is set to " + javadocExecutable);
90        } else {
91          javadocExecutable = tc.findTool("javadoc");
92        }
93      }
94  
95      String javadocCommand = "javadoc" + (SystemUtils.IS_OS_WINDOWS ? ".exe" : "");
96  
97      File javadocExe;
98  
99      // ----------------------------------------------------------------------
100     // The javadoc executable is defined by the user
101     // ----------------------------------------------------------------------
102     if (StringUtils.isNotEmpty(javadocExecutable)) {
103       javadocExe = new File(javadocExecutable);
104 
105       if (javadocExe.isDirectory()) {
106         javadocExe = new File(javadocExe, javadocCommand);
107       }
108 
109       if (SystemUtils.IS_OS_WINDOWS && javadocExe.getName().indexOf('.') < 0) {
110         javadocExe = new File(javadocExe.getPath() + ".exe");
111       }
112 
113       if (!javadocExe.isFile()) {
114         throw new IOException("The javadoc executable '" + javadocExe
115                 + "' doesn't exist or is not a file. Verify the <javadocExecutable/> parameter.");
116       }
117 
118       return javadocExe.getAbsolutePath();
119     }
120     File javaHome = SystemUtils.getJavaHome();
121     // ----------------------------------------------------------------------
122     // Try to find javadocExe from System.getProperty( "java.home" )
123     // By default, System.getProperty( "java.home" ) = JRE_HOME and JRE_HOME
124     // should be in the JDK_HOME
125     // ----------------------------------------------------------------------
126     // For IBM's JDK 1.2
127     if (SystemUtils.IS_OS_AIX) {
128       javadocExe
129               = new File(javaHome + File.separator + ".." + File.separator + "sh", javadocCommand);
130     } else if (SystemUtils.IS_OS_MAC_OSX) {
131       javadocExe = new File(javaHome + File.separator + "bin", javadocCommand);
132     } else {
133       javadocExe
134               = new File(javaHome + File.separator + ".." + File.separator + "bin", javadocCommand);
135     }
136 
137     // ----------------------------------------------------------------------
138     // Try to find javadocExe from JAVA_HOME environment variable
139     // ----------------------------------------------------------------------
140     if (!javadocExe.exists() || !javadocExe.isFile()) {
141       Properties env = CommandLineUtils.getSystemEnvVars();
142       String javaHomeStr = env.getProperty("JAVA_HOME");
143       if (StringUtils.isEmpty(javaHomeStr)) {
144         throw new IOException("The environment variable JAVA_HOME is not correctly set: javahome='"
145                 + javaHomeStr + '\'');
146       }
147       File javaHomeFile = new File(javaHomeStr);
148       if ((!javaHomeFile.exists()) || (!javaHomeFile.isDirectory())) {
149         throw new IOException("The environment variable JAVA_HOME=" + javaHome
150                 + " doesn't exist or is not a valid directory.");
151       }
152 
153       javadocExe = new File(javaHomeStr + File.separator + "bin", javadocCommand);
154     }
155 
156     if (!javadocExe.exists() || !javadocExe.isFile()) {
157       throw new IOException("The javadoc executable '" + javadocExe
158               + "' doesn't exist or is not a file. Verify the JAVA_HOME environment variable.");
159     }
160 
161     return javadocExe.getAbsolutePath();
162   }
163 
164   private Toolchain getToolchain() {
165     Toolchain tc = null;
166     if (toolchainManager != null) {
167       tc = toolchainManager.getToolchainFromBuildContext("jdk", mavenSession);
168     }
169 
170     return tc;
171   }
172 
173   @SuppressFBWarnings("IMPROPER_UNICODE")
174   final List<String> getCompileSourceRoots() {
175     if ("pom".equalsIgnoreCase(mavenProject.getPackaging())) {
176       return Collections.emptyList();
177     } else {
178       List<String> compileSourceRoots = mavenProject.getCompileSourceRoots();
179       return compileSourceRoots == null ? Collections.EMPTY_LIST : compileSourceRoots;
180     }
181   }
182 
183   final MavenProject getMavenProject() {
184     return mavenProject;
185   }
186 
187   final File getWorkingDirectory() {
188     return workingDirectory;
189   }
190 
191   final MojoExecution getMojoExecution() {
192     return mojoExecution;
193   }
194 
195   final List<RemoteRepository> getProjectRepos() {
196     return mavenProject.getRemoteProjectRepositories();
197   }
198 
199   final RepositorySystem getRepoSystem() {
200     return repoSystem;
201   }
202 
203   final ArrayList<String> getIncludePackageNames() {
204     return includePackageNames;
205   }
206 
207   final ToolchainManager getToolchainManager() {
208     return toolchainManager;
209   }
210 
211 
212   /**
213    * Overwite this class for proper toString.
214    * @return
215    */
216   @Override
217   public String toString() {
218     return "BaseJDiffMojo{" + "workingDirectory=" + workingDirectory + ", javadocExecutable="
219             + javadocExecutable + ", includePackageNames=" + includePackageNames + '}';
220   }
221 
222 }