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());
}
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");
}
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");
}
}
}
Aggregations