1515import io .apicurio .registry .rest .client .models .CreateVersion ;
1616import io .apicurio .registry .rest .client .models .IfArtifactExists ;
1717import io .apicurio .registry .rest .client .models .ProblemDetails ;
18+ import io .apicurio .registry .rest .client .models .RuleViolationProblemDetails ;
1819import io .apicurio .registry .rest .client .models .VersionContent ;
1920import io .apicurio .registry .rest .client .models .VersionMetaData ;
2021import io .apicurio .registry .types .ArtifactType ;
2526import org .apache .avro .Schema ;
2627import org .apache .commons .io .FileUtils ;
2728import org .apache .maven .plugin .MojoExecutionException ;
29+ import org .apache .maven .plugin .MojoFailureException ;
2830import org .apache .maven .plugins .annotations .Mojo ;
2931import 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
0 commit comments