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

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Seiji Sogabe
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.FilePath;
27 import hudson.Util;
28 import hudson.Extension;
29 import hudson.maven.MavenBuild;
30 import hudson.maven.MavenBuildProxy;
31 import hudson.maven.MavenModule;
32 import hudson.maven.MavenModuleSet;
33 import hudson.maven.MavenModuleSetBuild;
34 import hudson.maven.MavenReporter;
35 import hudson.maven.MavenReporterDescriptor;
36 import hudson.maven.MojoInfo;
37 import hudson.maven.MavenBuildProxy.BuildCallable;
38 import hudson.model.AbstractItem;
39 import hudson.model.Action;
40 import hudson.model.BuildListener;
41 import hudson.model.DirectoryBrowserSupport;
42 import hudson.model.ProminentProjectAction;
43 import hudson.model.Result;
44 import org.apache.maven.project.MavenProject;
45 import org.codehaus.plexus.component.configurator.ComponentConfigurationException;
46
47 import java.io.File;
48 import java.io.IOException;
49 import java.util.Collection;
50 import java.util.Collections;
51
52 /**
53 * Watches out for the execution of maven-site-plugin and records its output.
54 * Simple projects with one POM will find the site directly beneath {@code site}.
55 * For multi module projects the project whose pom is referenced in the configuration (i.e. the {@link MavenBuild#getParentBuild()} will be recorded to
56 * the {@code site}, module projects' sites will be stored beneath {@code site/${moduleProject.artifactId}}.
57 *
58 * @author Kohsuke Kawaguchi
59 */
60 public class MavenSiteArchiver extends MavenReporter {
61
62 public boolean postExecute(MavenBuildProxy build, MavenProject pom, MojoInfo mojo, BuildListener listener, Throwable error) throws InterruptedException, IOException {
63 if(!mojo.is("org.apache.maven.plugins","maven-site-plugin","site"))
64 return true;
65
66 File destDir;
67 try {
68 destDir = mojo.getConfigurationValue("outputDirectory", File.class);
69 } catch (ComponentConfigurationException e) {
70 e.printStackTrace(listener.fatalError("Unable to find the site output directory"));
71 build.setResult(Result.FAILURE);
72 return true;
73 }
74
75 if(destDir != null && destDir.exists()) {
76 // try to get the storage location if this is a multi-module project.
77 final String moduleName = getModuleName(build, pom);
78 // store at MavenModuleSet level and moduleName
79 final FilePath target = build.getModuleSetRootDir().child("site").child(moduleName);
80 try {
81 listener.getLogger().printf("[JENKINS] Archiving site from %s to %s\n", destDir, target);
82 new FilePath(destDir).copyRecursiveTo("**/*",target);
83 } catch (IOException e) {
84 Util.displayIOException(e,listener);
85 e.printStackTrace(listener.fatalError("Unable to copy site from %s to %s",destDir,target));
86 build.setResult(Result.FAILURE);
87 }
88
89 build.registerAsAggregatedProjectAction(this);
90 }
91
92 return true;
93 }
94
95 /**
96 * In multi module builds pomBaseDir of the parent project is the same as parent build module root.
97 *
98 * @param build
99 * @param pom
100 *
101 * @return the relative path component to copy sites of multi module builds.
102 * @throws IOException
103 * @throws InterruptedException
104 */
105 private String getModuleName(MavenBuildProxy build, MavenProject pom) throws IOException, InterruptedException {
106 String moduleRoot = build.execute(new BuildCallable<String, IOException>() {
107 private static final long serialVersionUID = 1L;
108
109 //@Override
110 public String call(MavenBuild mavenBuild) throws IOException, InterruptedException {
111 MavenModuleSetBuild moduleSetBuild = mavenBuild.getModuleSetBuild();
112 if (moduleSetBuild == null) {
113 throw new IOException("Parent build not found!");
114 }
115 return moduleSetBuild.getModuleRoot().getRemote();
116 }
117 });
118 final File pomBaseDir = pom.getBasedir();
119 final File remoteWorkspaceDir = new File(moduleRoot);
120 if (pomBaseDir.equals(remoteWorkspaceDir)) {
121 return "";
122 } else {
123 return pom.getArtifactId();
124 }
125 }
126
127
128 public Collection<? extends Action> getProjectActions(MavenModule project) {
129 return Collections.singleton(new SiteAction(project));
130 }
131
132 public Action getAggregatedProjectAction(MavenModuleSet project) {
133 return new SiteAction(project);
134 }
135
136 private static File getSiteDir(AbstractItem project) {
137 return new File(project.getRootDir(),"site");
138 }
139
140 public static class SiteAction implements ProminentProjectAction {
141 private final AbstractItem project;
142
143 public SiteAction(AbstractItem project) {
144 this.project = project;
145 }
146
147 public String getUrlName() {
148 return "site";
149 }
150
151 public String getDisplayName() {
152 return Messages.MavenSiteArchiver_DisplayName();
153 }
154
155 public String getIconFileName() {
156 if(getSiteDir(project).exists())
157 return "help.gif";
158 else
159 // hide it since we don't have site yet.
160 return null;
161 }
162
163 /**
164 * Serves the site.
165 */
166 public DirectoryBrowserSupport doDynamic() {
167 return new DirectoryBrowserSupport(this,new FilePath(getSiteDir(project)), project.getDisplayName()+" site", "help.gif", false);
168 }
169 }
170
171 @Extension
172 public static final class DescriptorImpl extends MavenReporterDescriptor {
173 public String getDisplayName() {
174 return "Maven site";
175 }
176
177 public MavenSiteArchiver newAutoInstance(MavenModule module) {
178 return new MavenSiteArchiver();
179 }
180 }
181
182 private static final long serialVersionUID = 1L;
183 }
184