Search in sources :

Example 1 with MavenUpdateRequest

use of org.eclipse.m2e.core.project.MavenUpdateRequest in project eclipse.jdt.ls by eclipse.

the class MavenProjectImporter method updateProjects.

private void updateProjects(Collection<IProject> projects, long lastWorkspaceStateSaved, IProgressMonitor monitor) throws CoreException {
    if (projects.isEmpty()) {
        return;
    }
    Iterator<IProject> iterator = projects.iterator();
    while (iterator.hasNext()) {
        IProject project = iterator.next();
        project.open(monitor);
        project.refreshLocal(IResource.DEPTH_INFINITE, monitor);
        if (!needsMavenUpdate(project, lastWorkspaceStateSaved)) {
            iterator.remove();
        }
    }
    if (projects.isEmpty()) {
        return;
    }
    new WorkspaceJob("Update Maven project configuration") {

        @Override
        public IStatus runInWorkspace(IProgressMonitor monitor) throws CoreException {
            for (IProject project : projects) {
                if (monitor.isCanceled()) {
                    return Status.CANCEL_STATUS;
                }
                JavaLanguageServerPlugin.logInfo("Updating project configuration for Maven project " + project.getName());
                MavenUpdateRequest request = new MavenUpdateRequest(project, MavenPlugin.getMavenConfiguration().isOffline(), true);
                configurationManager.updateProjectConfiguration(request, monitor);
            }
            return Status.OK_STATUS;
        }
    }.schedule();
}
Also used : MavenUpdateRequest(org.eclipse.m2e.core.project.MavenUpdateRequest) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IStatus(org.eclipse.core.runtime.IStatus) CoreException(org.eclipse.core.runtime.CoreException) WorkspaceJob(org.eclipse.core.resources.WorkspaceJob) IProject(org.eclipse.core.resources.IProject)

Example 2 with MavenUpdateRequest

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

the class LiferayMavenProjectCustomJSPValidationTest method testCustomJspValidationConfigured.

@Test
@Ignore
public void testCustomJspValidationConfigured() throws Exception {
    final IEclipsePreferences prefs = InstanceScope.INSTANCE.getNode(LiferayMavenCore.PLUGIN_ID);
    ;
    prefs.putBoolean(LiferayMavenCore.PREF_DISABLE_CUSTOM_JSP_VALIDATION, true);
    IProject project = importProject("projects/configurators/customJspValidation/pom.xml");
    assertNotNull(project);
    IMavenProjectFacade facade = MavenPlugin.getMavenProjectRegistry().create(project, monitor);
    assertNotNull(facade);
    assertTrue(CoreUtil.isLiferayProject(project));
    final IProjectConfigurationManager projectConfigurationManager = MavenPlugin.getProjectConfigurationManager();
    projectConfigurationManager.updateProjectConfiguration(new MavenUpdateRequest(project, mavenConfiguration.isOffline(), true), monitor);
    waitForBuildAndValidation(project);
    final IMarker[] markers = project.findMarkers("org.eclipse.jst.jsp.core.validationMarker", true, IResource.DEPTH_INFINITE);
    for (IMarker marker : markers) {
        assertEquals(false, marker.getAttribute(IMarker.SEVERITY).equals(IMarker.SEVERITY_ERROR));
    }
    project.delete(true, monitor);
}
Also used : MavenUpdateRequest(org.eclipse.m2e.core.project.MavenUpdateRequest) IEclipsePreferences(org.eclipse.core.runtime.preferences.IEclipsePreferences) IProjectConfigurationManager(org.eclipse.m2e.core.project.IProjectConfigurationManager) IMarker(org.eclipse.core.resources.IMarker) IMavenProjectFacade(org.eclipse.m2e.core.project.IMavenProjectFacade) IProject(org.eclipse.core.resources.IProject) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 3 with MavenUpdateRequest

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

the class LiferayMavenProjectCustomJSPValidationTest method testNoCustomJspValidationConfigured.

@Test
@Ignore
public void testNoCustomJspValidationConfigured() throws Exception {
    final IEclipsePreferences prefs = InstanceScope.INSTANCE.getNode(LiferayMavenCore.PLUGIN_ID);
    ;
    prefs.putBoolean(LiferayMavenCore.PREF_DISABLE_CUSTOM_JSP_VALIDATION, false);
    IProject project = importProject("projects/configurators/customJspValidation/pom.xml");
    assertNotNull(project);
    IMavenProjectFacade facade = MavenPlugin.getMavenProjectRegistry().create(project, monitor);
    assertNotNull(facade);
    assertTrue(CoreUtil.isLiferayProject(project));
    final IProjectConfigurationManager projectConfigurationManager = MavenPlugin.getProjectConfigurationManager();
    projectConfigurationManager.updateProjectConfiguration(new MavenUpdateRequest(project, mavenConfiguration.isOffline(), true), monitor);
    waitForBuildAndValidation(project);
    final IMarker[] markers = project.findMarkers("org.eclipse.jst.jsp.core.validationMarker", true, IResource.DEPTH_INFINITE);
    for (IMarker marker : markers) {
        assertEquals(true, marker.getAttribute(IMarker.SEVERITY).equals(IMarker.SEVERITY_ERROR));
    }
    project.delete(true, monitor);
}
Also used : MavenUpdateRequest(org.eclipse.m2e.core.project.MavenUpdateRequest) IEclipsePreferences(org.eclipse.core.runtime.preferences.IEclipsePreferences) IProjectConfigurationManager(org.eclipse.m2e.core.project.IProjectConfigurationManager) IMarker(org.eclipse.core.resources.IMarker) IMavenProjectFacade(org.eclipse.m2e.core.project.IMavenProjectFacade) IProject(org.eclipse.core.resources.IProject) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 4 with MavenUpdateRequest

use of org.eclipse.m2e.core.project.MavenUpdateRequest 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 5 with MavenUpdateRequest

use of org.eclipse.m2e.core.project.MavenUpdateRequest in project eclipse.jdt.ls by eclipse.

the class MavenBuildSupport method update.

@Override
public void update(IProject project, IProgressMonitor monitor) throws CoreException {
    if (!applies(project)) {
        return;
    }
    JavaLanguageServerPlugin.logInfo("Starting Maven update for " + project.getName());
    // TODO collect dependent projects and update them as well? i.e in case a parent project was modified
    IProjectConfigurationManager configurationManager = MavenPlugin.getProjectConfigurationManager();
    MavenUpdateRequest request = new MavenUpdateRequest(project, MavenPlugin.getMavenConfiguration().isOffline(), true);
    configurationManager.updateProjectConfiguration(request, monitor);
}
Also used : MavenUpdateRequest(org.eclipse.m2e.core.project.MavenUpdateRequest) IProjectConfigurationManager(org.eclipse.m2e.core.project.IProjectConfigurationManager)

Aggregations

MavenUpdateRequest (org.eclipse.m2e.core.project.MavenUpdateRequest)5 IProject (org.eclipse.core.resources.IProject)4 IProjectConfigurationManager (org.eclipse.m2e.core.project.IProjectConfigurationManager)4 IMavenProjectFacade (org.eclipse.m2e.core.project.IMavenProjectFacade)3 IMarker (org.eclipse.core.resources.IMarker)2 CoreException (org.eclipse.core.runtime.CoreException)2 IStatus (org.eclipse.core.runtime.IStatus)2 IEclipsePreferences (org.eclipse.core.runtime.preferences.IEclipsePreferences)2 Ignore (org.junit.Ignore)2 Test (org.junit.Test)2 IPortletFramework (com.liferay.ide.project.core.IPortletFramework)1 NewLiferayProfile (com.liferay.ide.project.core.model.NewLiferayProfile)1 PluginType (com.liferay.ide.project.core.model.PluginType)1 ProjectName (com.liferay.ide.project.core.model.ProjectName)1 File (java.io.File)1 IOException (java.io.IOException)1 Properties (java.util.Properties)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 Transformer (javax.xml.transform.Transformer)1