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.sonar.plugins.maven.MavenProjectConverter.java

License:Open Source License

@VisibleForTesting
static void merge(MavenProject pom, ProjectDefinition definition) {
    String key = getSonarKey(pom);
    // IMPORTANT NOTE : reference on properties from POM model must not be saved,
    // instead they should be copied explicitly - see SONAR-2896
    definition.setProperties(pom.getModel().getProperties()).setKey(key).setVersion(pom.getVersion())
            .setName(pom.getName()).setDescription(pom.getDescription()).addContainerExtension(pom);
    guessJavaVersion(pom, definition);//  ww  w.  ja  v a2  s. c  o m
    guessEncoding(pom, definition);
    convertMavenLinksToProperties(definition, pom);
    synchronizeFileSystem(pom, definition);
}

From source file:org.sonarsource.scanner.maven.bootstrap.MavenPlugin.java

License:Open Source License

/**
 * Returns a plugin from a pom based on its group id and artifact id
 * <p>//from  w  w w.  j ava  2 s. co m
 * It searches in the build section, then the reporting section and finally the pluginManagement section
 * </p>
 *
 * @param pom the project pom
 * @param groupId the plugin group id
 * @param artifactId the plugin artifact id
 * @return the plugin if it exists, null otherwise
 */
@CheckForNull
public static MavenPlugin getPlugin(MavenProject pom, String groupId, String artifactId) {
    Object pluginConfiguration = null;

    // look for plugin in <build> section
    Plugin plugin = getPlugin(pom.getBuildPlugins(), groupId, artifactId);

    if (plugin != null) {
        pluginConfiguration = plugin.getConfiguration();
    } else {
        // look for plugin in reporting
        Reporting reporting = pom.getModel().getReporting();
        if (reporting != null) {
            ReportPlugin reportPlugin = getReportPlugin(reporting.getPlugins(), groupId, artifactId);
            if (reportPlugin != null) {
                pluginConfiguration = reportPlugin.getConfiguration();
            }
        }
    }

    // look for plugin in <pluginManagement> section
    PluginManagement pluginManagement = pom.getPluginManagement();
    if (pluginManagement != null) {
        Plugin pluginFromManagement = getPlugin(pluginManagement.getPlugins(), groupId, artifactId);
        if (pluginFromManagement != null) {
            Object pluginConfigFromManagement = pluginFromManagement.getConfiguration();
            if (pluginConfiguration == null) {
                pluginConfiguration = pluginConfigFromManagement;
            } else if (pluginConfigFromManagement != null) {
                Xpp3Dom.mergeXpp3Dom((Xpp3Dom) pluginConfiguration, (Xpp3Dom) pluginConfigFromManagement);
            }
        }
    }

    if (pluginConfiguration != null) {
        return new MavenPlugin(pluginConfiguration);
    }
    return null;

}

From source file:org.sonarsource.scanner.maven.bootstrap.MavenProjectConverter.java

License:Open Source License

private void configureModules(List<MavenProject> mavenProjects, Map<MavenProject, Properties> propsByModule)
        throws IOException, MojoExecutionException {
    for (MavenProject pom : mavenProjects) {
        boolean skipped = "true".equals(pom.getModel().getProperties().getProperty("sonar.skip"));
        if (skipped) {
            log.info("Module " + pom + " skipped by property 'sonar.skip'");
            continue;
        }/*from ww w  . ja va  2 s .com*/
        Properties props = new Properties();
        merge(pom, props);
        propsByModule.put(pom, props);
    }
}

From source file:org.sonarsource.scanner.maven.bootstrap.MavenProjectConverter.java

License:Open Source License

private static void defineProjectKey(MavenProject pom, Properties props) {
    String key;//from  ww  w  .  j a v  a2  s .  c  o  m
    if (pom.getModel().getProperties().containsKey(ScanProperties.PROJECT_KEY)) {
        key = pom.getModel().getProperties().getProperty(ScanProperties.PROJECT_KEY);
    } else {
        key = getSonarKey(pom);
    }
    props.setProperty(MODULE_KEY, key);
}

From source file:org.sonarsource.scanner.maven.bootstrap.MavenProjectConverter.java

License:Open Source License

private void synchronizeFileSystemAndOtherProps(MavenProject pom, Properties props)
        throws MojoExecutionException {
    props.setProperty(ScanProperties.PROJECT_BASEDIR, pom.getBasedir().getAbsolutePath());
    File buildDir = getBuildDir(pom);
    if (buildDir != null) {
        props.setProperty(PROPERTY_PROJECT_BUILDDIR, buildDir.getAbsolutePath());
        props.setProperty(ScannerProperties.WORK_DIR, getSonarWorkDir(pom).getAbsolutePath());
    }//w w  w.j  a  v a 2 s .c o  m
    populateBinaries(pom, props);

    populateLibraries(pom, props, false);
    populateLibraries(pom, props, true);

    populateSurefireReportsPath(pom, props);

    // IMPORTANT NOTE : reference on properties from POM model must not be saved,
    // instead they should be copied explicitly - see SONAR-2896
    for (String k : pom.getModel().getProperties().stringPropertyNames()) {
        props.put(k, pom.getModel().getProperties().getProperty(k));
    }

    props.putAll(envProperties);

    // Add user properties (ie command line arguments -Dsonar.xxx=yyyy) in last position to
    // override all other
    props.putAll(userProperties);

    List<File> mainDirs = mainSources(pom);
    props.setProperty(ScanProperties.PROJECT_SOURCE_DIRS, StringUtils.join(toPaths(mainDirs), SEPARATOR));
    List<File> testDirs = testSources(pom);
    if (!testDirs.isEmpty()) {
        props.setProperty(ScanProperties.PROJECT_TEST_DIRS, StringUtils.join(toPaths(testDirs), SEPARATOR));
    } else {
        props.remove(ScanProperties.PROJECT_TEST_DIRS);
    }
}

From source file:org.sonarsource.scanner.maven.bootstrap.MavenProjectConverter.java

License:Open Source License

private List<File> mainSources(MavenProject pom) throws MojoExecutionException {
    Set<String> sources = new LinkedHashSet<>();
    if (MAVEN_PACKAGING_WAR.equals(pom.getModel().getPackaging())) {
        sources.add(MavenUtils.getPluginSetting(pom, MavenUtils.GROUP_ID_APACHE_MAVEN,
                ARTIFACTID_MAVEN_WAR_PLUGIN, "warSourceDirectory", "src/main/webapp"));
    }/*w w  w.java  2  s . c om*/

    sources.add(pom.getFile().getPath());
    sources.addAll(pom.getCompileSourceRoots());

    return sourcePaths(pom, ScanProperties.PROJECT_SOURCE_DIRS, sources);
}

From source file:org.sonarsource.scanner.maven.bootstrap.MavenProjectConverter.java

License:Open Source License

private List<File> sourcePaths(MavenProject pom, String propertyKey, Collection<String> mavenPaths)
        throws MojoExecutionException {
    List<File> filesOrDirs;
    boolean userDefined = false;
    String prop = StringUtils.defaultIfEmpty(userProperties.getProperty(propertyKey),
            envProperties.getProperty(propertyKey));
    prop = StringUtils.defaultIfEmpty(prop, pom.getProperties().getProperty(propertyKey));

    if (prop != null) {
        List<String> paths = Arrays.asList(StringUtils.split(prop, ","));
        filesOrDirs = resolvePaths(paths, pom.getBasedir());
        userDefined = true;// w  ww. j a  v  a 2 s  .c  o m
    } else {
        removeTarget(pom, mavenPaths);
        filesOrDirs = resolvePaths(mavenPaths, pom.getBasedir());
    }

    if (userDefined && !MAVEN_PACKAGING_POM.equals(pom.getModel().getPackaging())) {
        return existingPathsOrFail(filesOrDirs, pom, propertyKey);
    } else {
        // Maven provides some directories that do not exist. They
        // should be removed. Same for pom module were sonar.sources and sonar.tests
        // can be defined only to be inherited by children
        return removeNested(keepExistingPaths(filesOrDirs));
    }
}

From source file:org.sonatype.flexmojos.plugin.utilities.CompileConfigurationLoader.java

License:Open Source License

@SuppressWarnings("unchecked")
public static Xpp3Dom getCompilerPluginConfiguration(MavenProject project, String optionName) {
    Xpp3Dom value = findCompilerPluginSettingInPlugins(project.getModel().getBuild().getPlugins(), optionName);
    if (value == null && project.getModel().getBuild().getPluginManagement() != null) {
        value = findCompilerPluginSettingInPlugins(
                project.getModel().getBuild().getPluginManagement().getPlugins(), optionName);
    }/*from w  ww  .jav  a2  s.  c  om*/
    return value;
}

From source file:org.sonatype.m2e.mavenarchiver.internal.AbstractMavenArchiverConfigurator.java

License:Open Source License

/**
 * Workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=356725. 
 * Loads the parent project hierarchy if needed.
 * @param facade//from   ww  w . j ava  2 s.  com
 * @param monitor
 * @return true if parent projects had to be loaded.
 * @throws CoreException
 */
private boolean loadParentHierarchy(IMavenProjectFacade facade, IProgressMonitor monitor) throws CoreException {
    boolean loadedParent = false;
    MavenProject mavenProject = facade.getMavenProject();
    try {
        if (mavenProject.getModel().getParent() == null || mavenProject.getParent() != null) {
            //If the getParent() method is called without error,
            // we can assume the project has been fully loaded, no need to continue.
            return false;
        }
    } catch (IllegalStateException e) {
        //The parent can not be loaded properly 
    }
    MavenExecutionRequest request = null;
    while (mavenProject != null && mavenProject.getModel().getParent() != null) {
        if (monitor.isCanceled()) {
            break;
        }
        if (request == null) {
            request = projectManager.createExecutionRequest(facade, monitor);
        }
        MavenProject parentProject = maven.resolveParentProject(request, mavenProject, monitor);
        if (parentProject != null) {
            mavenProject.setParent(parentProject);
            loadedParent = true;
        }
        mavenProject = parentProject;
    }
    return loadedParent;
}

From source file:org.sonatype.nexus.maven.staging.deploy.DeployLifecycleParticipant.java

License:Open Source License

public void afterProjectsRead(final MavenSession session) throws MavenExecutionException {
    try {/*from w  ww  .  j  a va  2  s. com*/
        final int totalModules = session.getProjects().size();
        logger.info("Inspecting build with total of " + totalModules + " modules...");

        int stagingGoalsFoundInModules = 0;
        // check do we need to do anything at all?
        // should not find any nexus-staging-maven-plugin deploy goal executions in any project
        // otherwise, assume it's "manually done"
        for (MavenProject project : session.getProjects()) {
            final Plugin nexusMavenPlugin = getBuildPluginsNexusMavenPlugin(project.getModel());
            if (nexusMavenPlugin != null) {
                if (!nexusMavenPlugin.getExecutions().isEmpty()) {
                    for (PluginExecution pluginExecution : nexusMavenPlugin.getExecutions()) {
                        final List<String> goals = pluginExecution.getGoals();
                        if (goals.contains("deploy") || goals.contains("deploy-staged")
                                || goals.contains("staging-close") || goals.contains("staging-release")
                                || goals.contains("staging-promote")) {
                            stagingGoalsFoundInModules++;
                            break;
                        }
                    }
                }
            }
        }

        if (stagingGoalsFoundInModules > 0) {
            logger.info("Not installing Nexus Staging features:");
            if (stagingGoalsFoundInModules > 0) {
                logger.info(" * Preexisting staging related goal bindings found in "
                        + stagingGoalsFoundInModules + " modules.");
            }
            return;
        }

        logger.info("Installing Nexus Staging features:");

        // make maven-deploy-plugin to be skipped and install us instead
        int skipped = 0;
        for (MavenProject project : session.getProjects()) {
            final Plugin nexusMavenPlugin = getBuildPluginsNexusMavenPlugin(project.getModel());
            if (nexusMavenPlugin != null) {
                // skip the maven-deploy-plugin
                final Plugin mavenDeployPlugin = getBuildPluginsMavenDeployPlugin(project.getModel());
                if (mavenDeployPlugin != null) {
                    // TODO: better would be to remove them targeted?
                    // But this mojo has only 3 goals, but only one of them is usable in builds ("deploy")
                    mavenDeployPlugin.getExecutions().clear();

                    // add executions to nexus-staging-maven-plugin
                    final PluginExecution execution = new PluginExecution();
                    execution.setId("injected-nexus-deploy");
                    execution.getGoals().add("deploy");
                    execution.setPhase("deploy");
                    execution.setConfiguration(nexusMavenPlugin.getConfiguration());
                    nexusMavenPlugin.getExecutions().add(execution);

                    // count this in
                    skipped++;
                }
            }
        }
        if (skipped > 0) {
            logger.info("  ... total of " + skipped + " executions of maven-deploy-plugin replaced with "
                    + getPluginArtifactId());
        }
    } catch (IllegalStateException e) {
        // thrown by getPluginByGAFromContainer
        throw new MavenExecutionException(e.getMessage(), e);
    }
}