org.activiti.designer.eclipse.ui.CreateActivitiProjectWizard Maven / Gradle / Ivy
The newest version!
/**
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.activiti.designer.eclipse.ui;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import org.activiti.designer.eclipse.common.ActivitiPlugin;
import org.activiti.designer.util.ActivitiConstants;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.core.ClasspathContainerInitializer;
import org.eclipse.jdt.core.IClasspathContainer;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.launching.JavaRuntime;
import org.eclipse.ui.wizards.newresource.BasicNewProjectResourceWizard;
public class CreateActivitiProjectWizard extends BasicNewProjectResourceWizard {
@Override
public boolean performFinish() {
if (!super.performFinish()) {
return false;
}
IProject newProject = getNewProject();
try {
IProjectDescription description = newProject.getDescription();
String[] newNatures = new String[2];
newNatures[0] = JavaCore.NATURE_ID;
newNatures[1] = ActivitiConstants.NATURE_ID;
description.setNatureIds(newNatures);
newProject.setDescription(description, null);
IJavaProject javaProject = JavaCore.create(newProject);
createSourceFolders(newProject);
createOutputLocation(javaProject);
IFile pomFile = newProject.getFile("pom.xml");
InputStream pomSource = new ByteArrayInputStream(createPOMFile().getBytes());
pomFile.create(pomSource, true, null);
pomSource.close();
String[] userLibraryNames = JavaCore.getUserLibraryNames();
boolean activitiExtensionLibraryPresent = false;
if(userLibraryNames != null && userLibraryNames.length > 0) {
for(String userLibraryName : userLibraryNames) {
if(ActivitiPlugin.USER_LIBRARY_NAME_EXTENSIONS.equals(userLibraryName)) {
activitiExtensionLibraryPresent = true;
}
}
}
if(activitiExtensionLibraryPresent == false) {
ClasspathContainerInitializer initializer = JavaCore.getClasspathContainerInitializer(JavaCore.USER_LIBRARY_CONTAINER_ID);
IPath containerPath = new Path(JavaCore.USER_LIBRARY_CONTAINER_ID);
initializer.requestClasspathContainerUpdate(containerPath.append(ActivitiPlugin.USER_LIBRARY_NAME_EXTENSIONS),
null, new IClasspathContainer() {
@Override
public IPath getPath() {
return new Path(JavaCore.USER_LIBRARY_CONTAINER_ID).append(ActivitiPlugin.USER_LIBRARY_NAME_EXTENSIONS) ;
}
@Override
public int getKind() {
return K_APPLICATION;
}
@Override
public String getDescription() {
return ActivitiPlugin.USER_LIBRARY_NAME_EXTENSIONS;
}
@Override
public IClasspathEntry[] getClasspathEntries() {
return new IClasspathEntry[] {};
}
});
}
IClasspathEntry[] entries = createClasspathEntries(javaProject);
javaProject.setRawClasspath(entries, null);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
private IClasspathEntry[] createClasspathEntries(IJavaProject javaProject) {
IPath srcPath1 = javaProject.getPath().append("src/main/java");
IPath srcPath2 = javaProject.getPath().append("src/main/resources");
IPath srcPath3 = javaProject.getPath().append("src/test/java");
IPath srcPath4 = javaProject.getPath().append("src/test/resources");
IPath[] javaPath = new IPath[] { new Path("**/*.java") };
IPath testOutputLocation = javaProject.getPath().append("target/test-classes");
IPath srcPathUserLibrary = new Path(ActivitiPlugin.DESIGNER_EXTENSIONS_USER_LIB_PATH);
IClasspathEntry[] entries = { JavaCore.newSourceEntry(srcPath1, javaPath, null, null),
JavaCore.newSourceEntry(srcPath2, javaPath),
JavaCore.newSourceEntry(srcPath3, javaPath, null, testOutputLocation),
JavaCore.newSourceEntry(srcPath4, javaPath, testOutputLocation),
JavaRuntime.getDefaultJREContainerEntry(), JavaCore.newContainerEntry(srcPathUserLibrary) };
return entries;
}
private void createSourceFolders(IProject project) throws CoreException {
List sourceFolders = Collections.synchronizedList(new LinkedList());
sourceFolders.add("src");
sourceFolders.add("src/main");
sourceFolders.add("src/main/java");
sourceFolders.add("src/main/resources/");
sourceFolders.add(ActivitiConstants.DIAGRAM_FOLDER);
sourceFolders.add("src/test/");
sourceFolders.add("src/test/java/");
sourceFolders.add("src/test/resources");
for (String folder : sourceFolders) {
IFolder sourceFolder = project.getFolder(folder);
sourceFolder.create(false, true, null);
}
}
private void createOutputLocation(IJavaProject javaProject) throws JavaModelException {
IPath targetPath = javaProject.getPath().append("target/classes");
javaProject.setOutputLocation(targetPath, null);
}
private String createPOMFile() {
StringBuilder buffer = new StringBuilder();
buffer.append("\n");
buffer.append(" 4.0.0 \n");
buffer.append(" org.activiti.examples \n");
buffer.append(" activiti-examples \n");
buffer.append(" 1.0-SNAPSHOT \n");
buffer.append(" jar \n");
buffer.append(" BPMN 2.0 with Activiti - Examples \n");
buffer.append(" \n");
buffer.append(" 5.9 \n");
buffer.append(" \n");
buffer.append(" \n");
addDependency(buffer, "org.activiti", "activiti-engine", "${activiti-version}");
addDependency(buffer, "org.activiti", "activiti-spring", "${activiti-version}");
addDependency(buffer, "org.codehaus.groovy", "groovy", "1.7.5");
addDependency(buffer, "hsqldb", "hsqldb", "1.8.0.7");
addDependency(buffer, "com.h2database", "h2", "1.2.132");
addDependency(buffer, "junit", "junit", "4.8.1");
buffer.append(" \n");
buffer.append(" \n");
buffer.append(" \n");
buffer.append(" Activiti \n");
buffer.append(" http://maven.alfresco.com/nexus/content/repositories/activiti \n");
buffer.append(" \n");
buffer.append(" \n");
buffer.append(" \n");
buffer.append(" \n");
buffer.append(" \n");
buffer.append(" org.apache.maven.plugins \n");
buffer.append(" maven-compiler-plugin \n");
buffer.append(" 2.3.2 \n");
buffer.append(" \n");
buffer.append(" \n");
buffer.append(" 1.6 \n");
buffer.append(" \n");
buffer.append(" \n");
buffer.append(" \n");
buffer.append(" org.apache.maven.plugins \n");
buffer.append(" maven-eclipse-plugin \n");
buffer.append(" true \n");
buffer.append(" \n");
buffer.append(" \n");
buffer.append(" ");
buffer.append(ActivitiPlugin.DESIGNER_EXTENSIONS_USER_LIB_PATH);
buffer.append(" \n");
buffer.append(" \n");
buffer.append(" \n");
buffer.append(" \n");
buffer.append(" \n");
buffer.append(" \n");
buffer.append(" \n");
return buffer.toString();
}
private void addDependency(StringBuilder buffer, String groupId, String artifactId, String version) {
buffer.append(" \n")
.append(" ")
.append(groupId)
.append(" \n")
.append(" ")
.append(artifactId)
.append(" \n")
.append(" ")
.append(version)
.append(" \n")
.append(" \n");
}
}