File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\maven-plugin\src\main\java\hudson\maven\reporters\ReportCollector.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all 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
22 * THE SOFTWARE.
23 */
24 package hudson.maven.reporters;
25
26 import hudson.maven.MavenModule;
27 import hudson.maven.MavenReporter;
28 import hudson.maven.MavenReporterDescriptor;
29 import hudson.maven.MavenBuildProxy;
30 import hudson.maven.MojoInfo;
31 import hudson.maven.MavenBuild;
32 import hudson.model.BuildListener;
33 import org.apache.maven.project.MavenProject;
34 import org.apache.maven.reporting.MavenReport;
35
36 import java.io.IOException;
37 import java.io.File;
38 import java.util.Locale;
39
40 /**
41 * Watches out for executions of {@link MavenReport} mojos and record its output.
42 *
43 * @author Kohsuke Kawaguchi
44 */
45 public class ReportCollector extends MavenReporter {
46 private transient ReportAction action;
47
48 public boolean postExecute(MavenBuildProxy build, MavenProject pom, MojoInfo mojo, BuildListener listener, Throwable error) throws InterruptedException, IOException {
49 if(!(mojo.mojo instanceof MavenReport))
50 return true; // not a maven report
51
52 MavenReport report = (MavenReport)mojo.mojo;
53
54 String reportPath = report.getReportOutputDirectory().getPath();
55 String projectReportPath = pom.getReporting().getOutputDirectory();
56 if(!reportPath.startsWith(projectReportPath)) {
57 // report is placed outside site. Can't record it.
58 listener.getLogger().println(Messages.ReportCollector_OutsideSite(reportPath,projectReportPath));
59 return true;
60 }
61
62 if(action==null)
63 action = new ReportAction();
64
65
66 // this is the entry point to the report
67 File top = new File(report.getReportOutputDirectory(),report.getOutputName()+".html");
68 String relPath = top.getPath().substring(projectReportPath.length());
69
70 action.add(new ReportAction.Entry(relPath,report.getName(Locale.getDefault())));
71
72 return true;
73 }
74
75 public boolean leaveModule(MavenBuildProxy build, MavenProject pom, BuildListener listener) throws InterruptedException, IOException {
76 if(action!=null) {
77 // TODO: archive pom.getReporting().getOutputDirectory()
78 build.executeAsync(new AddActionTask(action));
79 }
80 action = null;
81 return super.leaveModule(build, pom, listener);
82 }
83
84 private static final class AddActionTask implements MavenBuildProxy.BuildCallable<Void,IOException> {
85 private final ReportAction action;
86
87 public AddActionTask(ReportAction action) {
88 this.action = action;
89 }
90
91 public Void call(MavenBuild build) throws IOException, InterruptedException {
92 build.addAction(action);
93 return null;
94 }
95
96 private static final long serialVersionUID = 1L;
97 }
98
99 public DescriptorImpl getDescriptor() {
100 return DescriptorImpl.DESCRIPTOR;
101 }
102
103 public static final class DescriptorImpl extends MavenReporterDescriptor {
104 public static final DescriptorImpl DESCRIPTOR = new DescriptorImpl();
105
106 public String getDisplayName() {
107 return Messages.ReportCollector_DisplayName();
108 }
109
110 public ReportCollector newAutoInstance(MavenModule module) {
111 return new ReportCollector();
112 }
113 }
114
115 private static final long serialVersionUID = 1L;
116 }
117