1
2
3
4
5
6
7
8
9
10
11
12
13
14
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.ResolutionScope;
24 import org.apache.maven.project.MavenProject;
25 import org.whitesource.agent.api.dispatch.CheckPoliciesResult;
26 import org.whitesource.agent.api.model.AgentProjectInfo;
27 import org.whitesource.agent.client.WssServiceException;
28
29 import java.util.Collection;
30 import java.util.Map;
31
32
33
34
35
36
37
38
39
40
41
42 @Mojo(name = "checkPolicies",
43 defaultPhase = LifecyclePhase.PACKAGE,
44 requiresDependencyResolution = ResolutionScope.TEST,
45 aggregator = true )
46 public class CheckPoliciesMojo extends AgentMojo {
47
48
49
50 public CheckPoliciesMojo() {
51 }
52
53
54
55 @Override
56 public void doExecute() throws MojoExecutionException, MojoFailureException {
57 if (reactorProjects == null) {
58 info("No projects found. Skipping update");
59 return;
60 }
61
62
63 init();
64
65
66 Collection<AgentProjectInfo> projectInfos = extractProjectInfos();
67
68
69 if (projectInfos == null || projectInfos.isEmpty()) {
70 info("No open source information found.");
71 } else {
72 sendCheckPolicies(projectInfos);
73 }
74 }
75
76
77
78 private void init() {
79
80 for (Map.Entry<Object, Object> entry : specialModuleTokens.entrySet()) {
81 moduleTokens.put(entry.getKey().toString(), entry.getValue().toString());
82 }
83
84
85 MavenProject topLevelProject = session.getTopLevelProject();
86 if (topLevelProject != null) {
87 if (StringUtils.isBlank(product)) {
88 product = topLevelProject.getName();
89 }
90 if (StringUtils.isBlank(product)) {
91 product = topLevelProject.getArtifactId();
92 }
93 }
94 }
95
96 private void sendCheckPolicies(Collection<AgentProjectInfo> projectInfos) throws MojoFailureException, MojoExecutionException {
97 try {
98 info("Checking policies...");
99 CheckPoliciesResult result = service.checkPolicies(orgToken, product, productVersion, projectInfos);
100
101 if (outputDirectory == null ||
102 (!outputDirectory.exists() && !outputDirectory.mkdirs())) {
103 warn("Output directory doesn't exist. Skipping policies check report.");
104 } else {
105 generateReport(result);
106 }
107
108 if (result.hasRejections()) {
109 String msg = "Some dependencies were rejected by the organization's policies.";
110 throw new MojoExecutionException(msg);
111 } else {
112 info("All dependencies conform with the organization's policies.");
113 }
114 } catch (WssServiceException e) {
115 throw new MojoExecutionException(Constants.ERROR_SERVICE_CONNECTION + e.getMessage(), e);
116 }
117 }
118
119 }