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

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

Introduction

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

Prototype

public Properties getProperties() 

Source Link

Usage

From source file:org.eclipse.m2e.core.internal.embedder.MavenProjectMutableState.java

License:Open Source License

public static MavenProjectMutableState takeSnapshot(MavenProject project) {
    MavenProjectMutableState snapshot = new MavenProjectMutableState();

    if (project.getContextValue(CTX_SNAPSHOT) == null) {
        snapshot.compileSourceRoots = new ArrayList<String>(project.getCompileSourceRoots());
        snapshot.testCompileSourceRoots = new ArrayList<String>(project.getTestCompileSourceRoots());
        snapshot.resources = new ArrayList<Resource>(project.getResources());
        snapshot.testResources = new ArrayList<Resource>(project.getTestResources());

        snapshot.properties = new Properties();
        snapshot.properties.putAll(project.getProperties());

        project.setContextValue(CTX_SNAPSHOT, Boolean.TRUE);
        snapshot.nested = false;/* w w  w .j a v  a  2  s  .  c  o  m*/
    }

    return snapshot;
}

From source file:org.eclipse.m2e.core.internal.embedder.MavenProjectMutableState.java

License:Open Source License

public void restore(MavenProject project) {
    if (nested) {
        return;/*from   ww w. ja  va2 s  . co  m*/
    }

    setElements(project.getCompileSourceRoots(), compileSourceRoots);
    setElements(project.getTestCompileSourceRoots(), testCompileSourceRoots);
    setElements(project.getResources(), resources);
    setElements(project.getTestResources(), testResources);

    if (properties != null) {
        project.getProperties().clear();
        project.getProperties().putAll(properties);
    }

    project.setContextValue(CTX_SNAPSHOT, null);
}

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

License:Open Source License

private void updateProjectConfiguration(final ProjectConfigurationRequest request, IProgressMonitor monitor)
        throws CoreException {
    final IProject project = request.getProject();
    long start = System.currentTimeMillis();
    final IMavenProjectFacade mavenProjectFacade = request.getMavenProjectFacade();
    log.debug("Updating project configuration for {}.", mavenProjectFacade.toString()); //$NON-NLS-1$

    addMavenNature(project, monitor);//from www. j ava 2  s . c o m

    // Configure project file encoding
    final MavenProject mavenProject = request.getMavenProject();
    Properties mavenProperties = mavenProject.getProperties();
    String sourceEncoding = mavenProperties.getProperty("project.build.sourceEncoding");
    log.debug("Setting encoding for project {}: {}", project.getName(), sourceEncoding); //$NON-NLS-1$
    project.setDefaultCharset(sourceEncoding, monitor);

    MavenExecutionContext executionContext = projectManager.createExecutionContext(mavenProjectFacade.getPom(),
            mavenProjectFacade.getResolverConfiguration());

    executionContext.execute(mavenProject, new ICallable<Void>() {
        public Void call(IMavenExecutionContext context, IProgressMonitor monitor) throws CoreException {
            ILifecycleMapping lifecycleMapping = getLifecycleMapping(mavenProjectFacade);

            if (lifecycleMapping != null) {
                mavenMarkerManager.deleteMarkers(mavenProjectFacade.getProject(),
                        IMavenConstants.MARKER_CONFIGURATION_ID);

                lifecycleMapping.configure(request, monitor);

                LifecycleMappingConfiguration.persist(request.getMavenProjectFacade(), monitor);
            } else {
                log.debug("LifecycleMapping is null for project {}", mavenProjectFacade.toString()); //$NON-NLS-1$
            }
            return null;
        }
    }, monitor);

    log.debug("Updated project configuration for {} in {} ms.", mavenProjectFacade.toString(), //$NON-NLS-1$
            System.currentTimeMillis() - start);
}

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

License:Open Source License

public static void addVersionProposal(final IProject project, final MavenProject mp, final Text groupIdText,
        final Text artifactIdText, final Text versionText, final Packaging packaging) {
    addCompletionProposal(versionText, new Searcher() {
        public Collection<String> search() throws CoreException {
            Collection<String> toRet = new ArrayList<String>();
            toRet.addAll(// w ww.j  av  a 2  s .  c om
                    getSearchEngine(project).findVersions(escapeQuerySpecialCharacters(groupIdText.getText()), //
                            escapeQuerySpecialCharacters(artifactIdText.getText()), "", packaging));
            if (mp != null) {
                //add version props now..
                Properties props = mp.getProperties();
                ArrayList<String> list = new ArrayList<String>();
                if (props != null) {
                    for (Object prop : props.keySet()) {
                        String propString = prop.toString();
                        if (propString.endsWith("Version") || propString.endsWith(".version")) { //$NON-NLS-1$//$NON-NLS-2$
                            list.add("${" + propString + "}"); //$NON-NLS-1$//$NON-NLS-2$
                        }
                    }
                }
                Collections.sort(list);
                toRet.addAll(list);
            }
            return toRet;
        }
    });
}

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

License:Open Source License

/**
 * copy pas//w  w w  . j  a va  2s .  c om
 * 
 * @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.PomContentAssistProcessor.java

License:Open Source License

/**
 * this is a proposal method for adding expressions when ${ is typed..
 * /*from ww w  . ja  v  a  2s.com*/
 * @param request
 * @param context
 * @param currentNode
 * @param prefix
 */
private void addExpressionProposal(ContentAssistRequest request, PomTemplateContext context, Node currentNode,
        String prefix) {
    int exprStart = prefix.lastIndexOf("${"); //$NON-NLS-1$
    if (exprStart != -1) {
        //the regular prefix is separated by whitespace and <> brackets only, we need to cut the last portion
        String realExpressionPrefix = prefix.substring(exprStart);
        if (realExpressionPrefix.contains("}")) { //$NON-NLS-1$
            //the expression is not opened..
            return;
        }
        if (expressionproposalContexts.contains(context)) {
            //add all effective pom expressions
            MavenProject prj = XmlUtils.extractMavenProject(sourceViewer);
            Region region = new Region(request.getReplacementBeginPosition() - realExpressionPrefix.length(),
                    realExpressionPrefix.length());
            Set<String> collect = new TreeSet<String>();
            if (prj != null) {
                Properties props = prj.getProperties();
                if (props != null) {
                    for (Object key : props.keySet()) {
                        String keyString = key.toString();
                        if (("${" + keyString).startsWith(realExpressionPrefix)) { //$NON-NLS-1$
                            collect.add(keyString);
                        }
                    }
                }
            }

            //add a few hardwired values as well
            if ("${basedir}".startsWith(realExpressionPrefix)) { //$NON-NLS-1$
                collect.add("basedir"); //$NON-NLS-1$
            }
            if ("${project.version}".startsWith(realExpressionPrefix)) { //$NON-NLS-1$
                collect.add("project.version"); //$NON-NLS-1$
            }
            if ("${project.groupId}".startsWith(realExpressionPrefix)) { //$NON-NLS-1$
                collect.add("project.groupId"); //$NON-NLS-1$
            }
            if ("${project.artifactId}".startsWith(realExpressionPrefix)) { //$NON-NLS-1$
                collect.add("project.artifactId"); //$NON-NLS-1$
            }
            if ("${project.build.directory}".startsWith(realExpressionPrefix)) { //$NON-NLS-1$
                collect.add("project.build.directory"); //$NON-NLS-1$
            }
            for (String key : collect) {
                ICompletionProposal proposal = new InsertExpressionProposal(region, key, prj);
                if (request.shouldSeparate()) {
                    request.addMacro(proposal);
                } else {
                    request.addProposal(proposal);
                }
            }
        }
    }
}

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

License:Open Source License

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;
        }/*  w w w.  j a  v  a 2 s  .co m*/
        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.wtp.WebProjectConfiguratorDelegate.java

License:Open Source License

/**
 * Get the context root from a maven web project
 * @param mavenProject//from w ww  .java 2  s  .co  m
 * @param warName 
 * @return the final name of the project if it exists, or the project's artifactId.
 */
protected String getContextRoot(MavenProject mavenProject, String warName) {
    String contextRoot;
    //MECLIPSEWTP-43 : Override with maven property
    String property = mavenProject.getProperties().getProperty(M2ECLIPSE_WTP_CONTEXT_ROOT);
    if (StringUtils.isBlank(property)) {
        String finalName = warName;
        if (StringUtils.isBlank(finalName)
                || finalName.equals(mavenProject.getArtifactId() + "-" + mavenProject.getVersion())) {
            contextRoot = mavenProject.getArtifactId();
        } else {
            contextRoot = finalName;
        }
    } else {
        contextRoot = property;
    }

    return contextRoot.trim().replace(" ", "_");
}

From source file:org.eclipse.scada.build.helper.target.UpdateMojo.java

License:Open Source License

protected String getVersion(final MavenProject project) {
    getLog().debug("Properties: " + project.getProperties());

    if (!project.getVersion().endsWith("-SNAPSHOT")) {
        // project version is already qualified
        return project.getVersion();
    }//from  w w  w. ja v  a2 s. c  o m

    final String version = project.getProperties().getProperty("qualifiedVersion");
    if (version != null) {
        // we do have a direct qualified version
        return version;
    }

    final String q = project.getProperties().getProperty("buildQualifier");
    final String v = project.getProperties().getProperty("unqualifiedVersion");

    if (q == null || v == null) {
        throw new IllegalStateException(
                String.format("Unable to find qualified version for: %s", project.getArtifactId()));
    }

    // just stick it together
    return v + "." + q;
}

From source file:org.eclipse.tycho.core.maven.TychoInterpolator.java

License:Open Source License

public TychoInterpolator(MavenSession mavenSession, MavenProject mavenProject) {
    final Properties baseProps = new Properties();
    // The order how the properties been added is important! 
    // It defines which properties win over others 
    // (session user properties overwrite system properties overwrite project properties)
    baseProps.putAll(mavenProject.getProperties());
    baseProps.putAll(mavenSession.getSystemProperties());
    baseProps.putAll(mavenSession.getUserProperties());

    final Settings settings = mavenSession.getSettings();

    // roughly match resources plugin behaviour
    // Using the project and settings as object value source to get things replaces like
    // ${project.artifactId}...;
    // Simple string replacement for ${localRepository}, ${version}, ${basedir};
    // An and string replacement for all property values
    // (session user properties, system properties, project properties).

    interpolator = new StringSearchInterpolator();
    interpolator.addValueSource(new PrefixedObjectValueSource("project", mavenProject));
    interpolator.addValueSource(new PrefixedObjectValueSource("settings", settings));
    interpolator/*  ww w .  ja v a 2 s. co m*/
            .addValueSource(new SingleResponseValueSource("localRepository", settings.getLocalRepository()));
    interpolator.addValueSource(new SingleResponseValueSource("version", mavenProject.getVersion()));
    interpolator.addValueSource(
            new SingleResponseValueSource("basedir", mavenProject.getBasedir().getAbsolutePath()));
    interpolator.addValueSource(new ValueSource() {
        @Override
        public Object getValue(String expression) {
            return baseProps.getProperty(expression);
        }

        @Override
        public void clearFeedback() {
        }

        @Override
        @SuppressWarnings("rawtypes")
        public List getFeedback() {
            return Collections.EMPTY_LIST;
        }
    });
}