Home » Eclipse Projects » Eclipse Platform » Create a plugin manually
| | | |
Re: Create a plugin manually [message #330733 is a reply to message #330730] |
Tue, 12 August 2008 11:54 |
Ed Merks Messages: 33264 Registered: July 2009 |
Senior Member |
|
|
This is a multi-part message in MIME format.
--------------020008060106040302040906
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 7bit
Lukas,
It's also possible to just generate directly the textual artifacts that
make a Java project a plugin. The EMF generators do that to avoid
direct dependencies on the PDE. Here's some horrible code to look at
that creates either a plugin project or plain project. Files like the
plugin.xml and MANIFEST.MF are created textually based on JET templates.
public static IProject createEMFProject
(IPath javaSource,
URI projectLocationURI,
List<IProject> referencedProjects,
Monitor monitor,
int style,
List<?> pluginVariables)
{
IProgressMonitor progressMonitor =
BasicMonitor.toIProgressMonitor(monitor);
String projectName = javaSource.segment(0);
IProject project = null;
try
{
List<IClasspathEntry> classpathEntries = new
UniqueEList<IClasspathEntry>();
progressMonitor.beginTask("", 10);
progressMonitor.subTask
(CodeGenEcorePlugin.INSTANCE.getString
("_UI_CreatingEMFProject_message",
new Object [] { projectName, projectLocationURI !=
null ? projectLocationURI.toString() : projectName }));
IWorkspace workspace = ResourcesPlugin.getWorkspace();
project = workspace.getRoot().getProject(projectName);
// Clean up any old project information.
//
if (!project.exists())
{
URI location = projectLocationURI;
if (location == null)
{
location =
URI.createFileURI(workspace.getRoot().getLocation().append(p rojectName).toOSString());
}
location = location.appendSegment(".project");
File projectFile = new File(location.toString());
if (projectFile.exists())
{
projectFile.renameTo(new File(location.toString() +
".old"));
}
}
IJavaProject javaProject = JavaCore.create(project);
IProjectDescription projectDescription = null;
if (!project.exists())
{
projectDescription =
ResourcesPlugin.getWorkspace().newProjectDescription(project Name);
if (projectLocationURI != null)
{
projectDescription.setLocationURI(new
java.net.URI(projectLocationURI.toString()));
}
project.create(projectDescription, new
SubProgressMonitor(progressMonitor, 1));
project.open(new SubProgressMonitor(progressMonitor, 1));
}
else
{
projectDescription = project.getDescription();
project.open(new SubProgressMonitor(progressMonitor, 1));
if (project.hasNature(JavaCore.NATURE_ID))
{
classpathEntries.addAll(Arrays.asList(javaProject.getRawClas spath()));
}
}
boolean isInitiallyEmpty = classpathEntries.isEmpty();
{
if (referencedProjects.size() != 0 && (style &
(EMF_PLUGIN_PROJECT_STYLE | EMF_EMPTY_PROJECT_STYLE)) == 0)
{
projectDescription.setReferencedProjects
(referencedProjects.toArray(new
IProject[referencedProjects.size()]));
for (IProject referencedProject : referencedProjects)
{
IClasspathEntry referencedProjectClasspathEntry =
JavaCore.newProjectEntry(referencedProject.getFullPath());
classpathEntries.add(referencedProjectClasspathEntry);
}
}
String [] natureIds = projectDescription.getNatureIds();
if (natureIds == null)
{
natureIds = new String [] { JavaCore.NATURE_ID,
"org.eclipse.pde.PluginNature" };
}
else
{
if (!project.hasNature(JavaCore.NATURE_ID))
{
String [] oldNatureIds = natureIds;
natureIds = new String [oldNatureIds.length + 1];
System.arraycopy(oldNatureIds, 0, natureIds, 0,
oldNatureIds.length);
natureIds[oldNatureIds.length] = JavaCore.NATURE_ID;
}
if (!project.hasNature("org.eclipse.pde.PluginNature"))
{
String [] oldNatureIds = natureIds;
natureIds = new String [oldNatureIds.length + 1];
System.arraycopy(oldNatureIds, 0, natureIds, 0,
oldNatureIds.length);
natureIds[oldNatureIds.length] =
"org.eclipse.pde.PluginNature";
}
}
projectDescription.setNatureIds(natureIds);
ICommand [] builders = projectDescription.getBuildSpec();
if (builders == null)
{
builders = new ICommand [0];
}
boolean hasManifestBuilder = false;
boolean hasSchemaBuilder = false;
for (int i = 0; i < builders.length; ++i)
{
if
("org.eclipse.pde.ManifestBuilder".equals(builders[i].getBuilderName()))
{
hasManifestBuilder = true;
}
if
("org.eclipse.pde.SchemaBuilder".equals(builders[i].getBuilderName()))
{
hasSchemaBuilder = true;
}
}
if (!hasManifestBuilder)
{
ICommand [] oldBuilders = builders;
builders = new ICommand [oldBuilders.length + 1];
System.arraycopy(oldBuilders, 0, builders, 0,
oldBuilders.length);
builders[oldBuilders.length] =
projectDescription.newCommand();
builders[oldBuilders.length].setBuilderName("org.eclipse.pde.ManifestBuilder ");
}
if (!hasSchemaBuilder)
{
ICommand [] oldBuilders = builders;
builders = new ICommand [oldBuilders.length + 1];
System.arraycopy(oldBuilders, 0, builders, 0,
oldBuilders.length);
builders[oldBuilders.length] =
projectDescription.newCommand();
builders[oldBuilders.length].setBuilderName("org.eclipse.pde.SchemaBuilder ");
}
projectDescription.setBuildSpec(builders);
project.setDescription(projectDescription, new
SubProgressMonitor(progressMonitor, 1));
IContainer sourceContainer = project;
if (javaSource.segmentCount() > 1)
{
IPath sourceContainerPath =
javaSource.removeFirstSegments(1).makeAbsolute();
sourceContainer = project.getFolder(sourceContainerPath);
if (!sourceContainer.exists())
{
for (int i = sourceContainerPath.segmentCount() - 1; i
>= 0; i--)
{
sourceContainer =
project.getFolder(sourceContainerPath.removeLastSegments(i)) ;
if (!sourceContainer.exists())
{
((IFolder)sourceContainer).create(false, true, new
SubProgressMonitor(progressMonitor, 1));
}
}
}
IClasspathEntry sourceClasspathEntry =
JavaCore.newSourceEntry(javaSource);
for (Iterator<IClasspathEntry> i =
classpathEntries.iterator(); i.hasNext(); )
{
IClasspathEntry classpathEntry = i.next();
if (classpathEntry.getPath().isPrefixOf(javaSource))
{
i.remove();
}
}
classpathEntries.add(0, sourceClasspathEntry);
}
if (isInitiallyEmpty)
{
IClasspathEntry jreClasspathEntry =
JavaCore.newVariableEntry
(new Path(JavaRuntime.JRELIB_VARIABLE), new
Path(JavaRuntime.JRESRC_VARIABLE), new
Path(JavaRuntime.JRESRCROOT_VARIABLE));
for (Iterator<IClasspathEntry> i =
classpathEntries.iterator(); i.hasNext(); )
{
IClasspathEntry classpathEntry = i.next();
if
(classpathEntry.getPath().isPrefixOf(jreClasspathEntry.getPa th()))
{
i.remove();
}
}
String jreContainer = JavaRuntime.JRE_CONTAINER;
String complianceLevel =
CodeGenUtil.EclipseUtil.getJavaComplianceLevel(project);
if ("1.5".equals(complianceLevel))
{
jreContainer +=
" /org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J 2SE-1.5 ";
}
else if ("1.6".equals(complianceLevel))
{
jreContainer +=
" /org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J avaSE-1.6 ";
}
classpathEntries.add(JavaCore.newContainerEntry(new
Path(jreContainer)));
}
if ((style & EMF_EMPTY_PROJECT_STYLE) == 0)
{
if ((style & EMF_PLUGIN_PROJECT_STYLE) != 0)
{
classpathEntries.add(JavaCore.newContainerEntry(new
Path("org.eclipse.pde.core.requiredPlugins")));
// Remove variables since the plugin.xml should
provide the complete path information.
//
for (Iterator<IClasspathEntry> i =
classpathEntries.iterator(); i.hasNext(); )
{
IClasspathEntry classpathEntry = i.next();
if (classpathEntry.getEntryKind() ==
IClasspathEntry.CPE_VARIABLE &&
!JavaRuntime.JRELIB_VARIABLE.equals(classpathEntry.getPath() .toString())
||
classpathEntry.getEntryKind() ==
IClasspathEntry.CPE_PROJECT)
{
i.remove();
}
}
}
else
{
CodeGenUtil.EclipseUtil.addClasspathEntries(classpathEntries ,
"ECLIPSE_CORE_RUNTIME", "org.eclipse.core.runtime");
CodeGenUtil.EclipseUtil.addClasspathEntries(classpathEntries ,
"ECLIPSE_CORE_RESOURCES", "org.eclipse.core.resources");
CodeGenUtil.EclipseUtil.addClasspathEntries(classpathEntries ,
"EMF_COMMON", "org.eclipse.emf.common");
CodeGenUtil.EclipseUtil.addClasspathEntries(classpathEntries ,
"EMF_ECORE", "org.eclipse.emf.ecore");
if ((style & EMF_XML_PROJECT_STYLE) != 0)
{
CodeGenUtil.EclipseUtil.addClasspathEntries(classpathEntries ,
"EMF_ECORE_XMI", "org.eclipse.emf.ecore.xmi");
}
if ((style & EMF_MODEL_PROJECT_STYLE) == 0)
{
CodeGenUtil.EclipseUtil.addClasspathEntries(classpathEntries ,
"EMF_EDIT", "org.eclipse.emf.edit");
if ((style & EMF_EDIT_PROJECT_STYLE) == 0)
{
CodeGenUtil.EclipseUtil.addClasspathEntries(classpathEntries ,
"ECLIPSE_SWT", "org.eclipse.swt");
CodeGenUtil.EclipseUtil.addClasspathEntries(classpathEntries ,
"ECLIPSE_JFACE", "org.eclipse.jface");
CodeGenUtil.EclipseUtil.addClasspathEntries(classpathEntries ,
"ECLIPSE_UI_VIEWS", "org.eclipse.ui.views");
CodeGenUtil.EclipseUtil.addClasspathEntries(classpathEntries ,
"ECLIPSE_UI_EDITORS", "org.eclipse.ui.editors");
CodeGenUtil.EclipseUtil.addClasspathEntries(classpathEntries ,
"ECLIPSE_UI_IDE", "org.eclipse.ui.ide");
CodeGenUtil.EclipseUtil.addClasspathEntries(classpathEntries ,
"ECLIPSE_UI_WORKBENCH", "org.eclipse.ui.workbench");
CodeGenUtil.EclipseUtil.addClasspathEntries(classpathEntries ,
"EMF_COMMON_UI", "org.eclipse.emf.common.ui");
CodeGenUtil.EclipseUtil.addClasspathEntries(classpathEntries ,
"EMF_EDIT_UI", "org.eclipse.emf.edit.ui");
if ((style & EMF_XML_PROJECT_STYLE) == 0)
{
CodeGenUtil.EclipseUtil.addClasspathEntries(classpathEntries ,
"EMF_ECORE_XMI", "org.eclipse.emf.ecore.xmi");
}
}
}
if ((style & EMF_TESTS_PROJECT_STYLE) != 0)
{
CodeGenUtil.EclipseUtil.addClasspathEntries(classpathEntries ,
"JUNIT", "org.junit");
}
if (pluginVariables != null)
{
for (Iterator<?> i = pluginVariables.iterator();
i.hasNext(); )
{
Object variable = i.next();
if (variable instanceof IClasspathEntry)
{
classpathEntries.add((IClasspathEntry)variable);
}
else if (variable instanceof String)
{
String pluginVariable = (String)variable;
String name;
String id;
int index = pluginVariable.indexOf("=");
if (index == -1)
{
name =
pluginVariable.replace('.','_').toUpperCase();
id = pluginVariable;
}
else
{
name = pluginVariable.substring(0, index);
id = pluginVariable.substring(index + 1);
}
CodeGenUtil.EclipseUtil.addClasspathEntries(classpathEntries , name, id);
}
}
}
}
}
javaProject.setRawClasspath
(classpathEntries.toArray(new
IClasspathEntry[classpathEntries.size()]),
new SubProgressMonitor(progressMonitor, 1));
}
if (isInitiallyEmpty)
{
javaProject.setOutputLocation(new Path("/" +
javaSource.segment(0) + "/bin"), new
SubProgressMonitor(progressMonitor, 1));
}
}
catch (Exception exception)
{
exception.printStackTrace();
CodeGenEcorePlugin.INSTANCE.log(exception);
}
finally
{
progressMonitor.done();
}
return project;
}
Daniel Megert wrote:
> Lukas Witschi wrote:
>>
>> Yes. The goal is to generate a serviceable plugin, which you can
>> import into eclipse.
> Take a look at the code in
> org.eclipse.pde.internal.ui.wizards.plugin.NewProjectCreatio nOperation
>
> Dani
>>
>> -> Lukas
>>
--------------020008060106040302040906
Content-Type: text/html; charset=ISO-8859-15
Content-Transfer-Encoding: 8bit
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-15"
http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Lukas,<br>
<br>
It's also possible to just generate directly the textual artifacts that
make a Java project a plugin.
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
| |
Goto Forum:
Current Time: Sun Dec 22 12:01:42 GMT 2024
Powered by FUDForum. Page generated in 0.04140 seconds
|