@Test public void testBuildModelWithSnapshotBom_invalidCoordinates() throws ModelBuildingException, ArtifactResolutionException { for (String invalidCoordinates : ImmutableList.of("a.b.c:d", "a:b:c:d:e:1", "a::c:0.1")) { try { LinkageMonitor.buildModelWithSnapshotBom( system, session, invalidCoordinates, ImmutableMap.of()); fail("The method should invalidate coordinates: " + invalidCoordinates); } catch (IllegalArgumentException ex) { } } }
@VisibleForTesting static Model buildModelWithSnapshotBom( RepositorySystem repositorySystem, RepositorySystemSession session, String bomCoordinates, Map<String, String> localArtifacts) throws ModelBuildingException, ArtifactResolutionException { String[] elements = bomCoordinates.split(":"); DefaultArtifact bom; if (elements.length >= 4) { bom = new DefaultArtifact(bomCoordinates); } else if (elements.length == 3) { bom = new DefaultArtifact(elements[0], elements[1], "pom", elements[2]); } else { throw new IllegalArgumentException( "BOM coordinates do not have valid format: " + bomCoordinates); } ArtifactResult bomResult = repositorySystem.resolveArtifact( session, new ArtifactRequest(bom, ImmutableList.of(CENTRAL), null)); ModelBuildingRequest modelRequest = new DefaultModelBuildingRequest(); modelRequest.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL); modelRequest.setProcessPlugins(false); modelRequest.setTwoPhaseBuilding(true); modelRequest.setPomFile(bomResult.getArtifact().getFile()); modelRequest.setModelResolver( new VersionSubstitutingModelResolver( session, null, repositorySystem, new DefaultRemoteRepositoryManager(), ImmutableList.of(CENTRAL), localArtifacts)); modelRequest.setSystemProperties(System.getProperties()); ModelBuildingResult resultPhase1 = modelBuilder.build(modelRequest); DependencyManagement dependencyManagement = resultPhase1.getEffectiveModel().getDependencyManagement(); for (org.apache.maven.model.Dependency dependency : dependencyManagement.getDependencies()) { if ("import".equals(dependency.getScope())) { String version = localArtifacts.getOrDefault( dependency.getGroupId() + ":" + dependency.getArtifactId(), dependency.getVersion()); dependency.setVersion(version); } } ModelBuildingResult resultPhase2 = modelBuilder.build(modelRequest, resultPhase1); return resultPhase2.getEffectiveModel(); }
LinkageMonitor { @VisibleForTesting static Model buildModelWithSnapshotBom( RepositorySystem repositorySystem, RepositorySystemSession session, String bomCoordinates, Map<String, String> localArtifacts) throws ModelBuildingException, ArtifactResolutionException { String[] elements = bomCoordinates.split(":"); DefaultArtifact bom; if (elements.length >= 4) { bom = new DefaultArtifact(bomCoordinates); } else if (elements.length == 3) { bom = new DefaultArtifact(elements[0], elements[1], "pom", elements[2]); } else { throw new IllegalArgumentException( "BOM coordinates do not have valid format: " + bomCoordinates); } ArtifactResult bomResult = repositorySystem.resolveArtifact( session, new ArtifactRequest(bom, ImmutableList.of(CENTRAL), null)); ModelBuildingRequest modelRequest = new DefaultModelBuildingRequest(); modelRequest.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL); modelRequest.setProcessPlugins(false); modelRequest.setTwoPhaseBuilding(true); modelRequest.setPomFile(bomResult.getArtifact().getFile()); modelRequest.setModelResolver( new VersionSubstitutingModelResolver( session, null, repositorySystem, new DefaultRemoteRepositoryManager(), ImmutableList.of(CENTRAL), localArtifacts)); modelRequest.setSystemProperties(System.getProperties()); ModelBuildingResult resultPhase1 = modelBuilder.build(modelRequest); DependencyManagement dependencyManagement = resultPhase1.getEffectiveModel().getDependencyManagement(); for (org.apache.maven.model.Dependency dependency : dependencyManagement.getDependencies()) { if ("import".equals(dependency.getScope())) { String version = localArtifacts.getOrDefault( dependency.getGroupId() + ":" + dependency.getArtifactId(), dependency.getVersion()); dependency.setVersion(version); } } ModelBuildingResult resultPhase2 = modelBuilder.build(modelRequest, resultPhase1); return resultPhase2.getEffectiveModel(); } }
LinkageMonitor { @VisibleForTesting static Model buildModelWithSnapshotBom( RepositorySystem repositorySystem, RepositorySystemSession session, String bomCoordinates, Map<String, String> localArtifacts) throws ModelBuildingException, ArtifactResolutionException { String[] elements = bomCoordinates.split(":"); DefaultArtifact bom; if (elements.length >= 4) { bom = new DefaultArtifact(bomCoordinates); } else if (elements.length == 3) { bom = new DefaultArtifact(elements[0], elements[1], "pom", elements[2]); } else { throw new IllegalArgumentException( "BOM coordinates do not have valid format: " + bomCoordinates); } ArtifactResult bomResult = repositorySystem.resolveArtifact( session, new ArtifactRequest(bom, ImmutableList.of(CENTRAL), null)); ModelBuildingRequest modelRequest = new DefaultModelBuildingRequest(); modelRequest.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL); modelRequest.setProcessPlugins(false); modelRequest.setTwoPhaseBuilding(true); modelRequest.setPomFile(bomResult.getArtifact().getFile()); modelRequest.setModelResolver( new VersionSubstitutingModelResolver( session, null, repositorySystem, new DefaultRemoteRepositoryManager(), ImmutableList.of(CENTRAL), localArtifacts)); modelRequest.setSystemProperties(System.getProperties()); ModelBuildingResult resultPhase1 = modelBuilder.build(modelRequest); DependencyManagement dependencyManagement = resultPhase1.getEffectiveModel().getDependencyManagement(); for (org.apache.maven.model.Dependency dependency : dependencyManagement.getDependencies()) { if ("import".equals(dependency.getScope())) { String version = localArtifacts.getOrDefault( dependency.getGroupId() + ":" + dependency.getArtifactId(), dependency.getVersion()); dependency.setVersion(version); } } ModelBuildingResult resultPhase2 = modelBuilder.build(modelRequest, resultPhase1); return resultPhase2.getEffectiveModel(); }  }
LinkageMonitor { @VisibleForTesting static Model buildModelWithSnapshotBom( RepositorySystem repositorySystem, RepositorySystemSession session, String bomCoordinates, Map<String, String> localArtifacts) throws ModelBuildingException, ArtifactResolutionException { String[] elements = bomCoordinates.split(":"); DefaultArtifact bom; if (elements.length >= 4) { bom = new DefaultArtifact(bomCoordinates); } else if (elements.length == 3) { bom = new DefaultArtifact(elements[0], elements[1], "pom", elements[2]); } else { throw new IllegalArgumentException( "BOM coordinates do not have valid format: " + bomCoordinates); } ArtifactResult bomResult = repositorySystem.resolveArtifact( session, new ArtifactRequest(bom, ImmutableList.of(CENTRAL), null)); ModelBuildingRequest modelRequest = new DefaultModelBuildingRequest(); modelRequest.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL); modelRequest.setProcessPlugins(false); modelRequest.setTwoPhaseBuilding(true); modelRequest.setPomFile(bomResult.getArtifact().getFile()); modelRequest.setModelResolver( new VersionSubstitutingModelResolver( session, null, repositorySystem, new DefaultRemoteRepositoryManager(), ImmutableList.of(CENTRAL), localArtifacts)); modelRequest.setSystemProperties(System.getProperties()); ModelBuildingResult resultPhase1 = modelBuilder.build(modelRequest); DependencyManagement dependencyManagement = resultPhase1.getEffectiveModel().getDependencyManagement(); for (org.apache.maven.model.Dependency dependency : dependencyManagement.getDependencies()) { if ("import".equals(dependency.getScope())) { String version = localArtifacts.getOrDefault( dependency.getGroupId() + ":" + dependency.getArtifactId(), dependency.getVersion()); dependency.setVersion(version); } } ModelBuildingResult resultPhase2 = modelBuilder.build(modelRequest, resultPhase1); return resultPhase2.getEffectiveModel(); }  static void main(String[] arguments); }
LinkageMonitor { @VisibleForTesting static Model buildModelWithSnapshotBom( RepositorySystem repositorySystem, RepositorySystemSession session, String bomCoordinates, Map<String, String> localArtifacts) throws ModelBuildingException, ArtifactResolutionException { String[] elements = bomCoordinates.split(":"); DefaultArtifact bom; if (elements.length >= 4) { bom = new DefaultArtifact(bomCoordinates); } else if (elements.length == 3) { bom = new DefaultArtifact(elements[0], elements[1], "pom", elements[2]); } else { throw new IllegalArgumentException( "BOM coordinates do not have valid format: " + bomCoordinates); } ArtifactResult bomResult = repositorySystem.resolveArtifact( session, new ArtifactRequest(bom, ImmutableList.of(CENTRAL), null)); ModelBuildingRequest modelRequest = new DefaultModelBuildingRequest(); modelRequest.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL); modelRequest.setProcessPlugins(false); modelRequest.setTwoPhaseBuilding(true); modelRequest.setPomFile(bomResult.getArtifact().getFile()); modelRequest.setModelResolver( new VersionSubstitutingModelResolver( session, null, repositorySystem, new DefaultRemoteRepositoryManager(), ImmutableList.of(CENTRAL), localArtifacts)); modelRequest.setSystemProperties(System.getProperties()); ModelBuildingResult resultPhase1 = modelBuilder.build(modelRequest); DependencyManagement dependencyManagement = resultPhase1.getEffectiveModel().getDependencyManagement(); for (org.apache.maven.model.Dependency dependency : dependencyManagement.getDependencies()) { if ("import".equals(dependency.getScope())) { String version = localArtifacts.getOrDefault( dependency.getGroupId() + ":" + dependency.getArtifactId(), dependency.getVersion()); dependency.setVersion(version); } } ModelBuildingResult resultPhase2 = modelBuilder.build(modelRequest, resultPhase1); return resultPhase2.getEffectiveModel(); }  static void main(String[] arguments);  }