Example usage for org.apache.maven.project MavenProject getFile

List of usage examples for org.apache.maven.project MavenProject getFile

Introduction

In this page you can find the example usage for org.apache.maven.project MavenProject getFile.

Prototype

public File getFile() 

Source Link

Usage

From source file:org.eclipse.m2e.core.internal.M2EUtils.java

License:Open Source License

public static IFile getPomFile(MavenProject project) {
    //XXX copied from XmlUtils.extractProject()
    File file = new File(project.getFile().toURI());
    IPath path = Path.fromOSString(file.getAbsolutePath());
    Stack<IFile> stack = new Stack<IFile>();
    //here we need to find the most inner project to the path.
    //we do so by shortening the path and remembering all the resources identified.
    // at the end we pick the last one from the stack. is there a catch to it?
    IFile ifile = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path);
    if (ifile != null) {
        stack.push(ifile);//w w  w  . j  a v a2  s  .com
    }
    while (path.segmentCount() > 1) {
        IResource ires = ResourcesPlugin.getWorkspace().getRoot().findMember(path);
        if (ires != null && ires instanceof IFile) {
            stack.push((IFile) ires);
        }
        path = path.removeFirstSegments(1);
    }
    return stack.empty() ? null : stack.pop();
}

From source file:org.eclipse.m2e.core.internal.markers.MavenProblemInfo.java

License:Open Source License

public MavenProblemInfo(MavenProject mavenProject, LifecycleMappingConfigurationException error) {
    SourceLocation errorLocation = error.getLocation();
    if (errorLocation != null) {
        if (mavenProject.getFile().getAbsolutePath().equals(errorLocation.getResourcePath())) {
            this.location = new SourceLocation(errorLocation.getLineNumber(), errorLocation.getColumnStart(),
                    errorLocation.getColumnEnd());
        } else {//  w ww. java2  s . c om
            this.location = new SourceLocation(1, 1, 1, errorLocation);
        }
    } else {
        this.location = new SourceLocation(1, 0, 0);
    }
    this.message = error.getMessage();
    this.severity = IMarker.SEVERITY_ERROR;
}

From source file:org.eclipse.m2e.core.internal.markers.SourceLocationHelper.java

License:Open Source License

public static SourceLocation findLocation(MavenProject mavenProject, MojoExecutionKey mojoExecutionKey) {
    Plugin plugin = mavenProject//from w ww. j  av  a 2  s  .c o  m
            .getPlugin(mojoExecutionKey.getGroupId() + ":" + mojoExecutionKey.getArtifactId());

    InputLocation inputLocation = plugin.getLocation(SELF);
    if (inputLocation == null) {
        // Plugin is specified in the maven lifecycle definition, not explicit in current pom or parent pom
        inputLocation = mavenProject.getModel().getLocation(PACKAGING);
        if (inputLocation != null) {
            return new SourceLocation(inputLocation.getLineNumber(),
                    inputLocation.getColumnNumber() - PACKAGING.length() - COLUMN_START_OFFSET,
                    inputLocation.getColumnNumber() - COLUMN_END_OFFSET);
        }
        inputLocation = mavenProject.getModel().getLocation(SELF);
        return new SourceLocation(inputLocation.getLineNumber(),
                inputLocation.getColumnNumber() - PROJECT.length() - COLUMN_START_OFFSET,
                inputLocation.getColumnNumber() - COLUMN_END_OFFSET);
    }

    String elementName;
    InputLocation executionInputLocation = findExecutionLocation(plugin, mojoExecutionKey.getExecutionId());
    if (executionInputLocation != null) {
        inputLocation = executionInputLocation;
        elementName = EXECUTION;
    } else {
        elementName = PLUGIN;
    }

    File pomFile = mavenProject.getFile();
    if (pomFile.getAbsolutePath().equals(inputLocation.getSource().getLocation())) {
        // Plugin/execution is specified in current pom
        return new SourceLocation(inputLocation.getLineNumber(),
                inputLocation.getColumnNumber() - elementName.length() - COLUMN_START_OFFSET,
                inputLocation.getColumnNumber() - COLUMN_END_OFFSET);
    }

    // Plugin/execution is specified in some parent pom
    SourceLocation causeLocation = new SourceLocation(inputLocation.getSource().getLocation(),
            inputLocation.getSource().getModelId(), inputLocation.getLineNumber(),
            inputLocation.getColumnNumber() - elementName.length() - COLUMN_START_OFFSET,
            inputLocation.getColumnNumber() - COLUMN_END_OFFSET);
    inputLocation = mavenProject.getModel().getParent().getLocation(SELF);
    return new SourceLocation(inputLocation.getLineNumber(),
            inputLocation.getColumnNumber() - PARENT.length() - COLUMN_START_OFFSET,
            inputLocation.getColumnNumber() - COLUMN_END_OFFSET, causeLocation);
}

From source file:org.eclipse.m2e.core.internal.markers.SourceLocationHelper.java

License:Open Source License

public static SourceLocation findLocation(MavenProject mavenProject,
        org.apache.maven.model.Dependency dependency) {
    InputLocation inputLocation = null;/*from   ww w  .j ava2  s. c o  m*/
    if (dependency != null) {
        inputLocation = dependency.getLocation(SELF);
    }
    if (inputLocation == null) {
        // Should never happen
        inputLocation = mavenProject.getModel().getLocation(SELF);
        return new SourceLocation(inputLocation.getLineNumber(),
                inputLocation.getColumnNumber() - PROJECT.length() - COLUMN_START_OFFSET,
                inputLocation.getColumnNumber() - COLUMN_END_OFFSET);
    }

    File pomFile = mavenProject.getFile();
    if (pomFile.getAbsolutePath().equals(inputLocation.getSource().getLocation())) {
        // Dependency is specified in current pom
        return new SourceLocation(inputLocation.getLineNumber(),
                inputLocation.getColumnNumber() - DEPENDENCY.length() - COLUMN_START_OFFSET,
                inputLocation.getColumnNumber() - COLUMN_END_OFFSET);
    }

    // Plugin/execution is specified in some parent pom
    SourceLocation causeLocation = new SourceLocation(inputLocation.getSource().getLocation(),
            inputLocation.getSource().getModelId(), inputLocation.getLineNumber(),
            inputLocation.getColumnNumber() - DEPENDENCY.length() - COLUMN_START_OFFSET,
            inputLocation.getColumnNumber() - COLUMN_END_OFFSET);
    inputLocation = mavenProject.getModel().getParent().getLocation(SELF);
    return new SourceLocation(inputLocation.getLineNumber(),
            inputLocation.getColumnNumber() - PARENT.length() - COLUMN_START_OFFSET,
            inputLocation.getColumnNumber() - COLUMN_END_OFFSET, causeLocation);
}

From source file:org.eclipse.m2e.core.internal.project.ProjectCachePlunger.java

License:Open Source License

public void register(MavenProject project, Key cacheKey) {
    // project.file is null for parent pom.xml resolved from repositories
    File file = project.getFile();
    if (file != null) {
        projectKeys.put(file, cacheKey);
        keyProjects.put(cacheKey, file);
    }/*from  www  .  ja va2  s  . c  o m*/
}

From source file:org.eclipse.m2e.editor.xml.internal.lifecycle.LifecycleMappingDialog.java

License:Open Source License

public void selectionChanged(SelectionChangedEvent event) {
    MavenProject project = pomComposite.fromSelection();
    if (getButton(OK) != null) {
        getButton(OK).setEnabled(project != null && project.getFile() != null);
    }//from   w  w w .  j  a v a2  s .  co m
    updateStatus(project);
}

From source file:org.eclipse.m2e.editor.xml.internal.lifecycle.LifecycleMappingDialog.java

License:Open Source License

private void updateStatus(MavenProject project) {
    if (project.getFile() == null) {
        status.setText("Non-workspace pom");
        status.setImage(PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJS_ERROR_TSK));
    } else if (project.equals(pluginProject)) {
        status.setText("Plugin definition in selected pom.");
        status.setImage(null);//from w  w  w .  j ava 2s. co m
    } else {
        status.setText("");
        status.setImage(null);
    }
}

From source file:org.eclipse.m2e.editor.xml.internal.XmlUtils.java

License:Open Source License

/**
 * converts an InputLocation to a file path on the local disk, null if not available. still the input source's model
 * value can be used further../*from w  w w  .  j  a  v a2  s .c  om*/
 * 
 * @param location
 * @return
 */
public static File fileForInputLocation(InputLocation location, MavenProject origin) {
    InputSource source = location.getSource();
    if (source != null) {
        //MNGECLIPSE-2539 apparently if maven can't resolve the model from local storage,
        //the location will be empty. not only applicable to local repo models but
        //apparently also to models in workspace not reachable by relativePath 
        String loc = source.getLocation();
        File file = null;
        if (loc != null) {
            file = new File(loc);
        } else {
            //try to find pom by coordinates..
            String modelId = source.getModelId();
            if (origin.getModel().getId().equals(modelId) && origin.getFile() != null) {
                return origin.getFile();
            }
            String[] splitStrings = modelId.split(":");
            assert splitStrings.length == 3;
            IMavenProjectFacade facade = MavenPlugin.getMavenProjectRegistry().getMavenProject(splitStrings[0],
                    splitStrings[1], splitStrings[2]);
            if (facade != null) {
                file = facade.getPomFile();
            } else {
                //if not in the workspace, try looking into the local repository.
                IMaven maven = MavenPlugin.getMaven();
                try {
                    String path = maven.getArtifactPath(maven.getLocalRepository(), splitStrings[0],
                            splitStrings[1], splitStrings[2], "pom", null);
                    if (path != null) {
                        file = new File(maven.getLocalRepositoryPath(), path);
                    }
                } catch (CoreException e) {
                    log.error("Failed to calculate local repository path of artifact", e);
                }
            }
        }
        return file;
    }
    return null;
}

From source file:org.eclipse.m2e.refactoring.exclude.ExcludeArtifactRefactoring.java

License:Open Source License

public RefactoringStatus checkFinalConditions(IProgressMonitor pm)
        throws CoreException, OperationCanceledException {
    changes = new ArrayList<Change>();
    Set<ArtifactKey> locatedKeys = new HashSet<ArtifactKey>();
    List<IStatus> statuses = new ArrayList<IStatus>();
    SubMonitor monitor = SubMonitor.convert(pm, getHierarchy().size());

    List<Operation> exclusionOp = new ArrayList<Operation>();
    // Exclusion point
    Visitor visitor = locate(exclusionPoint, monitor.newChild(1));
    for (Entry<Dependency, Set<ArtifactKey>> entry : visitor.getSourceMap().entrySet()) {
        locatedKeys.addAll(entry.getValue());
        Dependency dependency = entry.getKey();
        if (contains(entry.getValue(), dependency)) {
            exclusionOp.add(new RemoveDependencyOperation(dependency));
        } else {//from   www  .  j av  a  2 s . c  o m
            for (ArtifactKey key : entry.getValue()) {
                if (!hasExclusion(exclusionPoint, dependency, key)) {
                    exclusionOp.add(new AddExclusionOperation(dependency, key));
                }
            }
        }
    }

    // Below exclusion point - pull up dependency to exclusion point
    for (MavenProject project : getDescendants()) {
        visitor = locate(project, monitor.newChild(1));
        List<Operation> operations = new ArrayList<Operation>();
        for (Entry<Dependency, Set<ArtifactKey>> entry : visitor.getSourceMap().entrySet()) {
            locatedKeys.addAll(entry.getValue());
            Dependency dependency = entry.getKey();
            operations.add(new RemoveDependencyOperation(dependency));
            if (!contains(entry.getValue(), dependency)) {
                if (!hasDependency(exclusionPoint, dependency)) {
                    exclusionOp.add(new AddDependencyOperation(dependency));
                }
                for (ArtifactKey key : entry.getValue()) {
                    if (!hasExclusion(exclusionPoint, dependency, key)) {
                        exclusionOp.add(new AddExclusionOperation(dependency, key));
                    }
                }
            }
        }
        if (operations.size() > 0) {
            IFile pom = getFile(project);
            changes.add(PomHelper.createChange(pom,
                    new CompoundOperation(operations.toArray(new Operation[operations.size()])), getName(pom)));
        }
    }

    // Above exclusion - Add dep to exclusionPoint
    for (MavenProject project : getAncestors()) {
        visitor = locate(project, monitor.newChild(1));
        for (Entry<Dependency, Set<ArtifactKey>> entry : locate(project, monitor.newChild(1)).getSourceMap()
                .entrySet()) {
            locatedKeys.addAll(entry.getValue());
            Dependency dependency = entry.getKey();
            if (contains(entry.getValue(), dependency)) {
                if (project.getFile() != null) {
                    statuses.add(new Status(IStatus.INFO, PLUGIN_ID,
                            NLS.bind(Messages.ExcludeArtifactRefactoring_removeDependencyFrom,
                                    toString(dependency),
                                    getMavenProjectFacade(project).getPom().getFullPath())));
                    IFile pom = getFile(project);
                    changes.add(PomHelper.createChange(getFile(project),
                            new RemoveDependencyOperation(dependency), getName(pom)));
                }
            } else {
                exclusionOp.add(new AddDependencyOperation(dependency));
                for (ArtifactKey key : entry.getValue()) {
                    if (!hasExclusion(exclusionPoint, dependency, key)) {
                        exclusionOp.add(new AddExclusionOperation(dependency, key));
                    }
                }
            }
        }
    }
    if (!exclusionOp.isEmpty()) {
        IFile pom = getFile(exclusionPoint);
        changes.add(PomHelper.createChange(pom,
                new CompoundOperation(exclusionOp.toArray(new Operation[exclusionOp.size()])), getName(pom)));
    }

    if (statuses.size() == 1) {
        return RefactoringStatus.create(statuses.get(0));
    } else if (statuses.size() > 1) {
        return RefactoringStatus
                .create(new MultiStatus(PLUGIN_ID, 0, statuses.toArray(new IStatus[statuses.size()]),
                        Messages.ExcludeArtifactRefactoring_errorCreatingRefactoring, null));
    } else if (locatedKeys.isEmpty()) {
        return RefactoringStatus.createFatalErrorStatus(Messages.ExcludeArtifactRefactoring_noTargets);
    } else if (locatedKeys.size() != keys.length) {
        StringBuilder sb = new StringBuilder();
        for (ArtifactKey key : keys) {
            if (!locatedKeys.contains(key)) {
                sb.append(key.toString()).append(',');
            }
        }
        sb.deleteCharAt(sb.length() - 1);
        return RefactoringStatus.createErrorStatus(
                NLS.bind(Messages.ExcludeArtifactRefactoring_failedToLocateArtifact, sb.toString()));
    }
    return new RefactoringStatus();
}

From source file:org.eclipse.m2e.refactoring.exclude.ExcludeWizardPage.java

License:Open Source License

private void updateStatusBar(MavenProject project) {
    if (project == null) {
        setStatus(Messages.ExcludeWizardPage_errorSelectPom);
        setPageComplete(false);/*w w w  .  j  a  v a2s. c  om*/
    } else if (project.getFile() == null) {
        setStatus(Messages.ExcludeWizardPage_errorNonWorkspacePom);
        setPageComplete(false);
    } else if ((project = isAboveDependencyManagement(project)) != null) {
        setStatus(NLS.bind(Messages.ExcludeWizardPage_dependenciesManagedIn, toString(project)));
        setPageComplete(false);
    } else {
        setStatus(null);
        setPageComplete(true);
    }
}