Skip to content

Support to search about pom dependency #170

@cmoulliard

Description

@cmoulliard

Proposition

The project currently supports to search about java symbols: import, annotation, class, method, field and could be also extended to support to search about pom dependency, plugin, properties, etc

The main benefit to include such a search capability part of this project will be that the query language of the Rule could be simplified for the user to avoid to use XPath queries and buildin parser

when:
  condition: pom.dependency is (gav='g:a:v,g:a:v,g:a:v,...')
  condition: pom.dependency is (groupId='g', artifactIr='a', version='v')

and to leverage too from the language server to perform queries the analyzing phase.

The development of such queries are not too complex as we could use from the pom.xml loaded by the server

        DefaultModelBuildingRequest req = new DefaultModelBuildingRequest();
        req.setProcessPlugins(false);
        req.setPomFile(new File(pomPath));
        req.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL);
        req.setSystemProperties(System.getProperties());

        req.setModelResolver(new RepositoryModelResolver());

        ModelBuildingResult result;
        try {
            result = modelBuilder.build(req);
        } catch (Exception e) {
            System.out.println("Could not build effective model: " + e.getMessage());
            return Optional.empty();
        }

       // Then we can iterate and search over the model 
      Model model = result.getEffectiveModel();
      public Optional searchDependency(Model model, String groupId, String artifactId, String version) {
        if (model.getDependencyManagement() != null) {
            Optional<Dependency> dep = model.getDependencyManagement().getDependencies().stream()
                .filter(d -> groupId.equals(d.getGroupId()) && artifactId.equals(d.getArtifactId()))
                .findFirst();

            if (dep.isPresent()) {
                // !!! Location is empty
                return Optional.ofNullable(dep.get().getLocation(""));
            }
        }

        if (model.getDependencies() != null) {
            Optional<Dependency> dep = model.getDependencies().stream()
                .filter(d -> groupId.equals(d.getGroupId()) && artifactId.equals(d.getArtifactId()))
                .findFirst();

            if (dep.isPresent()) {
                // Found it!
                return Optional.ofNullable(dep.get().getLocation(""));
            }
        }
        return Optional.empty();
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/api-changeCategorizes issue or PR as related to adding, removing, or otherwise changing an APIkind/featureCategorizes issue or PR as related to a new feature.priority/backlogHigher priority than priority/awaiting-more-evidence.triage/acceptedIndicates an issue or PR is ready to be actively worked on.triage/needs-informationIndicates an issue needs more information in order to work on it.

    Type

    No type

    Projects

    Status

    🔖 Ready

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions