View Javadoc

1   package org.apache.maven.plugin.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.plugin.invoker.model.io.xpp3.BuildJobXpp3Reader;
26  import org.codehaus.plexus.util.ReaderFactory;
27  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
28  
29  import java.io.File;
30  import java.io.IOException;
31  
32  /**
33   * Checks the results of maven-invoker-plugin based integration tests and fails the build if any tests failed.
34   *
35   * @goal verify
36   * @phase verify
37   * @author <a href="mailto:olamy@apache.org">olamy</a>
38   * @since 1.4
39   */
40  public class VerifyMojo extends AbstractMojo
41  {
42  
43      /**
44       * Flag used to suppress certain invocations. This is useful in tailoring the build using profiles.
45       *
46       * @parameter expression="${invoker.skip}" default-value="false"
47       * @since 1.1
48       */
49      private boolean skipInvocation;
50  
51      /**
52       * Base directory where all build reports are read from.
53       *
54       * @parameter expression="${invoker.reportsDirectory}" default-value="${project.build.directory}/invoker-reports"
55       * @since 1.4
56       */
57      private File reportsDirectory;
58  
59      /**
60       * A flag controlling whether failures of the sub builds should fail the main build, too. If set to
61       * <code>true</code>, the main build will proceed even if one or more sub builds failed.
62       *
63       * @parameter expression="${maven.test.failure.ignore}" default-value="false"
64       * @since 1.3
65       */
66      private boolean ignoreFailures;
67  
68      /**
69       * Flag used to suppress the summary output notifying of successes and failures. If set to <code>true</code>, the
70       * only indication of the build's success or failure will be the effect it has on the main build (if it fails, the
71       * main build should fail as well).
72       *
73       * @parameter default-value="false"
74       */
75      private boolean suppressSummaries;
76  
77      /**
78       * Invokes Maven on the configured test projects.
79       *
80       * @throws org.apache.maven.plugin.MojoExecutionException If the goal encountered severe errors.
81       * @throws org.apache.maven.plugin.MojoFailureException If any of the Maven builds failed.
82       */
83      public void execute()
84          throws MojoExecutionException, MojoFailureException
85      {
86          if ( skipInvocation )
87          {
88              getLog().info( "Skipping invocation per configuration."
89                  + " If this is incorrect, ensure the skipInvocation parameter is not set to true." );
90              return;
91          }
92  
93          File[] reportFiles = ReportUtils.getReportFiles( reportsDirectory );
94          if ( reportFiles.length <= 0 )
95          {
96              getLog().info( "No invoker report files found, nothing to check." );
97              return;
98          }
99  
100         InvokerSession invokerSession = new InvokerSession();
101         for ( int i = 0, size = reportFiles.length; i < size; i++ )
102         {
103             File reportFile = reportFiles[i];
104             try
105             {
106                 BuildJobXpp3Reader reader = new BuildJobXpp3Reader();
107                 invokerSession.addJob( reader.read( ReaderFactory.newXmlReader( reportFile ) ) );
108             }
109             catch ( XmlPullParserException e )
110             {
111                 throw new MojoExecutionException( "Failed to parse report file: " + reportFile, e );
112             }
113             catch ( IOException e )
114             {
115                 throw new MojoExecutionException( "Failed to read report file: " + reportFile, e );
116             }
117         }
118 
119         if ( !suppressSummaries )
120         {
121             invokerSession.logSummary( getLog(), ignoreFailures );
122         }
123 
124         invokerSession.handleFailures( getLog(), ignoreFailures );
125     }
126 
127 }