View Javadoc

1   package org.codehaus.mojo.sonar;
2   
3   /*
4    * The MIT License
5    *
6    * Copyright 2009 The Codehaus.
7    *
8    * Permission is hereby granted, free of charge, to any person obtaining a copy of
9    * this software and associated documentation files (the "Software"), to deal in
10   * the Software without restriction, including without limitation the rights to
11   * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12   * of the Software, and to permit persons to whom the Software is furnished to do
13   * so, subject to the following conditions:
14   *
15   * The above copyright notice and this permission notice shall be included in all
16   * copies or substantial portions of the Software.
17   *
18   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24   * SOFTWARE.
25   */
26  
27  import org.apache.maven.artifact.repository.ArtifactRepository;
28  import org.apache.maven.execution.MavenSession;
29  import org.apache.maven.plugin.AbstractMojo;
30  import org.apache.maven.plugin.MavenPluginManager;
31  import org.apache.maven.plugin.MojoExecutionException;
32  import org.apache.maven.plugin.MojoFailureException;
33  import org.apache.maven.project.MavenProject;
34  
35  import java.io.IOException;
36  
37  /**
38   * Analyse project. WARNING, Sonar server must be started.
39   *
40   * @goal sonar
41   * @aggregator
42   * @requiresDependencyResolution test
43   */
44  public class SonarMojo extends AbstractMojo
45  {
46  
47      /**
48       * @parameter default-value="${project}"
49       * @required
50       * @readonly
51       */
52      protected MavenProject project;
53  
54      /**
55       * @parameter default-value="${session}"
56       * @required
57       * @readonly
58       */
59      private MavenSession session;
60  
61      /**
62       * Sonar host URL.
63       *
64       * @parameter expression="${sonar.host.url}" default-value="http://localhost:9000" alias="sonar.host.url"
65       */
66      private String sonarHostURL;
67  
68      /**
69       * @component
70       * @required
71       */
72      protected MavenPluginManager mavenPluginManager;
73  
74      /**
75       * The local repository.
76       *
77       * @parameter expression="${localRepository}"
78       */
79      protected ArtifactRepository localRepository;
80  
81      public void execute() throws MojoExecutionException, MojoFailureException
82      {
83          try
84          {
85              ServerMetadata server = new ServerMetadata( sonarHostURL );
86              server.logSettings( getLog() );
87  
88              if (server.supportsMaven3() )
89              {
90                  new Bootstraper( server, mavenPluginManager ).start( project, session );
91              } else
92              {
93                  throw new MojoExecutionException( "Sonar " + server.getVersion() + " does not support Maven 3" );
94              }
95  
96          } catch ( IOException e )
97          {
98              throw new MojoExecutionException( "Failed to execute Sonar", e );
99          }
100     }
101 
102 }