org.apache.maven.model.building.ModelProblem Java Examples

The following examples show how to use org.apache.maven.model.building.ModelProblem. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ModelUtils.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public static boolean checkByCLIMavenValidationLevel(ModelProblem problem) {
    // XXX HACK - this should be properly solved by upgrading the embeded maven
    String version = MavenSettings.getCommandLineMavenVersion();        
    try {
        if ( version != null && !"".equals(version.trim()) && 
             new DefaultArtifactVersion(version).compareTo(new DefaultArtifactVersion("3.2.1")) > 0) 
        {
            if ( (problem.getMessage().startsWith("'dependencies.dependency.exclusions.exclusion.groupId' for ") ||
                  problem.getMessage().startsWith("'dependencies.dependency.exclusions.exclusion.artifactId' for "))
                    && problem.getMessage().contains(" with value '*' does not match a valid id pattern")) 
            {
                return false;
            }
        }
    } catch (Throwable e) {
        // ignore and be optimistic about the hint
        LOG.log(Level.INFO, version, e);
    }
    return true;
}
 
Example #2
Source File: EffectivePomMD.java    From netbeans with Apache License 2.0 5 votes vote down vote up
static List<ModelProblem> runMavenValidationImpl(final File pom) {
    //TODO profiles based on current configuration??
    MavenEmbedder embedder = EmbedderFactory.getProjectEmbedder();
    MavenExecutionRequest meReq = embedder.createMavenExecutionRequest();
    ProjectBuildingRequest req = meReq.getProjectBuildingRequest();
    req.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MAVEN_3_1); // currently enables just <reporting> warning
    req.setLocalRepository(embedder.getLocalRepository());
    List<ArtifactRepository> remoteRepos = RepositoryPreferences.getInstance().remoteRepositories(embedder);
    req.setRemoteRepositories(remoteRepos);
    req.setRepositorySession(((DefaultMaven) embedder.lookupComponent(Maven.class)).newRepositorySession(meReq));
    List<ModelProblem> problems;
    try {
        problems = embedder.lookupComponent(ProjectBuilder.class).build(pom, req).getProblems();
    } catch (ProjectBuildingException x) {
        problems = new ArrayList<ModelProblem>();
        List<ProjectBuildingResult> results = x.getResults();
        if (results != null) { //one code point throwing ProjectBuildingException contains results,
            for (ProjectBuildingResult result : results) {
                problems.addAll(result.getProblems());
            }
        } else {
            // another code point throwing ProjectBuildingException doesn't contain results..
            Throwable cause = x.getCause();
            if (cause instanceof ModelBuildingException) {
                problems.addAll(((ModelBuildingException) cause).getProblems());
            }
        }
    }
    return problems;
}
 
Example #3
Source File: POMModelPanel.java    From netbeans with Apache License 2.0 5 votes vote down vote up
static Node createErrorNode(ModelBuildingException x) {
    AbstractNode an = new AbstractNode(Children.LEAF);
    StringBuilder b = new StringBuilder();
    for (ModelProblem p : x.getProblems()) {
        if (b.length() > 0) {
            b.append("; ");
        }
        b.append(p.getMessage());
    }
    an.setDisplayName(b.toString());
    return an;
}
 
Example #4
Source File: StatusProvider.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static void runMavenValidation(final POMModel model, final List<ErrorDescription> err) {
    File pom = model.getModelSource().getLookup().lookup(File.class);
    if (pom == null) {
        return;
    }
    
    List<ModelProblem> problems = runMavenValidationImpl(pom);
    for (ModelProblem problem : problems) {
        if (!problem.getSource().equals(pom.getAbsolutePath())) {
            LOG.log(Level.FINE, "found problem not in {0}: {1}", new Object[] {pom, problem.getSource()});
            continue;
        }
        int line = problem.getLineNumber();
        if (line <= 0) { // probably from a parent POM
            /* probably more irritating than helpful:
            line = 1; // fallback
            Parent parent = model.getProject().getPomParent();
            if (parent != null) {
                Line l = NbEditorUtilities.getLine(model.getBaseDocument(), parent.findPosition(), false);
                if (l != null) {
                    line = l.getLineNumber() + 1;
                }
            }
            */
            continue;
        }
        if (problem.getException() instanceof UnresolvableModelException) {
            // If a <parent> reference cannot be followed because e.g. no projects are opened (so no repos registered), just ignore it.
            continue;
        }
        try {
            err.add(ErrorDescriptionFactory.createErrorDescription(problem.getSeverity() == ModelProblem.Severity.WARNING ? Severity.WARNING : Severity.ERROR, problem.getMessage(), model.getBaseDocument(), line));
        } catch (IndexOutOfBoundsException x) {
            LOG.log(Level.WARNING, "improper line number: {0}", problem);
        }
    }
    
}
 
Example #5
Source File: StatusProvider.java    From netbeans with Apache License 2.0 5 votes vote down vote up
static List<ModelProblem> runMavenValidationImpl(final File pom) {
    MavenEmbedder embedder = EmbedderFactory.getProjectEmbedder();
    MavenExecutionRequest meReq = embedder.createMavenExecutionRequest();
    ProjectBuildingRequest req = meReq.getProjectBuildingRequest();
    req.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MAVEN_3_0); // 3.1 currently enables just <reporting> warning, see issue 223562 for details on why it's bad to show.
    req.setLocalRepository(embedder.getLocalRepository());
    List<ArtifactRepository> remoteRepos = RepositoryPreferences.getInstance().remoteRepositories(embedder);
    req.setRemoteRepositories(remoteRepos);
    req.setRepositorySession(((DefaultMaven) embedder.lookupComponent(Maven.class)).newRepositorySession(meReq));
    List<ModelProblem> problems;
    try {
        problems = embedder.lookupComponent(ProjectBuilder.class).build(pom, req).getProblems();
    } catch (ProjectBuildingException x) {
        problems = new ArrayList<ModelProblem>();
        List<ProjectBuildingResult> results = x.getResults();
        if (results != null) { //one code point throwing ProjectBuildingException contains results,
            for (ProjectBuildingResult result : results) {
                problems.addAll(result.getProblems());
            }
        } else {
            // another code point throwing ProjectBuildingException doesn't contain results..
            Throwable cause = x.getCause();
            if (cause instanceof ModelBuildingException) {
                problems.addAll(((ModelBuildingException) cause).getProblems());
            }
        }
    }
    List<ModelProblem> toRet = new LinkedList<ModelProblem>();
    for (ModelProblem problem : problems) {
        if(ModelUtils.checkByCLIMavenValidationLevel(problem)) {
            toRet.add(problem);
        }
    }
    return toRet;
}
 
Example #6
Source File: ProjectHelper.java    From furnace with Eclipse Public License 1.0 5 votes vote down vote up
public Model loadPomFromFile(File pomFile, String... profiles)
{
   RepositorySystem system = mavenContainer.getRepositorySystem();
   Settings settings = mavenContainer.getSettings();
   DefaultRepositorySystemSession session = mavenContainer.setupRepoSession(system, settings);
   final DefaultModelBuildingRequest request = new DefaultModelBuildingRequest()
            .setSystemProperties(System.getProperties())
            .setPomFile(pomFile)
            .setActiveProfileIds(settings.getActiveProfiles());
   ModelBuilder builder = new DefaultModelBuilderFactory().newInstance();
   ModelBuildingResult result;
   try
   {
      request.setModelResolver(new MavenModelResolver(system, session,
               MavenRepositories.getRemoteRepositories(mavenContainer, settings)));
      result = builder.build(request);
   }
   // wrap exception message
   catch (ModelBuildingException e)
   {
      String pomPath = request.getPomFile().getAbsolutePath();
      StringBuilder sb = new StringBuilder("Found ").append(e.getProblems().size())
               .append(" problems while building POM model from ").append(pomPath).append("\n");

      int counter = 1;
      for (ModelProblem problem : e.getProblems())
      {
         sb.append(counter++).append("/ ").append(problem).append("\n");
      }

      throw new RuntimeException(sb.toString());
   }
   return result.getEffectiveModel();
}
 
Example #7
Source File: MavenModelProblemsProvider.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@NbBundle.Messages({
    "TXT_Artifact_Resolution_problem=Artifact Resolution problem",
    "TXT_Artifact_Not_Found=Artifact Not Found",
    "TXT_Cannot_Load_Project=Unable to properly load project",
    "TXT_Cannot_read_model=Error reading project model",
    "TXT_NoMsg=Exception thrown while loading maven project at {0}. See messages.log for more information."
})
private Collection<ProjectProblem> reportExceptions(MavenExecutionResult res) {
    List<ProjectProblem> toRet = new ArrayList<ProjectProblem>();
    for (Throwable e : res.getExceptions()) {
        LOG.log(Level.FINE, "Error on loading project " + project.getProjectDirectory(), e);
        if (e instanceof ArtifactResolutionException) { // XXX when does this occur?
            toRet.add(ProjectProblem.createError(TXT_Artifact_Resolution_problem(), getDescriptionText(e)));
            problemReporter.addMissingArtifact(((ArtifactResolutionException) e).getArtifact());
            
        } else if (e instanceof ArtifactNotFoundException) { // XXX when does this occur?
            toRet.add(ProjectProblem.createError(TXT_Artifact_Not_Found(), getDescriptionText(e)));
            problemReporter.addMissingArtifact(((ArtifactNotFoundException) e).getArtifact());
        } else if (e instanceof ProjectBuildingException) {
            toRet.add(ProjectProblem.createError(TXT_Cannot_Load_Project(), getDescriptionText(e), new SanityBuildAction(project)));
            if (e.getCause() instanceof ModelBuildingException) {
                ModelBuildingException mbe = (ModelBuildingException) e.getCause();
                for (ModelProblem mp : mbe.getProblems()) {
                    LOG.log(Level.FINE, mp.toString(), mp.getException());
                    if (mp.getException() instanceof UnresolvableModelException) {
                        // Probably obsoleted by ProblemReporterImpl.checkParent, but just in case:
                        UnresolvableModelException ume = (UnresolvableModelException) mp.getException();
                        problemReporter.addMissingArtifact(EmbedderFactory.getProjectEmbedder().createProjectArtifact(ume.getGroupId(), ume.getArtifactId(), ume.getVersion()));
                    } else if (mp.getException() instanceof PluginResolutionException) {
                        Plugin plugin = ((PluginResolutionException) mp.getException()).getPlugin();
                        // XXX this is not actually accurate; should rather pick out the ArtifactResolutionException & ArtifactNotFoundException inside
                        problemReporter.addMissingArtifact(EmbedderFactory.getProjectEmbedder().createArtifact(plugin.getGroupId(), plugin.getArtifactId(), plugin.getVersion(), "jar"));
                    } else if (mp.getException() instanceof PluginManagerException) {
                        PluginManagerException ex = (PluginManagerException) mp.getException();                            
                        problemReporter.addMissingArtifact(EmbedderFactory.getProjectEmbedder().createArtifact(ex.getPluginGroupId(), ex.getPluginArtifactId(), ex.getPluginVersion(), "jar"));
                    }
                }
            }
        } else {
            String msg = e.getMessage();
            if(msg != null) {
                LOG.log(Level.INFO, "Exception thrown while loading maven project at " + project.getProjectDirectory(), e); //NOI18N
                toRet.add(ProjectProblem.createError(TXT_Cannot_read_model(), msg));
            } else {
                String path = project.getProjectDirectory().getPath();
                toRet.add(ProjectProblem.createError(TXT_Cannot_read_model(), TXT_NoMsg(path)));
                LOG.log(Level.WARNING, "Exception thrown while loading maven project at " + path, e); //NOI18N
            }
        }
    }
    return toRet;
}