View Javadoc
1   /*
2    * #%L
3    * wcm.io
4    * %%
5    * Copyright (C) 2015 wcm.io
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   *
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package io.wcm.caravan.maven.plugins.haldocs;
21  
22  import java.io.File;
23  import java.net.MalformedURLException;
24  import java.net.URL;
25  
26  import org.apache.maven.artifact.DependencyResolutionRequiredException;
27  import org.apache.maven.model.Build;
28  import org.apache.maven.model.Resource;
29  import org.apache.maven.plugin.AbstractMojo;
30  import org.apache.maven.plugins.annotations.Parameter;
31  import org.apache.maven.project.MavenProject;
32  
33  /**
34   * Common functionality for the MOJOs.
35   */
36  abstract class AbstractBaseMojo extends AbstractMojo {
37  
38    @Parameter(property = "project", required = true, readonly = true)
39    protected MavenProject project;
40  
41    private File generatedResourcesFolder;
42  
43    /**
44     * Get a List of URLs of all "compile" dependencies of this project.
45     * @return Class path URLs
46     * @throws DependencyResolutionRequiredException
47     */
48    protected URL[] getCompileClasspathElementURLs() throws DependencyResolutionRequiredException {
49      // build class loader to get classes to generate resources for
50      return project.getCompileClasspathElements().stream()
51          .map(path -> {
52            try {
53              return new File(path).toURI().toURL();
54            }
55            catch (MalformedURLException ex) {
56              throw new RuntimeException(ex);
57            }
58          })
59          .toArray(size -> new URL[size]);
60    }
61  
62    /**
63     * Attach directory with generated resoruces to project to include it in JAR file.
64     * @param sourceDirectory Source directory
65     * @param targetPath Target path in JAR file
66     */
67    protected void addResource(String sourceDirectory, String targetPath) {
68  
69      // construct resource
70      Resource resource = new Resource();
71      resource.setDirectory(sourceDirectory);
72      resource.setTargetPath(targetPath);
73  
74      // add to build
75      Build build = this.project.getBuild();
76      build.addResource(resource);
77      getLog().debug("Added resource: " + resource.getDirectory() + " -> " + resource.getTargetPath());
78    }
79  
80    /**
81     * Get folder to temporarily generate the resources to.
82     * @return Folder
83     */
84    protected File getGeneratedResourcesDirectory() {
85      if (generatedResourcesFolder == null) {
86        String generatedResourcesFolderAbsolutePath = this.project.getBuild().getDirectory() + "/" + getGeneratedResourcesDirectoryPath();
87        generatedResourcesFolder = new File(generatedResourcesFolderAbsolutePath);
88        if (!generatedResourcesFolder.exists()) {
89          generatedResourcesFolder.mkdirs();
90        }
91      }
92      return generatedResourcesFolder;
93    }
94  
95    /**
96     * @return Path name of directory for generated resources
97     */
98    protected abstract String getGeneratedResourcesDirectoryPath();
99  
100 }