Search in sources :

Example 1 with MavenArchetype

use of org.eclipse.m2e.core.ui.internal.archetype.MavenArchetype in project m2e-core by eclipse-m2e.

the class MavenProjectWizard method performFinish.

/**
 * To perform the actual project creation, an operation is created and run using this wizard as execution context.
 * That way, messages about the progress of the project creation are displayed inside the wizard.
 */
@Override
public boolean performFinish() {
    // First of all, we extract all the information from the wizard pages.
    // Note that this should not be done inside the operation we will run
    // since many of the wizard pages' methods can only be invoked from within
    // the SWT event dispatcher thread. However, the operation spawns a new
    // separate thread to perform the actual work, i.e. accessing SWT elements
    // from within that thread would lead to an exception.
    // final IProject project = locationPage.getProjectHandle();
    // final String projectName = locationPage.getProjectName();
    // Get the location where to create the project. For some reason, when using
    // the default workspace location for a project, we have to pass null
    // instead of the actual location.
    final Model model = getModel();
    final String projectName = ProjectConfigurationManager.getProjectName(importConfiguration, model);
    IStatus nameStatus = validateProjectName(importConfiguration, model);
    if (!nameStatus.isOK()) {
        MessageDialog.openError(getShell(), NLS.bind(Messages.wizardProjectJobFailed, projectName), nameStatus.getMessage());
        return false;
    }
    final IPath location = locationPage.isInWorkspace() ? null : locationPage.getLocationPath();
    final IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    final IProject project = root.getProject(projectName);
    boolean pomExists = (locationPage.isInWorkspace() ? root.getLocation().append(project.getName()) : location).append(IMavenConstants.POM_FILE_NAME).toFile().exists();
    if (pomExists) {
        MessageDialog.openError(getShell(), NLS.bind(Messages.wizardProjectJobFailed, projectName), Messages.wizardProjectErrorPomAlreadyExists);
        return false;
    }
    final AbstractCreateMavenProjectJob job;
    if (simpleProject.getSelection()) {
        final String[] folders = artifactPage.getFolders();
        job = new AbstractCreateMavenProjectJob(NLS.bind(Messages.wizardProjectJobCreatingProject, projectName)) {

            @Override
            protected List<IProject> doCreateMavenProjects(IProgressMonitor monitor) throws CoreException {
                // 
                MavenPlugin.getProjectConfigurationManager().createSimpleProject(// 
                project, // 
                location, // 
                model, // 
                folders, importConfiguration, new MavenProjectWorkspaceAssigner(workingSets), monitor);
                return Arrays.asList(project);
            }
        };
    } else {
        final Archetype archetype = archetypePage.getArchetype();
        final String groupId = model.getGroupId();
        final String artifactId = model.getArtifactId();
        final String version = model.getVersion();
        final String javaPackage = parametersPage.getJavaPackage();
        final Properties properties = parametersPage.getProperties();
        job = new AbstractCreateMavenProjectJob(NLS.bind(Messages.wizardProjectJobCreating, archetype.getArtifactId())) {

            @Override
            protected List<IProject> doCreateMavenProjects(IProgressMonitor monitor) throws CoreException {
                List<IProject> projects = M2EUIPluginActivator.getDefault().getArchetypeManager().getGenerator().createArchetypeProjects(location, // 
                new MavenArchetype(archetype), // 
                groupId, // 
                artifactId, // 
                version, // 
                javaPackage, properties, importConfiguration, new MavenProjectWorkspaceAssigner(workingSets), monitor);
                return projects;
            }
        };
    }
    job.addJobChangeListener(new JobChangeAdapter() {

        @Override
        public void done(IJobChangeEvent event) {
            final IStatus result = event.getResult();
            if (!result.isOK()) {
                Display.getDefault().asyncExec(() -> // 
                MessageDialog.openError(// 
                getShell(), NLS.bind(Messages.wizardProjectJobFailed, projectName), result.getMessage()));
            }
            MappingDiscoveryJob discoveryJob = new MappingDiscoveryJob(job.getCreatedProjects());
            discoveryJob.schedule();
        }
    });
    job.setRule(MavenPlugin.getProjectConfigurationManager().getRule());
    job.schedule();
    return true;
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) IPath(org.eclipse.core.runtime.IPath) Archetype(org.apache.maven.archetype.catalog.Archetype) MavenArchetype(org.eclipse.m2e.core.ui.internal.archetype.MavenArchetype) JobChangeAdapter(org.eclipse.core.runtime.jobs.JobChangeAdapter) IJobChangeEvent(org.eclipse.core.runtime.jobs.IJobChangeEvent) Properties(java.util.Properties) MavenArchetype(org.eclipse.m2e.core.ui.internal.archetype.MavenArchetype) IProject(org.eclipse.core.resources.IProject) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) CoreException(org.eclipse.core.runtime.CoreException) Model(org.apache.maven.model.Model) List(java.util.List)

Example 2 with MavenArchetype

use of org.eclipse.m2e.core.ui.internal.archetype.MavenArchetype in project m2e-core by eclipse-m2e.

the class MavenModuleWizard method performFinish.

/**
 * Performs the "finish" action.
 */
@Override
public boolean performFinish() {
    // First of all, we extract all the information from the wizard pages.
    // Note that this should not be done inside the operation we will run
    // since many of the wizard pages' methods can only be invoked from within
    // the SWT event dispatcher thread. However, the operation spawns a new
    // separate thread to perform the actual work, i.e. accessing SWT elements
    // from within that thread would lead to an exception.
    final String moduleName = parentPage.getModuleName();
    // Get the location where to create the project. For some reason, when using
    // the default workspace location for a project, we have to pass null
    // instead of the actual location.
    final IPath location = parentPage.getParentContainer().getLocation();
    final IFile parentPom = parentPage.getPom();
    Job job;
    if (parentPage.isSimpleProject()) {
        final Model model = artifactPage.getModel();
        if (model.getParent() != null) {
            Parent par = model.getParent();
            String relPath = location.makeRelativeTo(location.append(moduleName)).toOSString();
            if (!"..".equals(relPath)) {
                // $NON-NLS-1$
                par.setRelativePath(relPath);
            }
            // #335331 remove current model's version and groupId if equal to parent, to prevent showing a warning marker
            if (par.getGroupId() != null && par.getGroupId().equals(model.getGroupId())) {
                model.setGroupId(null);
            }
            if (par.getVersion() != null && par.getVersion().equals(model.getVersion())) {
                model.setVersion(null);
            }
        }
        final String[] folders = artifactPage.getFolders();
        job = new AbstractCreateMavenProjectJob(NLS.bind(Messages.wizardProjectJobCreatingProject, moduleName)) {

            @Override
            protected List<IProject> doCreateMavenProjects(IProgressMonitor monitor) throws CoreException {
                setProperty(IProgressConstants.ACTION_PROPERTY, new OpenMavenConsoleAction());
                String projectName = ProjectConfigurationManager.getProjectName(importConfiguration, model);
                IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
                // XXX respect parent's setting for separate projects for modules
                // XXX should run update sources on parent instead of creating new module project
                MavenPlugin.getProjectConfigurationManager().createSimpleProject(project, location.append(moduleName), model, folders, importConfiguration, new MavenProjectWorkspaceAssigner(workingSets), monitor);
                setModule(projectName);
                return Arrays.asList(project);
            }
        };
    } else {
        Model model = parametersPage.getModel();
        final Archetype archetype = archetypePage.getArchetype();
        final String groupId = model.getGroupId();
        final String artifactId = model.getArtifactId();
        final String version = model.getVersion();
        final String javaPackage = parametersPage.getJavaPackage();
        final Properties properties = parametersPage.getProperties();
        job = new AbstractCreateMavenProjectJob(NLS.bind(Messages.wizardProjectJobCreating, archetype.getArtifactId())) {

            @Override
            protected List<IProject> doCreateMavenProjects(IProgressMonitor monitor) throws CoreException {
                List<IProject> projects = M2EUIPluginActivator.getDefault().getArchetypeManager().getGenerator().createArchetypeProjects(location, // 
                new MavenArchetype(archetype), // 
                groupId, // 
                artifactId, // 
                version, // 
                javaPackage, properties, importConfiguration, new MavenProjectWorkspaceAssigner(workingSets), monitor);
                setModule(moduleName);
                return projects;
            }
        };
    }
    job.addJobChangeListener(new JobChangeAdapter() {

        @Override
        public void done(IJobChangeEvent event) {
            final IStatus result = event.getResult();
            if (result.isOK()) {
                if (!isEditor) {
                    // add the <module> element to the parent pom
                    try {
                        performOnDOMDocument(new OperationTuple(parentPom, (Operation) document -> {
                            Element root = document.getDocumentElement();
                            Element modules = getChild(root, "modules");
                            if (findChild(modules, "module", textEquals(moduleName)) == null) {
                                format(createElementWithText(modules, "module", moduleName));
                            }
                        }));
                    } catch (Exception e) {
                        // $NON-NLS-1$
                        LOG.error("Cannot add module to parent POM", e);
                    }
                }
            } else {
                Display.getDefault().asyncExec(() -> // 
                MessageDialog.openError(// 
                getShell(), // 
                NLS.bind(Messages.wizardProjectJobFailed, moduleName), result.getMessage()));
            }
        }
    });
    job.setRule(MavenPlugin.getProjectConfigurationManager().getRule());
    job.schedule();
    if (isEditor) {
        try {
            job.join();
        } catch (InterruptedException ex) {
        // ignore
        }
    }
    return true;
}
Also used : Arrays(java.util.Arrays) ResourcesPlugin(org.eclipse.core.resources.ResourcesPlugin) LoggerFactory(org.slf4j.LoggerFactory) CoreException(org.eclipse.core.runtime.CoreException) Parent(org.apache.maven.model.Parent) PomEdits.getChild(org.eclipse.m2e.core.ui.internal.editing.PomEdits.getChild) IStatus(org.eclipse.core.runtime.IStatus) OpenMavenConsoleAction(org.eclipse.m2e.core.ui.internal.actions.OpenMavenConsoleAction) PomEdits.performOnDOMDocument(org.eclipse.m2e.core.ui.internal.editing.PomEdits.performOnDOMDocument) IProject(org.eclipse.core.resources.IProject) IPath(org.eclipse.core.runtime.IPath) Composite(org.eclipse.swt.widgets.Composite) OperationTuple(org.eclipse.m2e.core.ui.internal.editing.PomEdits.OperationTuple) PomEdits.findChild(org.eclipse.m2e.core.ui.internal.editing.PomEdits.findChild) IFile(org.eclipse.core.resources.IFile) MessageDialog(org.eclipse.jface.dialogs.MessageDialog) JobChangeAdapter(org.eclipse.core.runtime.jobs.JobChangeAdapter) PomEdits.format(org.eclipse.m2e.core.ui.internal.editing.PomEdits.format) Properties(java.util.Properties) Logger(org.slf4j.Logger) Archetype(org.apache.maven.archetype.catalog.Archetype) Job(org.eclipse.core.runtime.jobs.Job) NLS(org.eclipse.osgi.util.NLS) ProjectConfigurationManager(org.eclipse.m2e.core.internal.project.ProjectConfigurationManager) Display(org.eclipse.swt.widgets.Display) IJobChangeEvent(org.eclipse.core.runtime.jobs.IJobChangeEvent) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) MavenArchetype(org.eclipse.m2e.core.ui.internal.archetype.MavenArchetype) List(java.util.List) Element(org.w3c.dom.Element) PomEdits.textEquals(org.eclipse.m2e.core.ui.internal.editing.PomEdits.textEquals) PomEdits.createElementWithText(org.eclipse.m2e.core.ui.internal.editing.PomEdits.createElementWithText) M2EUIPluginActivator(org.eclipse.m2e.core.ui.internal.M2EUIPluginActivator) MavenImages(org.eclipse.m2e.core.ui.internal.MavenImages) INewWizard(org.eclipse.ui.INewWizard) Messages(org.eclipse.m2e.core.ui.internal.Messages) MavenPlugin(org.eclipse.m2e.core.MavenPlugin) IProgressConstants(org.eclipse.ui.progress.IProgressConstants) Operation(org.eclipse.m2e.core.ui.internal.editing.PomEdits.Operation) SelectionListener(org.eclipse.swt.events.SelectionListener) Model(org.apache.maven.model.Model) IStatus(org.eclipse.core.runtime.IStatus) IFile(org.eclipse.core.resources.IFile) OperationTuple(org.eclipse.m2e.core.ui.internal.editing.PomEdits.OperationTuple) Parent(org.apache.maven.model.Parent) Archetype(org.apache.maven.archetype.catalog.Archetype) MavenArchetype(org.eclipse.m2e.core.ui.internal.archetype.MavenArchetype) JobChangeAdapter(org.eclipse.core.runtime.jobs.JobChangeAdapter) Element(org.w3c.dom.Element) Properties(java.util.Properties) List(java.util.List) Job(org.eclipse.core.runtime.jobs.Job) IPath(org.eclipse.core.runtime.IPath) IJobChangeEvent(org.eclipse.core.runtime.jobs.IJobChangeEvent) OpenMavenConsoleAction(org.eclipse.m2e.core.ui.internal.actions.OpenMavenConsoleAction) MavenArchetype(org.eclipse.m2e.core.ui.internal.archetype.MavenArchetype) IProject(org.eclipse.core.resources.IProject) CoreException(org.eclipse.core.runtime.CoreException) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) CoreException(org.eclipse.core.runtime.CoreException) Model(org.apache.maven.model.Model)

Aggregations

List (java.util.List)2 Properties (java.util.Properties)2 Archetype (org.apache.maven.archetype.catalog.Archetype)2 Model (org.apache.maven.model.Model)2 IProject (org.eclipse.core.resources.IProject)2 CoreException (org.eclipse.core.runtime.CoreException)2 IPath (org.eclipse.core.runtime.IPath)2 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)2 IStatus (org.eclipse.core.runtime.IStatus)2 IJobChangeEvent (org.eclipse.core.runtime.jobs.IJobChangeEvent)2 JobChangeAdapter (org.eclipse.core.runtime.jobs.JobChangeAdapter)2 MavenArchetype (org.eclipse.m2e.core.ui.internal.archetype.MavenArchetype)2 Arrays (java.util.Arrays)1 Parent (org.apache.maven.model.Parent)1 IFile (org.eclipse.core.resources.IFile)1 IWorkspaceRoot (org.eclipse.core.resources.IWorkspaceRoot)1 ResourcesPlugin (org.eclipse.core.resources.ResourcesPlugin)1 Job (org.eclipse.core.runtime.jobs.Job)1 MessageDialog (org.eclipse.jface.dialogs.MessageDialog)1 MavenPlugin (org.eclipse.m2e.core.MavenPlugin)1