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.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
36
37
38
39
40
41
42
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
52
53 @Parameter( alias = "checkPolicies", property = Constants.CHECK_POLICIES, required = false, defaultValue = "false")
54 private boolean checkPolicies;
55
56
57
58 public UpdateMojo() {
59 }
60
61
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
71 init();
72
73
74 Collection<AgentProjectInfo> projectInfos = extractProjectInfos();
75
76
77 if (projectInfos == null || projectInfos.isEmpty()) {
78 info("No open source information found.");
79 } else {
80 sendUpdate(projectInfos);
81 }
82 }
83
84
85
86 private void init() {
87
88 for (Map.Entry<Object, Object> entry : specialModuleTokens.entrySet()) {
89 moduleTokens.put(entry.getKey().toString(), entry.getValue().toString());
90 }
91
92
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);
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
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
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 }