Search in sources :

Example 11 with ResolverConfiguration

use of org.eclipse.m2e.core.project.ResolverConfiguration in project liferay-ide by liferay.

the class NewMavenPluginProjectProvider method createNewProject.

@Override
public IStatus createNewProject(NewLiferayPluginProjectOp op, IProgressMonitor monitor) throws CoreException {
    ElementList<ProjectName> projectNames = op.getProjectNames();
    IStatus retval = null;
    IMavenConfiguration mavenConfiguration = MavenPlugin.getMavenConfiguration();
    IMavenProjectRegistry mavenProjectRegistry = MavenPlugin.getMavenProjectRegistry();
    IProjectConfigurationManager projectConfigurationManager = MavenPlugin.getProjectConfigurationManager();
    String groupId = op.getGroupId().content();
    String artifactId = op.getProjectName().content();
    String version = op.getArtifactVersion().content();
    String javaPackage = op.getGroupId().content();
    String activeProfilesValue = op.getActiveProfilesValue().content();
    IPortletFramework portletFramework = op.getPortletFramework().content(true);
    String frameworkName = NewLiferayPluginProjectOpMethods.getFrameworkName(op);
    IPath location = PathBridge.create(op.getLocation().content());
    if (location.lastSegment().equals(artifactId)) {
        // use parent dir since maven archetype will generate new dir under this
        // location
        location = location.removeLastSegments(1);
    }
    String archetypeArtifactId = op.getArchetype().content(true);
    Archetype archetype = new Archetype();
    String[] gav = archetypeArtifactId.split(":");
    String archetypeVersion = gav[gav.length - 1];
    archetype.setGroupId(gav[0]);
    archetype.setArtifactId(gav[1]);
    archetype.setVersion(archetypeVersion);
    ArchetypeManager archetypeManager = MavenPluginActivator.getDefault().getArchetypeManager();
    ArtifactRepository remoteArchetypeRepository = archetypeManager.getArchetypeRepository(archetype);
    Properties properties = new Properties();
    try {
        List<?> archProps = archetypeManager.getRequiredProperties(archetype, remoteArchetypeRepository, monitor);
        if (ListUtil.isNotEmpty(archProps)) {
            for (Object prop : archProps) {
                if (prop instanceof RequiredProperty) {
                    RequiredProperty rProp = (RequiredProperty) prop;
                    Value<PluginType> pluginType = op.getPluginType();
                    if (pluginType.content().equals(PluginType.theme)) {
                        String key = rProp.getKey();
                        if (key.equals("themeParent")) {
                            properties.put(key, op.getThemeParent().content(true));
                        } else if (key.equals("themeType")) {
                            properties.put(key, ThemeUtil.getTemplateExtension(op.getThemeFramework().content(true)));
                        }
                    } else {
                        properties.put(rProp.getKey(), rProp.getDefaultValue());
                    }
                }
            }
        }
    } catch (UnknownArchetype e1) {
        LiferayMavenCore.logError("Unable to find archetype required properties", e1);
    }
    ResolverConfiguration resolverConfig = new ResolverConfiguration();
    if (!CoreUtil.isNullOrEmpty(activeProfilesValue)) {
        resolverConfig.setSelectedProfiles(activeProfilesValue);
    }
    ProjectImportConfiguration configuration = new ProjectImportConfiguration(resolverConfig);
    List<IProject> newProjects = projectConfigurationManager.createArchetypeProjects(location, archetype, groupId, artifactId, version, javaPackage, properties, configuration, monitor);
    if (ListUtil.isNotEmpty(newProjects)) {
        op.setImportProjectStatus(true);
        for (IProject project : newProjects) {
            projectNames.insert().setName(project.getName());
        }
    }
    if (ListUtil.isEmpty(newProjects)) {
        retval = LiferayMavenCore.createErrorStatus("New project was not created due to unknown error");
    } else {
        IProject firstProject = newProjects.get(0);
        if (!CoreUtil.isNullOrEmpty(activeProfilesValue)) {
            String[] activeProfiles = activeProfilesValue.split(",");
            // find all profiles that should go in user settings file
            List<NewLiferayProfile> newUserSettingsProfiles = getNewProfilesToSave(activeProfiles, op.getNewLiferayProfiles(), ProfileLocation.userSettings);
            if (ListUtil.isNotEmpty(newUserSettingsProfiles)) {
                String userSettingsFile = mavenConfiguration.getUserSettingsFile();
                String userSettingsPath = null;
                if (CoreUtil.isNullOrEmpty(userSettingsFile)) {
                    userSettingsPath = SettingsXmlConfigurationProcessor.DEFAULT_USER_SETTINGS_FILE.getAbsolutePath();
                } else {
                    userSettingsPath = userSettingsFile;
                }
                try {
                    // backup user's settings.xml file
                    File settingsXmlFile = new File(userSettingsPath);
                    File backupFile = _getBackupFile(settingsXmlFile);
                    FileUtils.copyFile(settingsXmlFile, backupFile);
                    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
                    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
                    Document pomDocument = docBuilder.parse(settingsXmlFile.getCanonicalPath());
                    for (NewLiferayProfile newProfile : newUserSettingsProfiles) {
                        MavenUtil.createNewLiferayProfileNode(pomDocument, newProfile);
                    }
                    TransformerFactory transformerFactory = TransformerFactory.newInstance();
                    Transformer transformer = transformerFactory.newTransformer();
                    DOMSource source = new DOMSource(pomDocument);
                    StreamResult result = new StreamResult(settingsXmlFile);
                    transformer.transform(source, result);
                } catch (Exception e) {
                    LiferayMavenCore.logError("Unable to save new Liferay profile to user settings.xml.", e);
                }
            }
            // find all profiles that should go in the project pom
            List<NewLiferayProfile> newProjectPomProfiles = getNewProfilesToSave(activeProfiles, op.getNewLiferayProfiles(), ProfileLocation.projectPom);
            // only need to set the first project as nested projects should pickup the
            // parent setting
            IMavenProjectFacade newMavenProject = mavenProjectRegistry.getProject(firstProject);
            IFile pomFile = newMavenProject.getPom();
            IDOMModel domModel = null;
            try {
                domModel = (IDOMModel) StructuredModelManager.getModelManager().getModelForEdit(pomFile);
                for (NewLiferayProfile newProfile : newProjectPomProfiles) {
                    MavenUtil.createNewLiferayProfileNode(domModel.getDocument(), newProfile);
                }
                domModel.save();
            } catch (IOException ioe) {
                LiferayMavenCore.logError("Unable to save new Liferay profiles to project pom.", ioe);
            } finally {
                if (domModel != null) {
                    domModel.releaseFromEdit();
                }
            }
            for (IProject project : newProjects) {
                try {
                    projectConfigurationManager.updateProjectConfiguration(new MavenUpdateRequest(project, mavenConfiguration.isOffline(), true), monitor);
                } catch (Exception e) {
                    LiferayMavenCore.logError("Unable to update configuration for " + project.getName(), e);
                }
            }
            String pluginVersion = getNewLiferayProfilesPluginVersion(activeProfiles, op.getNewLiferayProfiles(), archetypeVersion);
            String archVersion = MavenUtil.getMajorMinorVersionOnly(archetypeVersion);
            updateDtdVersion(firstProject, pluginVersion, archVersion);
        }
        Value<PluginType> pluginType = op.getPluginType();
        if (pluginType.content().equals(PluginType.portlet)) {
            String portletName = op.getPortletName().content(false);
            retval = portletFramework.postProjectCreated(firstProject, frameworkName, portletName, monitor);
        }
    }
    if (retval == null) {
        retval = Status.OK_STATUS;
    }
    return retval;
}
Also used : ResolverConfiguration(org.eclipse.m2e.core.project.ResolverConfiguration) MavenUpdateRequest(org.eclipse.m2e.core.project.MavenUpdateRequest) IStatus(org.eclipse.core.runtime.IStatus) DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Transformer(javax.xml.transform.Transformer) IFile(org.eclipse.core.resources.IFile) ProjectName(com.liferay.ide.project.core.model.ProjectName) Archetype(org.apache.maven.archetype.catalog.Archetype) UnknownArchetype(org.apache.maven.archetype.exception.UnknownArchetype) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) NewLiferayProfile(com.liferay.ide.project.core.model.NewLiferayProfile) IProjectConfigurationManager(org.eclipse.m2e.core.project.IProjectConfigurationManager) IMavenProjectRegistry(org.eclipse.m2e.core.project.IMavenProjectRegistry) Properties(java.util.Properties) Document(org.w3c.dom.Document) IMavenConfiguration(org.eclipse.m2e.core.embedder.IMavenConfiguration) RequiredProperty(org.apache.maven.archetype.metadata.RequiredProperty) IMavenProjectFacade(org.eclipse.m2e.core.project.IMavenProjectFacade) TransformerFactory(javax.xml.transform.TransformerFactory) IPath(org.eclipse.core.runtime.IPath) StreamResult(javax.xml.transform.stream.StreamResult) ArtifactRepository(org.apache.maven.artifact.repository.ArtifactRepository) IOException(java.io.IOException) PluginType(com.liferay.ide.project.core.model.PluginType) IProject(org.eclipse.core.resources.IProject) CoreException(org.eclipse.core.runtime.CoreException) IOException(java.io.IOException) ArchetypeManager(org.eclipse.m2e.core.internal.archetype.ArchetypeManager) IPortletFramework(com.liferay.ide.project.core.IPortletFramework) ProjectImportConfiguration(org.eclipse.m2e.core.project.ProjectImportConfiguration) DocumentBuilder(javax.xml.parsers.DocumentBuilder) UnknownArchetype(org.apache.maven.archetype.exception.UnknownArchetype) IFile(org.eclipse.core.resources.IFile) File(java.io.File)

Example 12 with ResolverConfiguration

use of org.eclipse.m2e.core.project.ResolverConfiguration in project liferay-ide by liferay.

the class MavenProjectBuilder method _execMavenLaunch.

private boolean _execMavenLaunch(IProject project, String goal, IMavenProjectFacade facade, IProgressMonitor monitor) throws CoreException {
    ILaunchManager launchManager = DebugPlugin.getDefault().getLaunchManager();
    ILaunchConfigurationType launchConfigurationType = launchManager.getLaunchConfigurationType(_launchConfigurationTypeId);
    IPath basedirLocation = project.getLocation();
    String newName = launchManager.generateLaunchConfigurationName(basedirLocation.lastSegment());
    ILaunchConfigurationWorkingCopy workingCopy = launchConfigurationType.newInstance(null, newName);
    workingCopy.setAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS, "-Dmaven.multiModuleProjectDirectory");
    workingCopy.setAttribute(_attrPomDir, basedirLocation.toString());
    workingCopy.setAttribute(_attrGoals, goal);
    // workingCopy.setAttribute( ATTR_UPDATE_SNAPSHOTS, Boolean.TRUE );
    workingCopy.setAttribute(_attrWorkspaceResolution, Boolean.TRUE);
    workingCopy.setAttribute(_attrSkipTests, Boolean.TRUE);
    if (facade != null) {
        ResolverConfiguration configuration = facade.getResolverConfiguration();
        String selectedProfiles = configuration.getSelectedProfiles();
        if ((selectedProfiles != null) && (selectedProfiles.length() > 0)) {
            workingCopy.setAttribute(_attrProfiles, selectedProfiles);
        }
        new LaunchHelper().launch(workingCopy, "run", monitor);
        return true;
    } else {
        return false;
    }
}
Also used : ResolverConfiguration(org.eclipse.m2e.core.project.ResolverConfiguration) IPath(org.eclipse.core.runtime.IPath) ILaunchConfigurationType(org.eclipse.debug.core.ILaunchConfigurationType) LaunchHelper(com.liferay.ide.core.util.LaunchHelper) ILaunchManager(org.eclipse.debug.core.ILaunchManager) ILaunchConfigurationWorkingCopy(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)

Example 13 with ResolverConfiguration

use of org.eclipse.m2e.core.project.ResolverConfiguration in project liferay-ide by liferay.

the class AbstractProjectMarkerResolution method run.

public void run(IMarker marker) {
    IProject project = marker.getResource().getProject();
    IProjectConfigurationManager projectManager = MavenPlugin.getProjectConfigurationManager();
    ResolverConfiguration configuration = projectManager.getResolverConfiguration(project);
    List<String> currentProfiles = configuration.getActiveProfileList();
    NewLiferayProfileOp op = NewLiferayProfileOp.TYPE.instantiate();
    ElementList<Profile> selectedProfiles = op.getSelectedProfiles();
    for (String currentProfile : currentProfiles) {
        selectedProfiles.insert().setId(currentProfile);
    }
    int result = promptUser(project, op);
    if (result == SapphireDialog.OK) {
        configuration.setSelectedProfiles(op.getActiveProfilesValue().content());
        boolean changed = projectManager.setResolverConfiguration(project, configuration);
        if (changed) {
            WorkspaceJob job = new WorkspaceJob("Updating project " + project.getName()) {

                public IStatus runInWorkspace(IProgressMonitor monitor) {
                    try {
                        MavenPlugin.getProjectConfigurationManager().updateProjectConfiguration(project, monitor);
                    } catch (CoreException ce) {
                        return ce.getStatus();
                    }
                    return Status.OK_STATUS;
                }
            };
            job.setRule(MavenPlugin.getProjectConfigurationManager().getRule());
            job.schedule();
        }
    }
}
Also used : ResolverConfiguration(org.eclipse.m2e.core.project.ResolverConfiguration) IProjectConfigurationManager(org.eclipse.m2e.core.project.IProjectConfigurationManager) WorkspaceJob(org.eclipse.core.resources.WorkspaceJob) IProject(org.eclipse.core.resources.IProject) Profile(com.liferay.ide.project.core.model.Profile) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) CoreException(org.eclipse.core.runtime.CoreException) NewLiferayProfileOp(com.liferay.ide.maven.core.model.NewLiferayProfileOp)

Example 14 with ResolverConfiguration

use of org.eclipse.m2e.core.project.ResolverConfiguration in project bndtools by bndtools.

the class BndConfigurator method execJarMojo.

private void execJarMojo(final IMavenProjectFacade projectFacade, IProgressMonitor monitor) throws CoreException {
    final IMaven maven = MavenPlugin.getMaven();
    ProjectRegistryManager projectRegistryManager = MavenPluginActivator.getDefault().getMavenProjectManagerImpl();
    ResolverConfiguration resolverConfiguration = new ResolverConfiguration();
    resolverConfiguration.setResolveWorkspaceProjects(true);
    IMavenExecutionContext context = projectRegistryManager.createExecutionContext(projectFacade.getPom(), resolverConfiguration);
    context.execute(new ICallable<Void>() {

        @Override
        public Void call(IMavenExecutionContext context, IProgressMonitor monitor) throws CoreException {
            SubMonitor progress = SubMonitor.convert(monitor);
            MavenProject mavenProject = getMavenProject(projectFacade, progress.newChild(1));
            MavenExecutionPlan plan = maven.calculateExecutionPlan(mavenProject, Arrays.asList("jar:jar"), true, monitor);
            List<MojoExecution> mojoExecutions = plan.getMojoExecutions();
            if (mojoExecutions != null) {
                for (MojoExecution mojoExecution : mojoExecutions) {
                    maven.execute(mavenProject, mojoExecution, progress.newChild(1));
                }
            }
            // We can now decorate based on the build we just did.
            try {
                IProjectDecorator decorator = Injector.ref.get();
                if (decorator != null) {
                    BndProjectInfo info = new MavenProjectInfo(mavenProject);
                    decorator.updateDecoration(projectFacade.getProject(), info);
                }
            } catch (Exception e) {
                logger.logError("Failed to decorate project " + projectFacade.getProject().getName(), e);
            }
            return null;
        }
    }, monitor);
}
Also used : ResolverConfiguration(org.eclipse.m2e.core.project.ResolverConfiguration) SubMonitor(org.eclipse.core.runtime.SubMonitor) IProjectDecorator(org.bndtools.build.api.IProjectDecorator) CoreException(org.eclipse.core.runtime.CoreException) BndProjectInfo(org.bndtools.build.api.IProjectDecorator.BndProjectInfo) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) CoreException(org.eclipse.core.runtime.CoreException) MavenProject(org.apache.maven.project.MavenProject) MojoExecution(org.apache.maven.plugin.MojoExecution) ProjectRegistryManager(org.eclipse.m2e.core.internal.project.registry.ProjectRegistryManager) IMavenExecutionContext(org.eclipse.m2e.core.embedder.IMavenExecutionContext) ArrayList(java.util.ArrayList) List(java.util.List) MavenExecutionPlan(org.apache.maven.lifecycle.MavenExecutionPlan) IMaven(org.eclipse.m2e.core.embedder.IMaven)

Example 15 with ResolverConfiguration

use of org.eclipse.m2e.core.project.ResolverConfiguration in project flux by eclipse.

the class DownloadProject method importAsPureMavenProject.

private void importAsPureMavenProject(IFile pomFile) {
    WorkspaceJob job = new WorkspaceJob("importAsMaven") {

        @Override
        public IStatus runInWorkspace(IProgressMonitor monitor) throws CoreException {
            try {
                ResolverConfiguration resolverConfiguration = new ResolverConfiguration();
                String activeProfiles = "pom.xml";
                resolverConfiguration.setActiveProfiles(activeProfiles);
                MavenPlugin.getProjectConfigurationManager().enableMavenNature(project, resolverConfiguration, monitor);
                return Status.OK_STATUS;
            } catch (CoreException e) {
                e.printStackTrace();
                return Status.OK_STATUS;
            }
        }
    };
    job.setRule(ResourcesPlugin.getWorkspace().getRoot());
    job.schedule();
}
Also used : ResolverConfiguration(org.eclipse.m2e.core.project.ResolverConfiguration) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) CoreException(org.eclipse.core.runtime.CoreException) WorkspaceJob(org.eclipse.core.resources.WorkspaceJob)

Aggregations

ResolverConfiguration (org.eclipse.m2e.core.project.ResolverConfiguration)15 CoreException (org.eclipse.core.runtime.CoreException)8 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)6 MavenProject (org.apache.maven.project.MavenProject)5 IPath (org.eclipse.core.runtime.IPath)5 MavenExecutionPlan (org.apache.maven.lifecycle.MavenExecutionPlan)4 MojoExecution (org.apache.maven.plugin.MojoExecution)4 IStatus (org.eclipse.core.runtime.IStatus)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 IMaven (org.eclipse.m2e.core.embedder.IMaven)3 IProjectConfigurationManager (org.eclipse.m2e.core.project.IProjectConfigurationManager)3 LaunchHelper (com.liferay.ide.core.util.LaunchHelper)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 MavenSession (org.apache.maven.execution.MavenSession)2 IFile (org.eclipse.core.resources.IFile)2 IProject (org.eclipse.core.resources.IProject)2 WorkspaceJob (org.eclipse.core.resources.WorkspaceJob)2 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)2 ILaunchConfigurationType (org.eclipse.debug.core.ILaunchConfigurationType)2