View Javadoc

1   /*
2    * The MIT License
3    *
4    * Copyright 2009 The Codehaus.
5    *
6    * Permission is hereby granted, free of charge, to any person obtaining a copy of
7    * this software and associated documentation files (the "Software"), to deal in
8    * the Software without restriction, including without limitation the rights to
9    * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10   * of the Software, and to permit persons to whom the Software is furnished to do
11   * so, subject to the following conditions:
12   *
13   * The above copyright notice and this permission notice shall be included in all
14   * copies or substantial portions of the Software.
15   *
16   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22   * SOFTWARE.
23   */
24  package org.sonar.mojo.bootstrap;
25  
26  import org.apache.maven.artifact.repository.ArtifactRepository;
27  import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
28  import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
29  import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
30  import org.apache.maven.execution.MavenSession;
31  import org.apache.maven.model.Plugin;
32  import org.apache.maven.plugin.MojoExecution;
33  import org.apache.maven.plugin.MojoExecutionException;
34  import org.apache.maven.plugin.PluginManager;
35  import org.apache.maven.plugin.descriptor.MojoDescriptor;
36  import org.apache.maven.plugin.descriptor.PluginDescriptor;
37  import org.apache.maven.project.MavenProject;
38  
39  import java.io.IOException;
40  import java.util.ArrayList;
41  import java.util.List;
42  
43  /**
44   * Configure pom and execute sonar internal maven plugin
45   */
46  public class Bootstraper {
47  
48      public static final String REPOSITORY_ID = "sonar";
49  
50      private ServerMetadata server;
51      private PluginManager pluginManager;
52      private ArtifactRepositoryFactory repoFactory;
53  
54      public Bootstraper(ServerMetadata server, ArtifactRepositoryFactory repoFactory, PluginManager pluginManager) {
55          this.server = server;
56          this.repoFactory = repoFactory;
57          this.pluginManager = pluginManager;
58      }
59  
60      public void start(MavenProject project, MavenSession session) throws IOException, MojoExecutionException {
61          configure(project);
62          executeMojo(project, session);
63      }
64  
65      private void executeMojo(MavenProject project, MavenSession session) throws MojoExecutionException {
66          try {
67              PluginDescriptor pluginDescriptor = pluginManager.verifyPlugin(
68                      createSonarPlugin(),
69                      project,
70                      session.getSettings(),
71                      session.getLocalRepository());
72              MojoDescriptor mojoDescriptor = pluginDescriptor.getMojo("internal");
73              if (mojoDescriptor == null) {
74                  throw new MojoExecutionException("Unknown mojo goal: install");
75              }
76              pluginManager.executeMojo(project, new MojoExecution(mojoDescriptor), session);
77  
78          } catch (Exception e) {
79              throw new MojoExecutionException("Can not execute Sonar", e);
80          }
81      }
82  
83      private void configure(MavenProject project) throws IOException {
84          configureRepositories(project);
85      }
86  
87      private void configureRepositories(MavenProject project) throws IOException {
88          List<ArtifactRepository> pluginRepositories = new ArrayList<ArtifactRepository>();
89          ArtifactRepository repository = createSonarRepository(repoFactory);
90          pluginRepositories.add(repository);
91          pluginRepositories.addAll(project.getPluginArtifactRepositories());
92          project.setPluginArtifactRepositories(pluginRepositories);
93  
94          List<ArtifactRepository> artifactRepositories = new ArrayList<ArtifactRepository>();
95          artifactRepositories.add(repository);
96          artifactRepositories.addAll(project.getRemoteArtifactRepositories());
97          project.setRemoteArtifactRepositories(artifactRepositories);
98  
99      }
100 
101     private Plugin createSonarPlugin() throws IOException {
102         Plugin plugin = new Plugin();
103         plugin.setGroupId("org.codehaus.sonar.runtime");
104         plugin.setArtifactId("sonar-core-maven-plugin");
105         plugin.setVersion(server.getKey());
106         return plugin;
107     }
108 
109     private ArtifactRepository createSonarRepository(ArtifactRepositoryFactory repoFactory) {
110         return repoFactory.createArtifactRepository(REPOSITORY_ID, server.getMavenRepositoryUrl(),
111                 new DefaultRepositoryLayout(),
112                 new ArtifactRepositoryPolicy(false, "never", "ignore"), // snapshot
113                 new ArtifactRepositoryPolicy(true, "never", "ignore"));
114     }
115 }