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

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

Introduction

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

Prototype

public Model getModel() 

Source Link

Usage

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 2s.c  om*/
            .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;/*w w w  .  ja  v a  2s  . c  om*/
    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.ui.internal.dialogs.MavenRepositorySearchDialog.java

License:Open Source License

/**
 * @param parent//  w  ww  . j  a va  2s . c o m
 * @param title
 * @param mp
 * @param p
 * @param inManagedSection true when the result will be added to the dependencyManagement section of the pom.
 * @return
 */
public static MavenRepositorySearchDialog createSearchDependencyDialog(Shell parent, String title,
        MavenProject mp, IProject p, boolean inManagedSection) {
    Set<ArtifactKey> artifacts = new HashSet<ArtifactKey>();
    Set<ArtifactKey> managed = new HashSet<ArtifactKey>();
    if (mp != null) {
        Set<ArtifactKey> keys = inManagedSection ? artifacts : managed;
        DependencyManagement dm = mp.getDependencyManagement();
        if (dm != null && dm.getDependencies() != null) {
            for (Dependency dep : dm.getDependencies()) {
                keys.add(new ArtifactKey(dep.getGroupId(), dep.getArtifactId(), dep.getVersion(),
                        dep.getClassifier()));
            }
        }
        if (!inManagedSection) {
            for (Dependency dep : mp.getModel().getDependencies()) {
                artifacts.add(new ArtifactKey(dep.getGroupId(), dep.getArtifactId(), dep.getVersion(),
                        dep.getClassifier()));
            }
        }
    }
    return new MavenRepositorySearchDialog(parent, title, IIndex.SEARCH_ARTIFACT, artifacts, managed, true, mp,
            p, true);
}

From source file:org.eclipse.m2e.core.ui.internal.dialogs.MavenRepositorySearchDialog.java

License:Open Source License

/**
 * @param parent//from  w w w  . j  a  v  a2s.  c o m
 * @param title
 * @param mp
 * @param p
 * @return
 */
public static MavenRepositorySearchDialog createSearchParentDialog(Shell parent, String title, MavenProject mp,
        IProject p) {
    Set<ArtifactKey> artifacts = new HashSet<ArtifactKey>();
    Set<ArtifactKey> managed = new HashSet<ArtifactKey>();
    if (mp != null && mp.getModel().getParent() != null) {
        Parent par = mp.getModel().getParent();
        artifacts.add(new ArtifactKey(par.getGroupId(), par.getArtifactId(), par.getVersion(), null));
    }
    return new MavenRepositorySearchDialog(parent, title, IIndex.SEARCH_PARENTS, artifacts, managed, false, mp,
            p, true);
}

From source file:org.eclipse.m2e.core.ui.internal.dialogs.MavenRepositorySearchDialog.java

License:Open Source License

/**
 * @param parent/*from www.j  a v  a 2  s  .  c  o m*/
 * @param title
 * @param mp
 * @param p
 * @param inManagedSection true when the result will be added to the dependencyManagement section of the pom.
 * @return
 */
public static MavenRepositorySearchDialog createSearchPluginDialog(Shell parent, String title, MavenProject mp,
        IProject p, boolean inManagedSection) {
    Set<ArtifactKey> artifacts = new HashSet<ArtifactKey>();
    Set<ArtifactKey> managed = new HashSet<ArtifactKey>();
    Set<ArtifactKey> keys = inManagedSection ? artifacts : managed;
    if (mp != null && mp.getBuild() != null) {
        PluginManagement pm = mp.getBuild().getPluginManagement();
        if (pm != null && pm.getPlugins() != null) {
            for (Plugin plug : pm.getPlugins()) {
                keys.add(new ArtifactKey(plug.getGroupId(), plug.getArtifactId(), plug.getVersion(), null));
            }
        }
        if (!inManagedSection && mp.getModel().getBuild() != null) {
            for (Plugin plug : mp.getModel().getBuild().getPlugins()) {
                artifacts
                        .add(new ArtifactKey(plug.getGroupId(), plug.getArtifactId(), plug.getVersion(), null));
            }
        }

    }
    return new MavenRepositorySearchDialog(parent, title, IIndex.SEARCH_PLUGIN, artifacts, managed, false, mp,
            p, true);
}

From source file:org.eclipse.m2e.core.ui.internal.util.ParentGatherer.java

License:Open Source License

/**
 * Return the list of parents for a give pom
 * // w  ww  .j a v a  2 s .co  m
 * @param monitor
 * @return list of {@link MavenProject} from the given project to its ultimate parent. The first entry is the given
 *         pom, the last one the ultimate parent.
 * @throws CoreException
 */
public LinkedList<MavenProject> getParentHierarchy(final IProgressMonitor monitor) throws CoreException {
    final LinkedList<MavenProject> hierarchy = new LinkedList<MavenProject>();
    final IMaven maven = MavenPlugin.getMaven();
    IMavenProjectRegistry projectManager = MavenPlugin.getMavenProjectRegistry();
    maven.detachFromSession(mavenProject);

    hierarchy.add(mavenProject);

    projectManager.execute(projectFacade, new ICallable<Void>() {
        public Void call(IMavenExecutionContext context, IProgressMonitor monitor) throws CoreException {
            MavenProject project = mavenProject;
            while (project.getModel().getParent() != null) {
                if (monitor.isCanceled()) {
                    return null;
                }
                project = maven.resolveParentProject(project, monitor);
                hierarchy.add(project);
            }
            return null;
        }
    }, monitor);

    return hierarchy;
}

From source file:org.eclipse.m2e.editor.composites.ParentGatherer.java

License:Open Source License

/**
 * Return the list of parents for a give pom
 * @param monitor/* w  w w .j  av a2  s  . c  o m*/
 * @return list of {@link MavenProject} from the given project to its ultimate parent. 
 * The first entry is the given pom, the last one the ultimate parent.
 * @throws CoreException
 */
public LinkedList<MavenProject> getParentHierarchy(IProgressMonitor monitor) throws CoreException {
    LinkedList<MavenProject> hierarchy = new LinkedList<MavenProject>();
    IMaven maven = MavenPlugin.getDefault().getMaven();
    MavenProjectManager projectManager = MavenPlugin.getDefault().getMavenProjectManager();
    maven.detachFromSession(mavenProject);

    hierarchy.add(mavenProject);

    MavenProject project = mavenProject;
    while (project.getModel().getParent() != null) {
        if (monitor.isCanceled()) {
            return null;
        }
        MavenExecutionRequest request = projectManager.createExecutionRequest(projectFacade, monitor);
        project = maven.resolveParentProject(request, project, monitor);
        hierarchy.add(project);
    }
    return hierarchy;
}

From source file:org.eclipse.m2e.editor.pom.FormUtils.java

License:Open Source License

/**
 * copy pas//from ww w. java2s . c o m
 * 
 * @param project
 * @param text
 * @return
 */
//TODO copy pasted from PomTemplateContext
static String simpleInterpolate(MavenProject project, String text) {
    if (text != null && text.contains("${")) { //$NON-NLS-1$
        //when expression is in the version but no project instance around
        // just give up.
        if (project == null) {
            return null;
        }
        Properties props = project.getProperties();
        RegexBasedInterpolator inter = new RegexBasedInterpolator();
        if (props != null) {
            inter.addValueSource(new PropertiesBasedValueSource(props));
        }
        inter.addValueSource(new PrefixedObjectValueSource(Arrays.asList(new String[] { "pom.", "project." }), //$NON-NLS-1$//$NON-NLS-2$
                project.getModel(), false));
        try {
            text = inter.interpolate(text);
        } catch (InterpolationException e) {
            text = null;
        }
    }
    return text;
}

From source file:org.eclipse.m2e.editor.xml.InsertExpressionProposal.java

License:Open Source License

public Object getAdditionalProposalInfo(IProgressMonitor monitor) {
    if (project == null) {
        return null;
    }//  w  w  w . ja v  a  2  s .co m
    String value = PomTemplateContext.simpleInterpolate(project, "${" + key + "}"); //$NON-NLS-1$ //$NON-NLS-2$
    MavenProject mavprj = project;
    String loc = null;
    if (mavprj != null) {
        Model mdl = mavprj.getModel();
        if (mdl.getProperties() != null && mdl.getProperties().containsKey(key)) {
            if (mdl.getLocation("properties") != null) {
                InputLocation location = mdl.getLocation("properties").getLocation(key); //$NON-NLS-1$
                if (location != null) {
                    //MNGECLIPSE-2539 apparently you can have an InputLocation with null input source.
                    // check!
                    InputSource source = location.getSource();
                    if (source != null) {
                        loc = source.getModelId();
                    }
                }
            }
        }
    }
    StringBuffer buff = new StringBuffer();
    buff.append("<html>"); //$NON-NLS-1$
    if (value != null) {
        buff.append(NLS.bind(Messages.InsertExpressionProposal_hint1, value));
    }
    if (loc != null) {
        buff.append(NLS.bind(Messages.InsertExpressionProposal_hint2, loc));
    }
    buff.append("</html>"); //$NON-NLS-1$
    return buff.toString();
}

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  ava  2 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;
}