View Javadoc
1   package org.apache.maven.plugins.invoker;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.plugin.AbstractMojo;
23  import org.apache.maven.plugin.MojoExecutionException;
24  import org.apache.maven.plugin.MojoFailureException;
25  import org.apache.maven.plugins.invoker.model.io.xpp3.BuildJobXpp3Reader;
26  import org.apache.maven.plugins.annotations.LifecyclePhase;
27  import org.apache.maven.plugins.annotations.Mojo;
28  import org.apache.maven.plugins.annotations.Parameter;
29  import org.codehaus.plexus.util.ReaderFactory;
30  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
31  
32  import java.io.File;
33  import java.io.IOException;
34  import java.io.Reader;
35  
36  /**
37   * Checks the results of maven-invoker-plugin based integration tests and fails the build if any tests failed.
38   *
39   * @author Olivier Lamy
40   * @since 1.4
41   */
42  @Mojo( name = "verify", defaultPhase = LifecyclePhase.VERIFY, threadSafe = true )
43  public class VerifyMojo
44      extends AbstractMojo
45  {
46  
47      /**
48       * Flag used to suppress certain invocations. This is useful in tailoring the build using profiles.
49       *
50       * @since 1.1
51       */
52      @Parameter( property = "invoker.skip", defaultValue = "false" )
53      private boolean skipInvocation;
54  
55      /**
56       * Base directory where all build reports are read from.
57       *
58       * @since 1.4
59       */
60      @Parameter( property = "invoker.reportsDirectory", defaultValue = "${project.build.directory}/invoker-reports" )
61      private File reportsDirectory;
62  
63      /**
64       * A flag controlling whether failures of the sub builds should fail the main build, too. If set to
65       * <code>true</code>, the main build will proceed even if one or more sub builds failed.
66       *
67       * @since 1.3
68       */
69      @Parameter( property = "maven.test.failure.ignore", defaultValue = "false" )
70      private boolean ignoreFailures;
71  
72      /**
73       * Flag used to suppress the summary output notifying of successes and failures. If set to <code>true</code>, the
74       * only indication of the build's success or failure will be the effect it has on the main build (if it fails, the
75       * main build should fail as well).
76       */
77      @Parameter( defaultValue = "false" )
78      private boolean suppressSummaries;
79  
80      /**
81       * Set this to <code>true</code> to cause a failure if there are no projects to invoke.
82       *
83       * @since 1.9
84       */
85      @Parameter( property = "invoker.failIfNoProjects" )
86      private Boolean failIfNoProjects;
87  
88      /**
89       * Set to <code>true</code> to output build.log to mojo log in case of failed jobs.
90       *
91       * @since 3.2.2
92       */
93      @Parameter( property = "invoker.streamLogsOnFailures", defaultValue = "false" )
94      private boolean streamLogsOnFailures;
95  
96      /**
97       * Invokes Maven on the configured test projects.
98       *
99       * @throws org.apache.maven.plugin.MojoExecutionException If the goal encountered severe errors.
100      * @throws org.apache.maven.plugin.MojoFailureException If any of the Maven builds failed.
101      */
102     public void execute()
103         throws MojoExecutionException, MojoFailureException
104     {
105         if ( skipInvocation )
106         {
107             getLog().info( "Skipping invocation per configuration."
108                                + " If this is incorrect, ensure the skipInvocation parameter is not set to true." );
109             return;
110         }
111 
112         File[] reportFiles = ReportUtils.getReportFiles( reportsDirectory );
113         if ( reportFiles.length <= 0 )
114         {
115             if ( Boolean.TRUE.equals( failIfNoProjects ) )
116             {
117                 throw new MojoFailureException( "No projects to invoke!" );
118             }
119             getLog().info( "No invoker report files found, nothing to check." );
120             return;
121         }
122 
123         BuildJobXpp3Reader reader = new BuildJobXpp3Reader();
124 
125         InvokerSession invokerSession = new InvokerSession();
126 
127         for ( File reportFile : reportFiles )
128         {
129             try ( Reader xmlReader = ReaderFactory.newXmlReader( reportFile ) )
130             {
131                 invokerSession.addJob( reader.read( xmlReader ) );
132             }
133             catch ( XmlPullParserException e )
134             {
135                 throw new MojoExecutionException( "Failed to parse report file: " + reportFile, e );
136             }
137             catch ( IOException e )
138             {
139                 throw new MojoExecutionException( "Failed to read report file: " + reportFile, e );
140             }
141         }
142 
143         if ( streamLogsOnFailures )
144         {
145             invokerSession.logFailedBuildLog( getLog(), ignoreFailures );
146         }
147 
148         if ( !suppressSummaries )
149         {
150             invokerSession.logSummary( getLog(), ignoreFailures );
151         }
152 
153         invokerSession.handleFailures( getLog(), ignoreFailures );
154     }
155 
156 }