Home » Language IDEs » Java Development Tools (JDT) » Creating a web project programmatically
|
Re: Creating a web project programmatically [message #1017233 is a reply to message #1016576] |
Mon, 11 March 2013 09:25 |
|
Hi,
If you want to create an Eclispe project programmatically, you will need to start by creating the "Project" and then add its natures and finally you will need to configure additionnal details. In order to create the project, your plug-in will need a dependency to the bundle "org.eclipse.core.resources" (MANIFEST.MF, dependencies tab).
Creation of the project:
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
if (!project.exists()) {
project.create(monitor);
project.open(monitor);
}
Configuration of its natures:
// Configure the project to be a Java project and a maven project
IProjectDescription description = project.getDescription();
description.setNatureIds(new String[] {JavaCore.NATURE_ID, "org.eclipse.m2e.core.maven2Nature" });
// Other popular project natures for a Java/JEE/Maven project
// "org.eclipse.jem.workbench.JavaEMFNature", "org.eclipse.wst.jsdt.core.jsNature",
// "org.eclipse.wst.common.modulecore.ModuleCoreNature",
// "org.eclipse.wst.common.project.facet.core.nature",
// "org.eclipse.m2e.core.maven2Nature"
project.setDescription(description, monitor);
In your case, you would need to add the "Web Project" nature. You can find out the nature that you need to add by looking at the file ".project" located at the root of every Eclispe project. In a Web project, you would see the identifier of the nature of a web project. For more information on the configuration of a Java project, have a look here (section 1.3).
You may want to create some folders inside this project, for example, in a Java/maven project you may want to have the following folder (src/main/java):
// src
IFolder src = project.getFolder("src");
src.create(true, true, monitor);
// src/main
IFolder srcMain = src.getFolder("main");
srcMain.create(true, true, monitor);
// src/main/java
IFolder srcMainJava = srcMain.getFolder("java");
srcMainJava.create(true, true, monitor);
Then you may want to configure the classpath of your project, to have for example, the JDK configured along with the source folders. We have created the src/main/java folder but it is not yet considered as a source folder.
IJavaProject javaProject = JavaCore.create(project);
// Let's add JavaSE-1.6 to our classpath
List<IClasspathEntry> entries = new ArrayList<IClasspathEntry>();
IExecutionEnvironmentsManager executionEnvironmentsManager = JavaRuntime.getExecutionEnvironmentsManager();
IExecutionEnvironment[] executionEnvironments = executionEnvironmentsManager.getExecutionEnvironments();
for (IExecutionEnvironment iExecutionEnvironment : executionEnvironments) {
// We will look for JavaSE-1.6 as the JRE container to add to our classpath
if ("JavaSE-1.6".equals(iExecutionEnvironment.getId())) {
entries.add(JavaCore.newContainerEntry(JavaRuntime.newJREContainerPath(iExecutionEnvironment)));
break;
}
}
// Let's add the maven container to our classpath to let the maven plug-in add the dependencies computed from a pom.xml file to our classpath
IClasspathEntry mavenEntry = JavaCore.newContainerEntry(new Path("org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"), new IAccessRule[0], new IClasspathAttribute[] {JavaCore.newClasspathAttribute("org.eclipse.jst.component.dependency", "/WEB-INF/lib") }, false);
entries.add(mavenEntry);
javaProject.setRawClasspath(entries.toArray(new IClasspathEntry[entries.size()]), null);
// Let's create our target/classes output folder
IFolder target = project.getFolder("target");
target.create(true, true, monitor);
IFolder classes = target.getFolder("classes");
classes.create(true, true, monitor);
// Let's add target/classes as our output folder for compiled ".class"
javaProject.setOutputLocation(classes.getFullPath(), monitor);
// Now let's add our source folder and output folder to our classpath
IClasspathEntry[] oldEntries = javaProject.getRawClasspath();
// +1 for our src/main/java entry
IClasspathEntry[] newEntries = new IClasspathEntry[oldEntries.length + 1];
System.arraycopy(oldEntries, 0, newEntries, 0, oldEntries.length);
IPackageFragmentRoot packageRoot = javaProject.getPackageFragmentRoot(srcMainJava);
newEntries[oldEntries.length] = JavaCore.newSourceEntry(packageRoot.getPath(), new Path[] {}, new Path[] {}, classes.getFullPath());
javaProject.setRawClasspath(newEntries, null);
With all this, you should have a good Java/Maven project with its classpath setup, if you want to add the Web project nature, I gave you some identifier of the possible natures that you can add but have a look at the ".project" file of a web project first to known everything you need to configure. There may be additionnal folders, files and settings to generate a perfect Web project, but with this you are ready to start. For more information on the use of the Java Development Tools, you can have a look here.
Regards,
Stephane Begaudeau, Obeo
--
Twitter: @sbegaudeau
Google+: +stephane.begaudeau
Blog: http://stephanebegaudeau.tumblr.com | Eclipse Java Development Tools Tips and Tricks
|
|
|
Re: Creating a web project programmatically [message #1058748 is a reply to message #1017233] |
Tue, 14 May 2013 20:55 |
Kevin Regan Messages: 33 Registered: May 2013 |
Member |
|
|
Hi I am trying to do this as well (java project but i don't think it matters). My code is very similar to the code posted above but I am getting an exception when trying to set the output location (it happens for other calls as well and seems to be a fundamental problem with the project object):
IProgressMonitor progressMonitor = new NullProgressMonitor();
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
if (project.exists()) {
return;
}
try {
project.create(progressMonitor);
project.open(progressMonitor);
IProjectDescription description = project.getDescription();
description.setNatureIds(new String[] {JavaCore.NATURE_ID});
IJavaProject javaProject = JavaCore.create(project);
IFolder binFolder = project.getFolder("bin");
binFolder.create(false, true, progressMonitor);
System.out.println(binFolder.getFullPath());
System.out.println(binFolder.getRawLocation().makeAbsolute().toString());
javaProject.setOutputLocation(binFolder.getFullPath(), null);
//javaProject.setOutputLocation(binFolder.getRawLocation().makeAbsolute(), null);
The setOutputLocation fails with the following exception:
Java Model Exception: Java Model Status [KmrProject does not exist]
at org.eclipse.jdt.internal.core.JavaElement.newNotPresentException(JavaElement.java:495)
at org.eclipse.jdt.internal.core.JavaModelManager.getPerProjectInfoCheckExistence(JavaModelManager.java:2285)
at org.eclipse.jdt.internal.core.JavaProject.getPerProjectInfo(JavaProject.java:1907)
at org.eclipse.jdt.internal.core.JavaProject.getOutputLocation(JavaProject.java:1734)
at org.eclipse.jdt.internal.core.JavaProject.setOutputLocation(JavaProject.java:3013)
at kmrstartupplugin.KmrStartupPlugin.earlyStartup(KmrStartupPlugin.java:65)
at org.eclipse.ui.internal.EarlyStartupRunnable.runEarlyStartup(EarlyStartupRunnable.java:87)
at org.eclipse.ui.internal.EarlyStartupRunnable.run(EarlyStartupRunnable.java:66)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
at org.eclipse.ui.internal.Workbench$54.run(Workbench.java:2412)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:53)
Can someone point out what I might be doing wrong? Thanks!
EDIT: I should point out that I get the error regardless of which of my variations of the call to set output location and also that the projectName is "KmrProject" (referenced in the error message).
[Updated on: Tue, 14 May 2013 21:13] Report message to a moderator
|
|
| | |
Goto Forum:
Current Time: Sun Jan 26 01:17:34 GMT 2025
Powered by FUDForum. Page generated in 0.04344 seconds
|