View Javadoc
1   /*
2    * To change this license header, choose License Headers in Project Properties.
3    * To change this template file, choose Tools | Templates
4    * and open the template in the editor.
5    */
6   package ch.albasim.html.report;
7   
8   import java.io.IOException;
9   import java.nio.file.FileVisitResult;
10  import java.nio.file.Files;
11  import java.nio.file.Path;
12  import java.nio.file.Paths;
13  import java.nio.file.SimpleFileVisitor;
14  import java.nio.file.StandardCopyOption;
15  import java.nio.file.attribute.BasicFileAttributes;
16  import java.util.Locale;
17  
18  import org.apache.maven.doxia.sink.Sink;
19  import org.apache.maven.plugin.logging.Log;
20  import org.apache.maven.plugins.annotations.LifecyclePhase;
21  import org.apache.maven.plugins.annotations.Mojo;
22  import org.apache.maven.plugins.annotations.Parameter;
23  import org.apache.maven.plugins.annotations.ResolutionScope;
24  import org.apache.maven.project.MavenProject;
25  import org.apache.maven.reporting.AbstractMavenReport;
26  import org.apache.maven.reporting.MavenReportException;
27  
28  /**
29   * TODO:publish to maven central
30   *
31   * @author maxence
32   */
33  @Mojo(
34      name = "html-report",
35      defaultPhase = LifecyclePhase.SITE,
36      requiresDependencyResolution = ResolutionScope.RUNTIME,
37      requiresProject = true,
38      threadSafe = true
39  )
40  public class HtmlReport extends AbstractMavenReport {
41  
42      /**
43       * Practical reference to the Maven project
44       */
45      @Parameter(defaultValue = "${project}", readonly = true, required = true)
46      private MavenProject project;
47  
48      @Parameter(defaultValue = "${name}", readonly = true, required = true)
49      private String name;
50  
51      @Parameter(defaultValue = "${description}", readonly = true, required = true)
52      private String description;
53  
54      @Parameter(defaultValue = "${htmlReportDirectory}", readonly = true, required = true)
55      private String htmlReportDirectory;
56  
57      @Parameter(defaultValue = "${index}", readonly = true, required = true)
58      private String index;
59  
60      @Override
61      public String getOutputName() {
62          return name;
63      }
64  
65      @Override
66      public String getName(Locale locale) {
67          return name;
68      }
69  
70      @Override
71      public String getDescription(Locale locale) {
72          return description;
73      }
74  
75      private void deleteFolder(Path path) throws IOException {
76          if (Files.exists(path)) {
77              Files.walkFileTree(path,
78                  new SimpleFileVisitor<Path>() {
79                  @Override
80                  public FileVisitResult postVisitDirectory(
81                      Path dir, IOException exc) throws IOException {
82                      Files.delete(dir);
83                      return FileVisitResult.CONTINUE;
84                  }
85  
86                  @Override
87                  public FileVisitResult visitFile(
88                      Path file, BasicFileAttributes attrs)
89                      throws IOException {
90                      Files.delete(file);
91                      return FileVisitResult.CONTINUE;
92                  }
93              });
94          }
95      }
96  
97      private void copyR(Path source, Path dest) throws IOException {
98          if (Files.isDirectory(source)) {
99              Files.walk(source).forEach(path -> {
100                 Path destination = Paths.get(dest.toString(), path.toString()
101                     .substring(source.toString().length()));
102                 try {
103                     Files.copy(path, destination, StandardCopyOption.REPLACE_EXISTING);
104                 } catch (IOException e) {
105                     e.printStackTrace();
106                 }
107             });
108         }
109     }
110 
111     private void copyReport() throws IOException {
112         Path reportPath = Paths.get(this.htmlReportDirectory);
113         Path outputPath = Paths.get(this.outputDirectory.getPath() + "/" + getOutputName());
114 
115         getLog().info("Copy report from " + reportPath + " to " + outputPath);
116         deleteFolder(outputPath);
117         this.copyR(reportPath, outputPath);
118     }
119 
120     private String getIndexLocation() {
121         return name + "/" + index;
122     }
123 
124     @Override
125     protected void executeReport(Locale locale) throws MavenReportException {
126 
127         // Get the logger
128         Log logger = getLog();
129 
130         // Some info
131         logger.info("Generating " + getOutputName() + ".html"
132             + " for " + project.getName() + " " + project.getVersion());
133 
134         try {
135             copyReport();
136 
137             Sink mainSink = getSink();
138             if (mainSink == null) {
139                 throw new MavenReportException("Could not get the Doxia sink");
140             }
141 
142             // Page title
143             mainSink.head();
144             mainSink.title();
145             mainSink.text(this.getName(locale) + " Report for " + project.getName() + " " + project.getVersion());
146             mainSink.title_();
147             mainSink.head_();
148 
149             mainSink.body();
150 
151             // Heading 1
152             mainSink.section1();
153             mainSink.sectionTitle1();
154             mainSink.text(this.getName(locale) + " Report for " + project.getName() + " " + project.getVersion());
155             mainSink.sectionTitle1_();
156 
157             // Content
158             mainSink.paragraph();
159             mainSink.link(getIndexLocation());
160             mainSink.text("report");
161             mainSink.link("");
162             mainSink.paragraph_();
163 
164             // Close
165             mainSink.section1_();
166             mainSink.body_();
167 
168         } catch (IOException ex) {
169             logger.error("Failed to copy report !", ex);
170         }
171     }
172 
173 }