예제 #1
0
  public static void performOnRootElement(
      IFile resource, NodeOperation<Element> operation, boolean autoSave)
      throws IOException, CoreException {
    assert resource != null;
    assert operation != null;
    IDOMModel domModel = null;
    try {
      domModel = (IDOMModel) StructuredModelManager.getModelManager().getModelForRead(resource);
      if (domModel == null) {
        throw new IllegalArgumentException("Document is not structured: " + resource);
      }
      IStructuredDocument document = domModel.getStructuredDocument();
      Element root = domModel.getDocument().getDocumentElement();
      operation.process(root, document);

      if (autoSave && domModel.getReferenceCountForEdit() == 0) {
        domModel.save();
      }

    } finally {
      if (domModel != null) {
        domModel.releaseFromRead();
      }
    }
  }
  public IStatus doCreateNewProject(final NewLiferayPluginProjectOp op, IProgressMonitor monitor)
      throws CoreException {
    IStatus retval = null;

    final IMavenConfiguration mavenConfiguration = MavenPlugin.getMavenConfiguration();
    final IMavenProjectRegistry mavenProjectRegistry = MavenPlugin.getMavenProjectRegistry();
    final IProjectConfigurationManager projectConfigurationManager =
        MavenPlugin.getProjectConfigurationManager();

    final String groupId = op.getGroupId().content();
    final String artifactId = op.getProjectName().content();
    final String version = op.getArtifactVersion().content();
    final String javaPackage = op.getGroupId().content();
    final String activeProfilesValue = op.getActiveProfilesValue().content();
    final PluginType pluginType = op.getPluginType().content(true);
    final IPortletFramework portletFramework = op.getPortletFramework().content(true);
    final String frameworkName = getFrameworkName(op);

    IPath location = PathBridge.create(op.getLocation().content());

    // for location we should use the parent location
    if (location.lastSegment().equals(artifactId)) {
      // use parent dir since maven archetype will generate new dir under this location
      location = location.removeLastSegments(1);
    }

    String archetypeType = null;

    if (pluginType.equals(PluginType.portlet) && portletFramework.isRequiresAdvanced()) {
      archetypeType =
          "portlet-" + frameworkName.replace("_", "-"); // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    } else {
      archetypeType = pluginType.name();
    }

    final String archetypeArtifactId =
        "liferay-" + archetypeType + "-archetype"; // $NON-NLS-1$ //$NON-NLS-2$

    // get latest liferay archetype
    monitor.beginTask(
        "Determining latest Liferay maven plugin archetype version.", IProgressMonitor.UNKNOWN);
    final String archetypeVersion =
        AetherUtil.getLatestAvailableLiferayArtifact(
                LIFERAY_ARCHETYPES_GROUP_ID, archetypeArtifactId)
            .getVersion();

    final Archetype archetype = new Archetype();
    archetype.setArtifactId(archetypeArtifactId);
    archetype.setGroupId(LIFERAY_ARCHETYPES_GROUP_ID);
    archetype.setModelEncoding("UTF-8");
    archetype.setVersion(archetypeVersion);

    final Properties properties = new Properties();

    final ResolverConfiguration resolverConfig = new ResolverConfiguration();

    if (!CoreUtil.isNullOrEmpty(activeProfilesValue)) {
      resolverConfig.setSelectedProfiles(activeProfilesValue);
    }

    final ProjectImportConfiguration configuration = new ProjectImportConfiguration(resolverConfig);

    final List<IProject> newProjects =
        projectConfigurationManager.createArchetypeProjects(
            location,
            archetype,
            groupId,
            artifactId,
            version,
            javaPackage,
            properties,
            configuration,
            monitor);

    if (CoreUtil.isNullOrEmpty(newProjects)) {
      retval =
          LiferayMavenCore.createErrorStatus("New project was not created due to unknown error");
    } else {
      final IProject firstProject = newProjects.get(0);

      // add new profiles if it was specified to add to project or parent poms
      if (!CoreUtil.isNullOrEmpty(activeProfilesValue)) {
        final String[] activeProfiles = activeProfilesValue.split(",");

        // find all profiles that should go in user settings file
        final List<NewLiferayProfile> newUserSettingsProfiles =
            getNewProfilesToSave(
                activeProfiles, op.getNewLiferayProfiles(), ProfileLocation.userSettings);

        if (newUserSettingsProfiles.size() > 0) {
          final String userSettingsFile = mavenConfiguration.getUserSettingsFile();

          String userSettingsPath = null;

          if (CoreUtil.isNullOrEmpty(userSettingsFile)) {
            userSettingsPath = MavenCli.DEFAULT_USER_SETTINGS_FILE.getAbsolutePath();
          } else {
            userSettingsPath = userSettingsFile;
          }

          try {
            // backup user's settings.xml file
            final File settingsXmlFile = new File(userSettingsPath);
            final File backupFile = getBackupFile(settingsXmlFile);

            FileUtils.copyFile(settingsXmlFile, backupFile);

            final DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
            final DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
            final Document pomDocument = docBuilder.parse(settingsXmlFile.getCanonicalPath());

            for (NewLiferayProfile newProfile : newUserSettingsProfiles) {
              MavenUtil.createNewLiferayProfileNode(pomDocument, newProfile, archetypeVersion);
            }

            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
        final List<NewLiferayProfile> newProjectPomProfiles =
            getNewProfilesToSave(
                activeProfiles, op.getNewLiferayProfiles(), ProfileLocation.projectPom);

        // only need to set the first project as nested projects should pickup the parent setting
        final IMavenProjectFacade newMavenProject = mavenProjectRegistry.getProject(firstProject);

        final IFile pomFile = newMavenProject.getPom();

        try {
          final IDOMModel domModel =
              (IDOMModel) StructuredModelManager.getModelManager().getModelForEdit(pomFile);

          for (final NewLiferayProfile newProfile : newProjectPomProfiles) {
            MavenUtil.createNewLiferayProfileNode(
                domModel.getDocument(), newProfile, archetypeVersion);
          }

          domModel.save();

          domModel.releaseFromEdit();
        } catch (IOException e) {
          LiferayMavenCore.logError("Unable to save new Liferay profiles to project pom.", e);
        }

        for (final 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);
          }
        }
      }

      if (op.getPluginType().content().equals(PluginType.portlet)) {
        final String portletName = op.getPortletName().content(false);
        retval =
            portletFramework.postProjectCreated(firstProject, frameworkName, portletName, monitor);
      }
    }

    if (retval == null) {
      retval = Status.OK_STATUS;
    }

    return retval;
  }