1 package hudson.maven.reporters; 2 3 import hudson.maven.MavenBuild; 4 import hudson.maven.MavenBuildProxy; 5 import hudson.maven.MavenBuildProxy.BuildCallable; 6 import hudson.maven.MavenModule; 7 import hudson.maven.MavenReporter; 8 import hudson.maven.MavenReporterDescriptor; 9 import hudson.maven.MojoInfo; 10 import hudson.model.Action; 11 import hudson.model.BuildListener; 12 import hudson.model.Result; 13 import hudson.tasks.junit.TestResult; 14 import hudson.tasks.junit.TestResultAction; 15 import hudson.tasks.test.TestResultProjectAction; 16 import org.apache.maven.project.MavenProject; 17 import org.apache.tools.ant.DirectoryScanner; 18 import org.apache.tools.ant.Project; 19 import org.apache.tools.ant.types.FileSet; 20 import org.codehaus.plexus.component.configurator.ComponentConfigurationException; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.util.Date ; 25 26 30 public class SurefireArchiver extends MavenReporter { 31 private transient Date started; 32 33 public boolean preExecute(MavenBuildProxy build, MavenProject pom, MojoInfo mojo, BuildListener listener) throws InterruptedException , IOException { 34 if (isSurefireTest(mojo)) 35 started = new Date (); 36 return true; 37 } 38 39 public boolean postExecute(MavenBuildProxy build, MavenProject pom, MojoInfo mojo, final BuildListener listener) throws InterruptedException , IOException { 40 if (!isSurefireTest(mojo)) return true; 41 42 File reportsDir; 43 try { 44 reportsDir = mojo.getConfigurationValue("reportsDirectory", File .class); 45 } catch (ComponentConfigurationException e) { 46 e.printStackTrace(listener.fatalError("Unable to obtain the reportsDirectory from surefire:test mojo")); 47 build.setResult(Result.FAILURE); 48 return true; 49 } 50 51 if(reportsDir.exists()) { 52 54 FileSet fs = new FileSet(); 55 Project p = new Project(); 56 fs.setProject(p); 57 fs.setDir(reportsDir); 58 fs.setIncludes("*.xml"); 59 DirectoryScanner ds = fs.getDirectoryScanner(p); 60 61 if(ds.getIncludedFiles().length==0) 62 return true; 64 65 final TestResult tr = new TestResult(started.getTime() - 1000, ds); 66 67 build.execute(new BuildCallable<Void , IOException >() { 68 public Void call(MavenBuild build) throws IOException , InterruptedException { 69 TestResultAction action = new TestResultAction(build, tr, listener); 70 build.getActions().add(action); 71 if(tr.getFailCount()>0) 72 build.setResult(Result.UNSTABLE); 73 build.registerAsProjectAction(SurefireArchiver.this); 74 return null; 75 } 76 }); 77 } 78 79 return true; 80 } 81 82 83 public Action getProjectAction(MavenModule module) { 84 return new TestResultProjectAction(module); 85 } 86 87 private boolean isSurefireTest(MojoInfo mojo) { 88 return mojo.pluginName.matches("org.apache.maven.plugins", "maven-surefire-plugin") && mojo.getGoal().equals("test"); 89 } 90 91 public DescriptorImpl getDescriptor() { 92 return DescriptorImpl.DESCRIPTOR; 93 } 94 95 public static final class DescriptorImpl extends MavenReporterDescriptor { 96 public static final DescriptorImpl DESCRIPTOR = new DescriptorImpl(); 97 98 private DescriptorImpl() { 99 super(SurefireArchiver.class); 100 } 101 102 public String getDisplayName() { 103 return "Publish surefire reports"; 104 } 105 106 public SurefireArchiver newAutoInstance(MavenModule module) { 107 return new SurefireArchiver(); 108 } 109 } 110 111 private static final long serialVersionUID = 1L; 112 } 113 | Popular Tags |