Skip to content

Commit db5b229

Browse files
authored
fix(maven): Improved maven plugin output on failure (#5690)
* Improved maven plugin output on failure * spotless:apply
1 parent a758eeb commit db5b229

File tree

5 files changed

+82
-24
lines changed

5 files changed

+82
-24
lines changed

app/src/main/java/io/apicurio/registry/services/http/CoreRegistryExceptionMapperService.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,19 +81,21 @@ private ProblemDetails toProblemDetails(Throwable t, int code) {
8181
if (t instanceof RuleViolationException) {
8282
RuleViolationException rve = (RuleViolationException) t;
8383
details = new RuleViolationProblemDetails();
84+
((RuleViolationProblemDetails) details).setTitle(rve.getMessage());
85+
((RuleViolationProblemDetails) details).setDetail(rve.getDetailMessage());
8486
((RuleViolationProblemDetails) details).setCauses(toRestCauses(rve.getCauses()));
8587
} else {
8688
details = new ProblemDetails();
89+
details.setTitle(t.getLocalizedMessage());
90+
if (includeStackTrace) {
91+
details.setDetail(getStackTrace(t));
92+
} else {
93+
details.setDetail(getRootMessage(t));
94+
}
8795
}
8896

8997
details.setStatus(code);
90-
details.setTitle(t.getLocalizedMessage());
9198
details.setName(t.getClass().getSimpleName());
92-
if (includeStackTrace) {
93-
details.setDetail(getStackTrace(t));
94-
} else {
95-
details.setDetail(getRootMessage(t));
96-
}
9799
return details;
98100
}
99101

schema-util/common/src/main/java/io/apicurio/registry/rules/RuleViolationException.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ public RuleViolationException(String message, RuleType ruleType, String ruleConf
7373
this.causes = causes;
7474
}
7575

76-
@Override
77-
public String getMessage() {
76+
public String getDetailMessage() {
7877
return super.getMessage() + causes.stream().map(rv -> rv.getDescription()
7978
+ (rv.getContext() != null && !rv.getContext().isBlank() ? " at " + rv.getContext() : ""))
8079
.reduce((left, right) -> left + ", " + right).map(s -> " Causes: " + s).orElse("");

utils/maven-plugin/src/main/java/io/apicurio/registry/maven/AbstractRegistryMojo.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package io.apicurio.registry.maven;
22

3+
import com.microsoft.kiota.ApiException;
34
import io.apicurio.registry.client.auth.VertXAuthFactory;
45
import io.apicurio.registry.rest.client.RegistryClient;
6+
import io.apicurio.registry.rest.client.models.ProblemDetails;
7+
import io.apicurio.registry.rest.client.models.RuleViolationProblemDetails;
58
import io.apicurio.registry.types.ContentTypes;
69
import io.kiota.http.vertx.VertXRequestAdapter;
710
import io.vertx.core.Vertx;
@@ -141,4 +144,37 @@ public void setUsername(String username) {
141144
public void setPassword(String password) {
142145
this.password = password;
143146
}
147+
148+
protected void logAndThrow(ApiException e) throws MojoExecutionException, MojoFailureException {
149+
if (e instanceof RuleViolationProblemDetails) {
150+
logAndThrow((RuleViolationProblemDetails) e);
151+
}
152+
if (e instanceof ProblemDetails) {
153+
logAndThrow((ProblemDetails) e);
154+
}
155+
}
156+
157+
protected void logAndThrow(ProblemDetails e) throws MojoExecutionException {
158+
getLog().error("---");
159+
getLog().error("Error registering artifact: " + e.getName());
160+
getLog().error(e.getTitle());
161+
getLog().error(e.getDetail());
162+
getLog().error("---");
163+
throw new MojoExecutionException("Error registering artifact: " + e.getName(), e);
164+
}
165+
166+
protected void logAndThrow(RuleViolationProblemDetails e) throws MojoFailureException {
167+
getLog().error("---");
168+
getLog().error("Registry rule validation failure: " + e.getName());
169+
getLog().error(e.getTitle());
170+
if (e.getCauses() != null) {
171+
e.getCauses().forEach(cause -> {
172+
getLog().error("\t-> " + cause.getContext());
173+
getLog().error("\t " + cause.getDescription());
174+
});
175+
}
176+
getLog().error("---");
177+
throw new MojoFailureException("Registry rule validation failure: " + e.getName(), e);
178+
}
179+
144180
}

utils/maven-plugin/src/main/java/io/apicurio/registry/maven/RegisterRegistryMojo.java

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import io.apicurio.registry.rest.client.models.CreateVersion;
1616
import io.apicurio.registry.rest.client.models.IfArtifactExists;
1717
import io.apicurio.registry.rest.client.models.ProblemDetails;
18+
import io.apicurio.registry.rest.client.models.RuleViolationProblemDetails;
1819
import io.apicurio.registry.rest.client.models.VersionContent;
1920
import io.apicurio.registry.rest.client.models.VersionMetaData;
2021
import io.apicurio.registry.types.ArtifactType;
@@ -25,6 +26,7 @@
2526
import org.apache.avro.Schema;
2627
import org.apache.commons.io.FileUtils;
2728
import org.apache.maven.plugin.MojoExecutionException;
29+
import org.apache.maven.plugin.MojoFailureException;
2830
import org.apache.maven.plugins.annotations.Mojo;
2931
import org.apache.maven.plugins.annotations.Parameter;
3032

@@ -54,7 +56,7 @@ public class RegisterRegistryMojo extends AbstractRegistryMojo {
5456
/**
5557
* The list of pre-registered artifacts that can be used as references.
5658
*/
57-
@Parameter(required = true)
59+
@Parameter(required = false)
5860
List<ExistingReference> existingReferences;
5961

6062
/**
@@ -69,6 +71,13 @@ public class RegisterRegistryMojo extends AbstractRegistryMojo {
6971
@Parameter(property = "skipRegister", defaultValue = "false")
7072
boolean skip;
7173

74+
/**
75+
* Set this to 'true' to perform the action with the "dryRun" option enabled. This will effectively test
76+
* whether registration *would have worked*. But it results in no changes made on the server.
77+
*/
78+
@Parameter(property = "dryRun", defaultValue = "false")
79+
boolean dryRun;
80+
7281
DefaultArtifactTypeUtilProviderImpl utilProviderFactory = new DefaultArtifactTypeUtilProviderImpl();
7382

7483
/**
@@ -85,6 +94,10 @@ protected boolean validate() throws MojoExecutionException {
8594
return false;
8695
}
8796

97+
if (existingReferences == null) {
98+
existingReferences = new ArrayList<>();
99+
}
100+
88101
int idx = 0;
89102
int errorCount = 0;
90103
for (RegisterArtifact artifact : artifacts) {
@@ -182,8 +195,8 @@ protected void executeInternal() throws MojoExecutionException {
182195
}
183196

184197
private VersionMetaData registerWithAutoRefs(RegistryClient registryClient, RegisterArtifact artifact,
185-
ReferenceIndex index, Stack<RegisterArtifact> registrationStack)
186-
throws IOException, ExecutionException, InterruptedException {
198+
ReferenceIndex index, Stack<RegisterArtifact> registrationStack) throws IOException,
199+
ExecutionException, InterruptedException, MojoExecutionException, MojoFailureException {
187200
if (loopDetected(artifact, registrationStack)) {
188201
throw new RuntimeException(
189202
"Artifact reference loop detected (not supported): " + printLoop(registrationStack));
@@ -203,7 +216,8 @@ private VersionMetaData registerWithAutoRefs(RegistryClient registryClient, Regi
203216
.findExternalReferences(typedArtifactContent);
204217

205218
// Register all of the references first, then register the artifact.
206-
List<ArtifactReference> registeredReferences = externalReferences.stream().map(externalRef -> {
219+
List<ArtifactReference> registeredReferences = new ArrayList<>(externalReferences.size());
220+
for (ExternalReference externalRef : externalReferences) {
207221
IndexedResource iresource = index.lookup(externalRef.getResource(),
208222
Paths.get(artifact.getFile().toURI()));
209223

@@ -236,16 +250,17 @@ private VersionMetaData registerWithAutoRefs(RegistryClient registryClient, Regi
236250
reference.setVersion(iresource.getRegistration().getVersion());
237251
reference.setGroupId(iresource.getRegistration().getGroupId());
238252
reference.setArtifactId(iresource.getRegistration().getArtifactId());
239-
240-
return reference;
241-
}).sorted((ref1, ref2) -> ref1.getName().compareTo(ref2.getName())).collect(Collectors.toList());
253+
registeredReferences.add(reference);
254+
}
255+
registeredReferences.sort((ref1, ref2) -> ref1.getName().compareTo(ref2.getName()));
242256

243257
registrationStack.pop();
244258
return registerArtifact(registryClient, artifact, registeredReferences);
245259
}
246260

247261
private void registerDirectory(RegistryClient registryClient, RegisterArtifact artifact)
248-
throws IOException, ExecutionException, InterruptedException {
262+
throws IOException, ExecutionException, InterruptedException, MojoExecutionException,
263+
MojoFailureException {
249264
switch (artifact.getArtifactType()) {
250265
case ArtifactType.AVRO:
251266
final AvroDirectoryParser avroDirectoryParser = new AvroDirectoryParser(registryClient);
@@ -277,8 +292,8 @@ private void registerDirectory(RegistryClient registryClient, RegisterArtifact a
277292
}
278293

279294
private VersionMetaData registerArtifact(RegistryClient registryClient, RegisterArtifact artifact,
280-
List<ArtifactReference> references)
281-
throws FileNotFoundException, ExecutionException, InterruptedException {
295+
List<ArtifactReference> references) throws FileNotFoundException, ExecutionException,
296+
InterruptedException, MojoExecutionException, MojoFailureException {
282297
if (artifact.getFile() != null) {
283298
return registerArtifact(registryClient, artifact, new FileInputStream(artifact.getFile()),
284299
references);
@@ -303,7 +318,7 @@ private VersionMetaData getArtifactVersionMetadata(RegistryClient registryClient
303318

304319
private VersionMetaData registerArtifact(RegistryClient registryClient, RegisterArtifact artifact,
305320
InputStream artifactContent, List<ArtifactReference> references)
306-
throws ExecutionException, InterruptedException {
321+
throws ExecutionException, InterruptedException, MojoFailureException, MojoExecutionException {
307322
String groupId = artifact.getGroupId();
308323
String artifactId = artifact.getArtifactId();
309324
String version = artifact.getVersion();
@@ -350,6 +365,9 @@ private VersionMetaData registerArtifact(RegistryClient registryClient, Register
350365
if (artifact.getIfExists() != null) {
351366
config.queryParameters.ifExists = IfArtifactExists
352367
.forValue(artifact.getIfExists().value());
368+
if (dryRun) {
369+
config.queryParameters.dryRun = true;
370+
}
353371
}
354372
config.queryParameters.canonical = canonicalize;
355373
});
@@ -358,8 +376,9 @@ private VersionMetaData registerArtifact(RegistryClient registryClient, Register
358376
groupId, artifactId, vmd.getVersion().getGlobalId()));
359377

360378
return vmd.getVersion();
361-
} catch (ProblemDetails e) {
362-
throw new RuntimeException(e.getDetail());
379+
} catch (RuleViolationProblemDetails | ProblemDetails e) {
380+
logAndThrow(e);
381+
return null;
363382
}
364383
}
365384

@@ -368,8 +387,8 @@ private static boolean hasReferences(RegisterArtifact artifact) {
368387
}
369388

370389
private List<ArtifactReference> processArtifactReferences(RegistryClient registryClient,
371-
List<RegisterArtifactReference> referencedArtifacts)
372-
throws FileNotFoundException, ExecutionException, InterruptedException {
390+
List<RegisterArtifactReference> referencedArtifacts) throws FileNotFoundException,
391+
ExecutionException, InterruptedException, MojoExecutionException, MojoFailureException {
373392
List<ArtifactReference> references = new ArrayList<>();
374393
for (RegisterArtifactReference artifact : referencedArtifacts) {
375394
List<ArtifactReference> nestedReferences = new ArrayList<>();
@@ -434,7 +453,6 @@ private void addExistingReferencesToIndex(RegistryClient registryClient, Referen
434453
}
435454
index.index(ref.getResourceName(), vmd);
436455
}
437-
;
438456
}
439457
}
440458

utils/maven-plugin/src/main/java/io/apicurio/registry/maven/TestUpdateRegistryMojo.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@
1515

1616
/**
1717
* Test artifact against current artifact rules, if an update is possible / valid.
18+
*
19+
* @deprecated In favor of using the "dryRun" option of the "register" mojo.
1820
*/
1921
@Mojo(name = "test-update")
22+
@Deprecated
2023
public class TestUpdateRegistryMojo extends AbstractRegistryMojo {
2124

2225
/**

0 commit comments

Comments
 (0)