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 java.io.IOException;
28  import java.util.Collections;
29  import java.util.List;
30  
31  import org.apache.maven.artifact.repository.DefaultRepositoryRequest;
32  import org.apache.maven.artifact.repository.RepositoryRequest;
33  import org.apache.maven.execution.MavenSession;
34  import org.apache.maven.model.Plugin;
35  import org.apache.maven.plugin.MavenPluginManager;
36  import org.apache.maven.plugin.Mojo;
37  import org.apache.maven.plugin.MojoExecution;
38  import org.apache.maven.plugin.MojoExecutionException;
39  import org.apache.maven.plugin.descriptor.MojoDescriptor;
40  import org.apache.maven.plugin.descriptor.PluginDescriptor;
41  import org.apache.maven.project.MavenProject;
42  import org.codehaus.plexus.configuration.PlexusConfiguration;
43  import org.codehaus.plexus.util.xml.Xpp3Dom;
44  import org.sonatype.aether.graph.DependencyFilter;
45  import org.sonatype.aether.graph.DependencyNode;
46  import org.sonatype.aether.repository.RemoteRepository;
47  
48  /**
49   * Configure pom and execute sonar internal maven plugin
50   */
51  public class Bootstraper
52  {
53  
54      private ServerMetadata server;
55      private MavenPluginManager pluginManager;
56  
57      public Bootstraper( ServerMetadata server, MavenPluginManager pluginManager )
58      {
59          this.server = server;
60          this.pluginManager = pluginManager;
61      }
62  
63      public void start( MavenProject project, MavenSession session ) throws IOException, MojoExecutionException
64      {
65          executeMojo( project, session );
66      }
67  
68      private void executeMojo( MavenProject project, MavenSession session ) throws MojoExecutionException
69      {
70          ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
71          try
72          {
73              
74              RepositoryRequest repositoryRequest = new DefaultRepositoryRequest();
75              repositoryRequest.setLocalRepository( session.getLocalRepository() );
76              repositoryRequest.setRemoteRepositories( project.getPluginArtifactRepositories() );
77  
78              Plugin plugin = createSonarPlugin();
79  
80              List<RemoteRepository> remoteRepositories = session.getCurrentProject().getRemotePluginRepositories();
81              
82              PluginDescriptor pluginDescriptor = pluginManager.getPluginDescriptor( plugin, remoteRepositories ,
83                  session.getRepositorySession() );
84  
85              String goal = "sonar";
86  
87              MojoDescriptor mojoDescriptor = pluginDescriptor.getMojo( goal );
88              if ( mojoDescriptor == null )
89              {
90                  throw new MojoExecutionException( "Unknown mojo goal: " + goal );
91              }
92              MojoExecution mojoExecution = new MojoExecution( plugin, goal, "sonar" + goal );
93  
94              mojoExecution.setConfiguration( convert( mojoDescriptor ) );
95  
96              mojoExecution.setMojoDescriptor( mojoDescriptor );
97  
98              // olamy : we exclude nothing and import nothing regarding realm import and artifacts
99              
100             DependencyFilter artifactFilter = new DependencyFilter()
101             {
102                 public boolean accept( DependencyNode arg0, List<DependencyNode> arg1 )
103                 {
104                     return true;
105                 }
106             };
107             
108             pluginManager.setupPluginRealm( pluginDescriptor, session, Thread.currentThread().getContextClassLoader(),
109                 Collections.<String>emptyList(), artifactFilter );            
110 
111             Mojo mojo = pluginManager.getConfiguredMojo( Mojo.class, session, mojoExecution );
112             Thread.currentThread().setContextClassLoader( pluginDescriptor.getClassRealm() );
113             mojo.execute();
114 
115         }
116         catch ( Exception e )
117         {
118             throw new MojoExecutionException( "Can not execute Sonar", e );
119         }
120         finally
121         {
122             Thread.currentThread().setContextClassLoader( originalClassLoader );
123         }
124     }
125 
126     private Xpp3Dom convert( MojoDescriptor mojoDescriptor )
127     {
128         Xpp3Dom dom = new Xpp3Dom( "configuration" );
129 
130         PlexusConfiguration c = mojoDescriptor.getMojoConfiguration();
131 
132         PlexusConfiguration[] ces = c.getChildren();
133 
134         if ( ces != null )
135         {
136             for ( PlexusConfiguration ce : ces )
137             {
138                 String value = ce.getValue( null );
139                 String defaultValue = ce.getAttribute( "default-value", null );
140                 if ( value != null || defaultValue != null )
141                 {
142                     Xpp3Dom e = new Xpp3Dom( ce.getName() );
143                     e.setValue( value );
144                     if ( defaultValue != null )
145                     {
146                         e.setAttribute( "default-value", defaultValue );
147                     }
148                     dom.addChild( e );
149                 }
150             }
151         }
152 
153         return dom;
154     }
155 
156     private Plugin createSonarPlugin() throws IOException
157     {
158         Plugin plugin = new Plugin();
159         plugin.setGroupId( "org.codehaus.sonar" );
160         plugin.setArtifactId( "sonar-maven3-plugin" );
161         plugin.setVersion( server.getVersion() );
162         return plugin;
163     }
164 }