Example usage for org.apache.maven.execution DefaultMavenExecutionRequest setBaseDirectory

List of usage examples for org.apache.maven.execution DefaultMavenExecutionRequest setBaseDirectory

Introduction

In this page you can find the example usage for org.apache.maven.execution DefaultMavenExecutionRequest setBaseDirectory.

Prototype

@Override
    public MavenExecutionRequest setBaseDirectory(File basedir) 

Source Link

Usage

From source file:com.bluexml.side.util.dependencies.MavenUtil.java

License:Open Source License

public MavenExecutionResult doMavenGoal(File baseDir, List<String> goals, Map<String, String> parameters,
        List<String> profiles, Boolean offline) throws Exception {
    System.out.println(baseDir.getAbsolutePath());
    DefaultMavenExecutionRequest archetypeCreateRequest = new DefaultMavenExecutionRequest();
    archetypeCreateRequest.setBaseDirectory(baseDir);
    archetypeCreateRequest.setGoals(goals);
    archetypeCreateRequest.setInteractiveMode(false);
    archetypeCreateRequest.setProperty("basedir", baseDir.getAbsolutePath().toString());

    // set active profile
    if (profiles != null && !profiles.isEmpty()) {
        archetypeCreateRequest.addActiveProfiles(profiles);
    }/*from   w ww  .j  a  v a  2  s. c om*/
    // set offline
    if (offline != null) {
        archetypeCreateRequest.setOffline(offline);
    }
    if (parameters != null) {
        // manage additional parameters
        for (Map.Entry<String, String> param : parameters.entrySet()) {
            archetypeCreateRequest.setProperty(param.getKey(), param.getValue());
        }
    }
    MavenEmbedder embedder = getEmbedder();
    archetypeCreateRequest.setUpdateSnapshots(true);

    //System.out.println("Active profiles :"+archetypeCreateRequest.getActiveProfiles());
    if (archetypeCreateRequest.getActiveProfiles().size() == 0) {
        throw new Exception("No active profile found report this bug to SIDE developers team");
    }

    MavenExecutionResult result = embedder.execute(archetypeCreateRequest);
    return result;
}

From source file:com.google.gdt.eclipse.maven.e35.configurators.GoogleProjectConfigurator.java

License:Open Source License

@Override
protected void doConfigure(final MavenProject mavenProject, IProject project,
        ProjectConfigurationRequest request, final IProgressMonitor monitor) throws CoreException {

    final IMaven maven = MavenPlugin.getDefault().getMaven();

    boolean configureGaeNatureSuccess = configureNature(project, mavenProject, GaeNature.NATURE_ID, true,
            new NatureCallbackAdapter() {

                @Override/*from  w ww. ja va2  s. c  o m*/
                public void beforeAddingNature() {
                    try {
                        DefaultMavenExecutionRequest executionRequest = new DefaultMavenExecutionRequest();
                        executionRequest.setBaseDirectory(mavenProject.getBasedir());
                        executionRequest.setLocalRepository(maven.getLocalRepository());
                        executionRequest.setRemoteRepositories(mavenProject.getRemoteArtifactRepositories());
                        executionRequest
                                .setPluginArtifactRepositories(mavenProject.getPluginArtifactRepositories());
                        executionRequest.setPom(mavenProject.getFile());
                        executionRequest.setGoals(GAE_UNPACK_GOAL);

                        MavenExecutionResult result = maven.execute(executionRequest, monitor);
                        if (result.hasExceptions()) {
                            Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
                                    "Error configuring project", result.getExceptions().get(0)));
                        }
                    } catch (CoreException e) {
                        Activator.getDefault().getLog().log(
                                new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Error configuring project", e));
                    }
                }
            }, monitor);

    boolean configureGWTNatureSuccess = configureNature(project, mavenProject, GWTNature.NATURE_ID, true,
            new NatureCallbackAdapter() {

                @Override
                public void beforeAddingNature() {

                    // Get the GWT version from the project pom
                    String gwtVersion = null;
                    List<Dependency> dependencies = mavenProject.getDependencies();
                    for (Dependency dependency : dependencies) {
                        if (GWTMavenRuntime.MAVEN_GWT_GROUP_ID.equals(dependency.getGroupId())
                                && (GWTMavenRuntime.MAVEN_GWT_USER_ARTIFACT_ID
                                        .equals(dependency.getArtifactId())
                                        || GWTMavenRuntime.MAVEN_GWT_SERVLET_ARTIFACT_ID
                                                .equals(dependency.getArtifactId()))) {
                            gwtVersion = dependency.getVersion();
                            break;
                        }
                    }

                    // Check that the pom.xml has GWT dependencies
                    if (!StringUtilities.isEmpty(gwtVersion)) {
                        try {
                            /*
                             * Download and install the gwt-dev.jar into the local
                             * repository.
                             */
                            maven.resolve(GWTMavenRuntime.MAVEN_GWT_GROUP_ID,
                                    GWTMavenRuntime.MAVEN_GWT_DEV_JAR_ARTIFACT_ID, gwtVersion, "jar", null,
                                    mavenProject.getRemoteArtifactRepositories(), monitor);
                        } catch (CoreException e) {
                            Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
                                    "Error configuring project", e));
                        }
                    }
                }
            }, monitor);

    if (configureGWTNatureSuccess || configureGaeNatureSuccess) {
        try {
            // Add GWT Web Application configuration parameters
            WebAppProjectProperties.setWarSrcDir(project, new Path("src/main/webapp"));
            WebAppProjectProperties.setWarSrcDirIsOutput(project, false);

            String artifactId = mavenProject.getArtifactId();
            String version = mavenProject.getVersion();
            IPath location = (project.getRawLocation() != null ? project.getRawLocation()
                    : project.getLocation());
            if (location != null && artifactId != null && version != null) {
                WebAppProjectProperties.setLastUsedWarOutLocation(project,
                        location.append("target").append(artifactId + "-" + version));
            }
        } catch (BackingStoreException be) {
            Activator.getDefault().getLog()
                    .log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Error configuring project", be));
        }
    }
}

From source file:org.springframework.ide.eclipse.maven.legacy.internal.core.GoogleProjectConfigurator.java

License:Open Source License

/**
 * {@inheritDoc}/*from www  . j  a v a  2 s . c o m*/
 */
@Override
protected void doConfigure(final MavenProject mavenProject, IProject project,
        ProjectConfigurationRequest request, final IProgressMonitor monitor) throws CoreException {

    final IMaven maven = MavenPlugin.getDefault().getMaven();

    configureNature(project, mavenProject, GAE_NATURE_ID, true, new NatureCallbackAdapter() {

        @Override
        public void beforeAddingNature() {
            try {
                DefaultMavenExecutionRequest executionRequest = new DefaultMavenExecutionRequest();
                executionRequest.setBaseDirectory(mavenProject.getBasedir());
                executionRequest.setLocalRepository(maven.getLocalRepository());
                executionRequest.setRemoteRepositories(mavenProject.getRemoteArtifactRepositories());
                executionRequest.setPluginArtifactRepositories(mavenProject.getPluginArtifactRepositories());
                executionRequest.setPom(mavenProject.getFile());
                executionRequest.setGoals(GAE_UNPACK_GOAL);

                MavenExecutionResult result = maven.execute(executionRequest, monitor);
                if (result.hasExceptions()) {
                    MavenCorePlugin.getDefault().getLog()
                            .log(new Status(IStatus.ERROR, MavenCorePlugin.NON_LEGACY_PLUGIN_ID,
                                    "Error configuring project", result.getExceptions().get(0)));
                }
            } catch (CoreException e) {
                MavenCorePlugin.getDefault().getLog().log(new Status(IStatus.ERROR,
                        MavenCorePlugin.NON_LEGACY_PLUGIN_ID, "Error configuring project", e));
            }
        }
    }, monitor);

    if (configureNature(project, mavenProject, GWT_NATURE_ID, true, new NatureCallbackAdapter() {

        @Override
        public void beforeAddingNature() {

            // Get the GWT version from the project pom
            String gwtVersion = null;
            List<Dependency> dependencies = mavenProject.getDependencies();
            for (Dependency dependency : dependencies) {
                if (GWT_GROUP_ID.equals(dependency.getGroupId())
                        && ("gwt-user".equals(dependency.getArtifactId())
                                || "gwt-servlet".equals(dependency.getArtifactId()))) {
                    gwtVersion = dependency.getVersion();
                    break;
                }
            }

            // Check that the pom.xml has GWT dependencies
            if (StringUtils.hasLength(gwtVersion)) {
                try {
                    // Download and install the gwt-dev.jar into the local repository
                    maven.resolve(GWT_GROUP_ID, "gwt-dev", gwtVersion, "jar", null,
                            mavenProject.getRemoteArtifactRepositories(), monitor);
                } catch (CoreException e) {
                    MavenCorePlugin.getDefault().getLog().log(new Status(IStatus.ERROR,
                            MavenCorePlugin.NON_LEGACY_PLUGIN_ID, "Error configuring project", e));
                }
            }
        }
    }, monitor)) {

        try {
            // Add GWT Web Application configuration parameters
            IEclipsePreferences prefs = SpringCorePreferences
                    .getProjectPreferences(project, "com.google.gdt.eclipse.core").getProjectPreferences();
            prefs.put("warSrcDir", "src/main/webapp");
            prefs.putBoolean("warSrcDirIsOutput", false);

            String artifactId = mavenProject.getArtifactId();
            String version = mavenProject.getVersion();
            IPath location = SpringCoreUtils.getProjectLocation(project);
            if (location != null && artifactId != null && version != null) {
                prefs.put("lastWarOutDir", location.append("target").append(artifactId + "-" + version).toFile()
                        .getAbsolutePath());
            }

            prefs.flush();
        } catch (BackingStoreException e) {
            MavenCorePlugin.getDefault().getLog().log(new Status(IStatus.ERROR,
                    MavenCorePlugin.NON_LEGACY_PLUGIN_ID, "Error configuring project", e));
        }
    }
}