/** * Given a local path to a Maven project, this attempts to find the transitive dependencies of the * project. * * @param projectPath The path to search for Maven projects. * @param scopes The scopes to look up dependencies in. */ public String resolvePomDependencies(String projectPath, Set<String> scopes) { DefaultModelProcessor processor = new DefaultModelProcessor(); processor.setModelLocator(new DefaultModelLocator()); processor.setModelReader(new DefaultModelReader()); File pom = processor.locatePom(new File(projectPath)); FileModelSource pomSource = new FileModelSource(pom); // First resolve the model source locations. resolveSourceLocations(pomSource); // Next, fully resolve the models. Model model = modelResolver.getEffectiveModel(pomSource); if (model != null) { traverseDeps(model, scopes, Sets.newHashSet(), null); } return pom.getAbsolutePath(); }
@Override public ModelSource resolveModel(String groupId, String artifactId, String version) throws UnresolvableModelException { Artifact pomArtifact = new DefaultArtifact(groupId, artifactId, "", "pom", version); try { ArtifactRequest request = new ArtifactRequest(pomArtifact, repositories, null); pomArtifact = system.resolveArtifact(session, request).getArtifact(); } catch (org.eclipse.aether.resolution.ArtifactResolutionException ex) { throw new UnresolvableModelException(ex.getMessage(), groupId, artifactId, version, ex); } File pomFile = pomArtifact.getFile(); return new FileModelSource(pomFile); }
@Override public ModelSource2 resolveModel( final String groupId, final String artifactId, final String version) throws UnresolvableModelException { final Config config = Config.load(); final String localRepository = config.getMavenLocalRepository(); final String parent = ClassNameUtils.replace(groupId, ".", File.separator); final String path = Joiner.on(File.separator).join(localRepository, parent, artifactId, version); final String file = artifactId + '-' + version + ".pom"; final File pom = new File(path, file); final boolean exists = pom.exists(); if (!loaded.contains(pom)) { loaded.add(pom); } if (exists) { return new FileModelSource(pom); } return null; }
@Override public ModelSource2 resolveModel(final Parent parent) throws UnresolvableModelException { final String groupId = parent.getGroupId(); final String artifactId = parent.getArtifactId(); final String version = parent.getVersion(); final ModelSource2 model = resolveModel(groupId, artifactId, version); if (nonNull(model)) { return model; } String relativePath = parent.getRelativePath(); if (nonNull(relativePath) && !relativePath.isEmpty()) { File pom = new File(this.projectRoot, relativePath); if (!relativePath.endsWith("pom.xml")) { pom = new File(relativePath, "pom.xml"); } if (!loaded.contains(pom)) { loaded.add(pom); } if (pom.exists()) { return new FileModelSource(pom); } } return null; }
@Override public ModelSource resolveModel(String groupId, String artifactId, String version) throws UnresolvableModelException { Artifact pomArtifact = new DefaultArtifact(groupId, artifactId, "", "pom", version); try { final ArtifactRequest request = new ArtifactRequest(pomArtifact, repositories, null); pomArtifact = system.resolveArtifact(session, request).getArtifact(); } catch (ArtifactResolutionException e) { throw new UnresolvableModelException("Failed to resolve POM for " + groupId + ":" + artifactId + ":" + version + " due to " + e.getMessage(), groupId, artifactId, version, e); } final File pomFile = pomArtifact.getFile(); return new FileModelSource(pomFile); }
public ModelSource resolveModel( String groupId, String artifactId, String version ) throws UnresolvableModelException { Artifact pomArtifact = new DefaultArtifact( groupId, artifactId, "", "pom", version ); try { ArtifactRequest request = new ArtifactRequest( pomArtifact, repositories, context ); request.setTrace( trace ); pomArtifact = resolver.resolveArtifact( session, request ).getArtifact(); } catch ( ArtifactResolutionException e ) { throw new UnresolvableModelException( e.getMessage(), groupId, artifactId, version, e ); } File pomFile = pomArtifact.getFile(); return new FileModelSource( pomFile ); }
@Override public ModelSource resolveModel(String groupId, String artifactId, String version) throws UnresolvableModelException { LocalRepository localRepo = repositorySystemSession.getLocalRepository(); File localRepoDir = localRepo.getBasedir(); StringBuilder pomPath = new StringBuilder(); pomPath.append(groupId.replace('.', File.separatorChar)); pomPath.append(File.separatorChar); pomPath.append(artifactId); pomPath.append(File.separatorChar); pomPath.append(version); pomPath.append(File.separatorChar); pomPath.append(artifactId); pomPath.append('-'); pomPath.append(version); pomPath.append(".pom"); File pomFile = new File(localRepoDir, pomPath.toString()); if (pomFile.exists()) { return new FileModelSource(pomFile); } else { throw new UnresolvableModelException("POM does not exist in local repository: " + pomFile, groupId, artifactId, version); } }
/** Find the POM files for a given pom's parent(s) and submodules. */ private void resolveSourceLocations(FileModelSource fileModelSource) { Model model = modelResolver.getRawModel(fileModelSource); if (model == null) { return; } // Self. Parent parent = model.getParent(); if (model.getGroupId() == null) { model.setGroupId(parent.getGroupId()); } if (!modelResolver.putModelSource(model.getGroupId(), model.getArtifactId(), fileModelSource)) { return; } // Parent. File pomDirectory = new File(fileModelSource.getLocation()).getParentFile(); if (parent != null && !parent.getArtifactId().equals(model.getArtifactId())) { File parentPom; try { parentPom = new File(pomDirectory, parent.getRelativePath()).getCanonicalFile(); } catch (IOException e) { logger.warning( "Unable to get canonical path of " + pomDirectory + " and " + parent.getRelativePath()); return; } if (parentPom.exists()) { resolveSourceLocations(new FileModelSource(parentPom)); } } // Submodules. for (String module : model.getModules()) { resolveSourceLocations(new FileModelSource(new File(pomDirectory, module + "/pom.xml"))); } }
/** * {@inheritDoc} */ public ModelSource resolveModel( String groupId, String artifactId, String version ) { File pomFile = reactorModelPool.find(groupId, artifactId, version); if ( pomFile == null ) { Artifact pomArtifact = this.artifactFactory.createProjectArtifact( groupId, artifactId, version ); pomArtifact = this.localRepository.find( pomArtifact ); pomFile = pomArtifact.getFile(); } return new FileModelSource( pomFile ); }
public ModelSource resolveModel( String groupId, String artifactId, String version ) throws UnresolvableModelException { File pomFile = null; if ( modelPool != null ) { pomFile = modelPool.get( groupId, artifactId, version ); } if ( pomFile == null ) { Artifact pomArtifact = new DefaultArtifact( groupId, artifactId, "", "pom", version ); try { ArtifactRequest request = new ArtifactRequest( pomArtifact, repositories, context ); request.setTrace( trace ); pomArtifact = resolver.resolveArtifact( session, request ).getArtifact(); } catch ( ArtifactResolutionException e ) { throw new UnresolvableModelException( e.getMessage(), groupId, artifactId, version, e ); } pomFile = pomArtifact.getFile(); } return new FileModelSource( pomFile ); }
public ModelBuildingResult buildModel(File pomFile) { ModelBuildingResult result = null; DefaultModelBuildingRequest request = new DefaultModelBuildingRequest(modelBuildingRequestTemplate); request.setPomFile(pomFile); request.setModelSource(new FileModelSource(pomFile)); try { result = modelBuilder.build(request); } catch (ModelBuildingException e) { result = null; } return result; }
private void validate(ValidatorContext ctx, File pomFile) { DefaultModelBuildingRequest request = new DefaultModelBuildingRequest(modelBuildingRequestTemplate); request.setPomFile(pomFile); request.setModelSource(new FileModelSource(pomFile)); request.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MAVEN_3_0); try { modelBuilder.build(request); } catch (ModelBuildingException e) { ctx.addError(this, pomFile, e); } }
@Override public ModelSource resolveModel(String groupId, String artifactId, String version) throws UnresolvableModelException { Artifact artifact = new DefaultArtifact(groupId + ":" + artifactId + ":" + version); return new FileModelSource(Maven.getMetadata(system, session, artifact).getFile()); }
public ProjectBuildingResult build( File pomFile, ProjectBuildingRequest request ) throws ProjectBuildingException { return build( pomFile, new FileModelSource( pomFile ), new InternalConfig( request, null ) ); }
public ProjectBuildingResult build( Artifact artifact, boolean allowStubModel, ProjectBuildingRequest request ) throws ProjectBuildingException { org.sonatype.aether.artifact.Artifact pomArtifact = RepositoryUtils.toArtifact( artifact ); pomArtifact = ArtifactDescriptorUtils.toPomArtifact( pomArtifact ); InternalConfig config = new InternalConfig( request, null ); boolean localProject; try { ArtifactRequest pomRequest = new ArtifactRequest(); pomRequest.setArtifact( pomArtifact ); pomRequest.setRepositories( config.repositories ); ArtifactResult pomResult = repoSystem.resolveArtifact( config.session, pomRequest ); pomArtifact = pomResult.getArtifact(); localProject = pomResult.getRepository() instanceof WorkspaceRepository; } catch ( org.sonatype.aether.resolution.ArtifactResolutionException e ) { if ( e.getResults().get( 0 ).isMissing() && allowStubModel ) { return build( null, createStubModelSource( artifact ), config ); } throw new ProjectBuildingException( artifact.getId(), "Error resolving project artifact: " + e.getMessage(), e ); } File pomFile = pomArtifact.getFile(); if ( "pom".equals( artifact.getType() ) ) { artifact.selectVersion( pomArtifact.getVersion() ); artifact.setFile( pomFile ); artifact.setResolved( true ); } return build( localProject ? pomFile : null, new FileModelSource( pomFile ), config ); }