Search in sources :

Example 6 with Model

use of org.eclipse.che.ide.maven.tools.Model in project che by eclipse.

the class SimpleGeneratorStrategy method generateProject.

@Override
public void generateProject(Path projectPath, Map<String, AttributeValue> attributes, Map<String, String> options) throws ForbiddenException, ConflictException, ServerException {
    AttributeValue artifactId = attributes.get(ARTIFACT_ID);
    AttributeValue groupId = attributes.get(GROUP_ID);
    AttributeValue version = attributes.get(VERSION);
    if (artifactId == null) {
        throw new ConflictException("Missed required attribute artifactId");
    }
    if (groupId == null) {
        throw new ConflictException("Missed required attribute groupId");
    }
    if (version == null) {
        throw new ConflictException("Missed required attribute version");
    }
    Model model = Model.createModel();
    model.setModelVersion("4.0.0");
    final FolderEntry baseFolder = new FolderEntry(vfs.getRoot().createFolder(projectPath.toString()));
    if (baseFolder.getChild("pom.xml") == null) {
        baseFolder.createFile("pom.xml", new byte[0]);
    }
    AttributeValue parentArtifactId = attributes.get(PARENT_ARTIFACT_ID);
    if (parentArtifactId != null) {
        model.setArtifactId(parentArtifactId.getString());
    }
    AttributeValue parentGroupId = attributes.get(PARENT_GROUP_ID);
    if (parentGroupId != null) {
        model.setGroupId(parentGroupId.getString());
    }
    AttributeValue parentVersion = attributes.get(PARENT_VERSION);
    if (parentVersion != null) {
        model.setVersion(parentVersion.getString());
    }
    model.setArtifactId(artifactId.getString());
    model.setGroupId(groupId.getString());
    model.setVersion(version.getString());
    AttributeValue packaging = attributes.get(PACKAGING);
    if (packaging != null) {
        model.setPackaging(packaging.getString());
    }
    AttributeValue sourceFolders = attributes.get(SOURCE_FOLDER);
    if (sourceFolders != null) {
        final String sourceFolder = sourceFolders.getString();
        baseFolder.createFolder(sourceFolder);
        if (!DEFAULT_SOURCE_FOLDER.equals(sourceFolder)) {
            model.setBuild(new Build().setSourceDirectory(sourceFolder));
        }
    }
    AttributeValue testSourceFolders = attributes.get(TEST_SOURCE_FOLDER);
    if (testSourceFolders != null) {
        final String testSourceFolder = testSourceFolders.getString();
        baseFolder.createFolder(testSourceFolder);
        if (!DEFAULT_TEST_SOURCE_FOLDER.equals(testSourceFolder)) {
            Build build = model.getBuild();
            if (build != null) {
                build.setTestSourceDirectory(testSourceFolder);
            } else {
                model.setBuild(new Build().setTestSourceDirectory(testSourceFolder));
            }
        }
    }
    model.writeTo(baseFolder.getChild("pom.xml").getVirtualFile());
}
Also used : AttributeValue(org.eclipse.che.api.project.server.type.AttributeValue) ConflictException(org.eclipse.che.api.core.ConflictException) Build(org.eclipse.che.ide.maven.tools.Build) FolderEntry(org.eclipse.che.api.project.server.FolderEntry) Model(org.eclipse.che.ide.maven.tools.Model)

Example 7 with Model

use of org.eclipse.che.ide.maven.tools.Model in project che by eclipse.

the class WorkspaceTest method testRemovingModule.

@Test
public void testRemovingModule() throws Exception {
    FolderEntry parentFolder = createMultimoduleProject();
    IProject parent = ResourcesPlugin.getWorkspace().getRoot().getProject("parent");
    mavenWorkspace.update(Collections.singletonList(parent));
    mavenWorkspace.waitForUpdate();
    assertThat(projectRegistry.getProjects()).hasSize(3).onProperty("path").containsOnly("/parent", "/parent/module1", "/parent/module2");
    VirtualFile parentPom = parentFolder.getChild("pom.xml").getVirtualFile();
    Model model = Model.readFrom(parentPom);
    List<String> modules = new ArrayList<>(model.getModules());
    ListIterator<String> listIterator = modules.listIterator();
    while (listIterator.hasNext()) {
        if ("module2".equals(listIterator.next())) {
            listIterator.remove();
            break;
        }
    }
    model.setModules(modules);
    model.writeTo(parentPom);
    mavenWorkspace.update(Collections.singletonList(parent));
    mavenWorkspace.waitForUpdate();
    assertThat(projectRegistry.getProjects()).hasSize(2).onProperty("path").containsOnly("/parent", "/parent/module1");
}
Also used : VirtualFile(org.eclipse.che.api.vfs.VirtualFile) FolderEntry(org.eclipse.che.api.project.server.FolderEntry) Model(org.eclipse.che.ide.maven.tools.Model) ArrayList(java.util.ArrayList) IProject(org.eclipse.core.resources.IProject) Test(org.testng.annotations.Test) MavenServerManagerTest(org.eclipse.che.plugin.maven.server.rmi.MavenServerManagerTest)

Example 8 with Model

use of org.eclipse.che.ide.maven.tools.Model in project che by eclipse.

the class MavenProjectTypeTest method testMavenProject.

@Test
public void testMavenProject() throws Exception {
    WorkspaceDto usersWorkspaceMock = mock(WorkspaceDto.class);
    WorkspaceConfigDto workspaceConfigMock = mock(WorkspaceConfigDto.class);
    when(httpJsonRequestFactory.fromLink(eq(DtoFactory.newDto(Link.class).withMethod("GET").withHref("/workspace/")))).thenReturn(httpJsonRequest);
    when(httpJsonRequestFactory.fromLink(eq(DtoFactory.newDto(Link.class).withMethod("PUT").withHref("/workspace/" + "/project")))).thenReturn(httpJsonRequest);
    when(httpJsonRequest.request()).thenReturn(httpJsonResponse);
    when(httpJsonResponse.asDto(WorkspaceDto.class)).thenReturn(usersWorkspaceMock);
    final ProjectConfigDto projectConfig = DtoFactory.getInstance().createDto(ProjectConfigDto.class).withName("project").withPath("/myProject").withType(MavenAttributes.MAVEN_ID);
    when(usersWorkspaceMock.getConfig()).thenReturn(workspaceConfigMock);
    when(workspaceConfigMock.getProjects()).thenReturn(Collections.singletonList(projectConfig));
    Map<String, List<String>> attributes = new HashMap<>();
    attributes.put(MavenAttributes.ARTIFACT_ID, Collections.singletonList("myartifact"));
    attributes.put(MavenAttributes.GROUP_ID, Collections.singletonList("mygroup"));
    attributes.put(MavenAttributes.VERSION, Collections.singletonList("1.0"));
    attributes.put(MavenAttributes.PACKAGING, Collections.singletonList("jar"));
    RegisteredProject project = pm.createProject(DtoFactory.getInstance().createDto(ProjectConfigDto.class).withType("maven").withAttributes(attributes).withPath("/myProject").withName("myProject"), new HashMap<>(0));
    for (VirtualFileEntry file : project.getBaseFolder().getChildren()) {
        if (file.getName().equals("pom.xml")) {
            Model pom = Model.readFrom(file.getVirtualFile().getContent());
            Assert.assertEquals(pom.getVersion(), "1.0");
        }
    }
}
Also used : HashMap(java.util.HashMap) ProjectConfigDto(org.eclipse.che.api.workspace.shared.dto.ProjectConfigDto) Model(org.eclipse.che.ide.maven.tools.Model) VirtualFileEntry(org.eclipse.che.api.project.server.VirtualFileEntry) WorkspaceDto(org.eclipse.che.api.workspace.shared.dto.WorkspaceDto) WorkspaceConfigDto(org.eclipse.che.api.workspace.shared.dto.WorkspaceConfigDto) List(java.util.List) RegisteredProject(org.eclipse.che.api.project.server.RegisteredProject) Test(org.junit.Test)

Aggregations

Model (org.eclipse.che.ide.maven.tools.Model)8 ArrayList (java.util.ArrayList)4 FolderEntry (org.eclipse.che.api.project.server.FolderEntry)3 IProject (org.eclipse.core.resources.IProject)3 IOException (java.io.IOException)2 List (java.util.List)2 ZipFile (java.util.zip.ZipFile)2 RegisteredProject (org.eclipse.che.api.project.server.RegisteredProject)2 VirtualFileEntry (org.eclipse.che.api.project.server.VirtualFileEntry)2 VirtualFile (org.eclipse.che.api.vfs.VirtualFile)2 Build (org.eclipse.che.ide.maven.tools.Build)2 MavenProjectProblem (org.eclipse.che.maven.data.MavenProjectProblem)2 Inject (com.google.inject.Inject)1 ApiOperation (io.swagger.annotations.ApiOperation)1 ApiParam (io.swagger.annotations.ApiParam)1 ApiResponse (io.swagger.annotations.ApiResponse)1 ApiResponses (io.swagger.annotations.ApiResponses)1 File (java.io.File)1 FilenameFilter (java.io.FilenameFilter)1 Path (java.nio.file.Path)1