View Javadoc

1   /**
2    * Copyright (C) 2011 White Source Ltd.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.whitesource.maven;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.apache.maven.plugin.MojoExecutionException;
20  import org.apache.maven.plugin.MojoFailureException;
21  import org.apache.maven.plugins.annotations.LifecyclePhase;
22  import org.apache.maven.plugins.annotations.Mojo;
23  import org.apache.maven.plugins.annotations.Parameter;
24  import org.apache.maven.plugins.annotations.ResolutionScope;
25  import org.apache.maven.project.MavenProject;
26  import org.whitesource.agent.api.dispatch.CheckPoliciesResult;
27  import org.whitesource.agent.api.dispatch.UpdateInventoryResult;
28  import org.whitesource.agent.api.model.AgentProjectInfo;
29  import org.whitesource.agent.client.WssServiceException;
30  
31  import java.util.Collection;
32  import java.util.Map;
33  
34  /**
35   * Send updates of open source software usage information to White Source.
36   *
37   * <p>
38   *     Further documentation for the plugin and its usage can be found in the
39   *     <a href="http://docs.whitesourcesoftware.com/display/serviceDocs/Maven+plugin">online documentation</a>.
40   * </p>
41   *
42   * @author Edo.Shor
43   */
44  @Mojo(name = "update",
45          defaultPhase = LifecyclePhase.PACKAGE,
46          requiresDependencyResolution = ResolutionScope.TEST,
47          aggregator = true )
48  public class UpdateMojo extends AgentMojo {
49  
50      /**
51       * Optional. Set to true to check policies.
52       */
53      @Parameter( alias = "checkPolicies", property = Constants.CHECK_POLICIES, required = false, defaultValue = "false")
54      private boolean checkPolicies;
55  
56      /* --- Constructors --- */
57  
58      public UpdateMojo() {
59      }
60  
61      /* --- Concrete implementation methods --- */
62  
63      @Override
64      public void doExecute() throws MojoExecutionException, MojoFailureException {
65          if (reactorProjects == null) {
66              info("No projects found. Skipping update");
67              return;
68          }
69  
70          // initialize
71          init();
72  
73          // Collect OSS usage information
74          Collection<AgentProjectInfo> projectInfos = extractProjectInfos();
75  
76          // send to white source
77          if (projectInfos == null || projectInfos.isEmpty()) {
78              info("No open source information found.");
79          } else {
80              sendUpdate(projectInfos);
81          }
82      }
83  
84      /* --- Private methods --- */
85  
86      private void init() {
87          // copy token for modules with special names into moduleTokens.
88          for (Map.Entry<Object, Object> entry : specialModuleTokens.entrySet()) {
89              moduleTokens.put(entry.getKey().toString(), entry.getValue().toString());
90          }
91  
92          // take product name and version from top level project
93          MavenProject topLevelProject = session.getTopLevelProject();
94          if (topLevelProject != null) {
95              if (StringUtils.isBlank(product)) {
96                  product = topLevelProject.getName();
97              }
98              if (StringUtils.isBlank(product)) {
99                  product = topLevelProject.getArtifactId();
100             }
101         }
102     }
103 
104     private void sendUpdate(Collection<AgentProjectInfo> projectInfos) throws MojoFailureException, MojoExecutionException {
105         try {
106             UpdateInventoryResult updateResult;
107             if (checkPolicies) {
108                 info("Checking policies...");
109                 CheckPoliciesResult result = service.checkPolicies(orgToken, product, productVersion, projectInfos);
110 
111                 if (outputDirectory == null ||
112                         (!outputDirectory.exists() && !outputDirectory.mkdirs())) {
113                     warn("Output directory doesn't exist. Skipping policies check report.");
114                 } else {
115                     generateReport(result);
116                 }
117 
118                 if (result.hasRejections()) {
119                     String msg = "Some dependencies were rejected by the organization's policies.";
120                     throw new MojoExecutionException(msg); // this is handled in base class
121                 } else {
122                     info("All dependencies conform with the organization's policies.");
123                     info("Sending updates to White Source");
124                     updateResult = service.update(orgToken, product, productVersion, projectInfos);
125                 }
126             } else {
127                 info("Sending updates to White Source");
128                 updateResult = service.update(orgToken, product, productVersion, projectInfos);
129             }
130             logResult(updateResult);
131         } catch (WssServiceException e) {
132             throw new MojoExecutionException(Constants.ERROR_SERVICE_CONNECTION + e.getMessage(), e);
133         }
134     }
135 
136     private void logResult(UpdateInventoryResult result) {
137         info("Inventory update results for " + result.getOrganization());
138 
139         // newly created projects
140         Collection<String> createdProjects = result.getCreatedProjects();
141         if (createdProjects.isEmpty()) {
142             info("No new projects found.");
143         } else {
144             info("Newly created projects:");
145             for (String projectName : createdProjects) {
146                 info(projectName);
147             }
148         }
149 
150         // updated projects
151         Collection<String> updatedProjects = result.getUpdatedProjects();
152         if (updatedProjects.isEmpty()) {
153             info("No projects were updated.");
154         } else {
155             info("Updated projects:");
156             for (String projectName : updatedProjects) {
157                 info(projectName);
158             }
159         }
160     }
161 
162 }