@Test public void testArtifactResolution() throws Exception { String coords = "x:y:1.2.3"; DefaultModelResolver modelResolver = mock(DefaultModelResolver.class); when(modelResolver.resolveModel(fromCoords(coords))) .thenReturn(new UrlModelSource(new URL("http://repo.foo.org/maven/x/y/1.2.3/y-1.2.3.pom"))); String httpGetBody = "5fe28b9518e58819180a43a850fbc0dd24b7c050"; Resolver resolver = new Resolver(modelResolver, ALIASES) { protected InputStream httpGet(String url) throws IOException { return new ByteArrayInputStream(httpGetBody.getBytes()); } }; resolver.resolveArtifact(coords); Collection<Rule> rules = resolver.getRules(); assertThat(rules).hasSize(1); Rule rule = rules.iterator().next(); assertThat(rule.name()).isEqualTo("x_y"); assertThat(rule.getRepository()).isEqualTo("http://repo.foo.org/maven/"); assertThat(rule.getSha1()).isEqualTo("5fe28b9518e58819180a43a850fbc0dd24b7c050"); }
private void updateDependencyResolutionContext( List<Map<String, String>> bomDependencies) { URI[] uris = Grape.getInstance().resolve(null, bomDependencies.toArray(new Map[bomDependencies.size()])); DefaultModelBuilder modelBuilder = new DefaultModelBuilderFactory().newInstance(); for (URI uri : uris) { try { DefaultModelBuildingRequest request = new DefaultModelBuildingRequest(); request.setModelResolver(new GrapeModelResolver()); request.setModelSource(new UrlModelSource(uri.toURL())); request.setSystemProperties(System.getProperties()); Model model = modelBuilder.build(request).getEffectiveModel(); this.resolutionContext.addDependencyManagement( new MavenModelDependencyManagement(model)); } catch (Exception ex) { throw new IllegalStateException("Failed to build model for '" + uri + "'. Is it a valid Maven bom?", ex); } } }
@Override public ModelSource resolveModel(String groupId, String artifactId, String version) throws UnresolvableModelException { Map<String, String> dependency = new HashMap<String, String>(); dependency.put("group", groupId); dependency.put("module", artifactId); dependency.put("version", version); dependency.put("type", "pom"); try { return new UrlModelSource( Grape.getInstance().resolve(null, dependency)[0].toURL()); } catch (MalformedURLException e) { throw new UnresolvableModelException(e.getMessage(), groupId, artifactId, version); } }
private List<MavenProject> getProjectsForMavenReactor( MavenSession session ) throws ProjectBuildingException { MavenExecutionRequest request = session.getRequest(); request.getProjectBuildingRequest().setRepositorySession( session.getRepositorySession() ); List<MavenProject> projects = new ArrayList<MavenProject>(); // We have no POM file. // if ( request.getPom() == null ) { ModelSource modelSource = new UrlModelSource( DefaultMaven.class.getResource( "project/standalone.xml" ) ); MavenProject project = projectBuilder.build( modelSource, request.getProjectBuildingRequest() ).getProject(); project.setExecutionRoot( true ); projects.add( project ); request.setProjectPresent( false ); return projects; } List<File> files = Arrays.asList( request.getPom().getAbsoluteFile() ); collectProjects( projects, files, request ); return projects; }
private List<MavenProject> getProjectsForMavenReactor( MavenExecutionRequest request ) throws ProjectBuildingException { List<MavenProject> projects = new ArrayList<MavenProject>(); // We have no POM file. // if ( request.getPom() == null ) { ModelSource modelSource = new UrlModelSource( DefaultMaven.class.getResource( "project/standalone.xml" ) ); MavenProject project = projectBuilder.build( modelSource, request.getProjectBuildingRequest() ).getProject(); project.setExecutionRoot( true ); projects.add( project ); request.setProjectPresent( false ); return projects; } List<File> files = Arrays.asList( request.getPom().getAbsoluteFile() ); collectProjects( projects, files, request ); return projects; }
/** * This is used for pom-less execution like running archetype:generate. I am taking out the profile handling and the * interpolation of the base directory until we spec this out properly. */ public MavenProject buildStandaloneSuperProject( ProjectBuilderConfiguration configuration ) throws ProjectBuildingException { ProjectBuildingRequest request = injectSession( toRequest( configuration ) ); request.setProcessPlugins( false ); request.setValidationLevel( ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL ); ModelSource modelSource = new UrlModelSource( getClass().getResource( "standalone.xml" ) ); MavenProject project = projectBuilder.build( modelSource, request ).getProject(); project.setExecutionRoot( true ); return project; }