Eclipse 플러그 인 개발 중 자바 프로젝트 를 만 드 는 코드
package com.wisoft.magicube.codegenerator.popup.actions;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.eclipse.core.resources.ICommand;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.ui.PreferenceConstants;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
public class NewJavaProjectAction implements IObjectActionDelegate {
public void run(IAction action) {
//
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
// /////////////////////////////////// ///////////////////////////
final IProject project = root.getProject("xyz");
//
// ,
IPath projectPath = new Path("g:/myplugIn");
IWorkspace workspace = root.getWorkspace();
final IProjectDescription description = workspace.newProjectDescription(project.getName());
description.setLocation(projectPath);
// , java
String[] javaNature = description.getNatureIds();
String[] newJavaNature = new String[javaNature.length + 1];
System.arraycopy(javaNature, 0, newJavaNature, 0, javaNature.length);
newJavaNature[javaNature.length] = "org.eclipse.jdt.core.javanature"; // Java
description.setNatureIds(newJavaNature);
// /////////////////////////////
try {
NullProgressMonitor monitor = new NullProgressMonitor();
project.create(description, monitor);
project.open(IResource.BACKGROUND_REFRESH, new SubProgressMonitor(monitor, 1000));
} catch (CoreException e) {
e.printStackTrace();
}
//
/*
* // ,WorkspaceModifyOperation org.eclipse.ui.ide
* WorkspaceModifyOperation workspaceModifyOperation = new
* WorkspaceModifyOperation() {
*
* @Override protected void execute(IProgressMonitor monitor) throws
* CoreException, InvocationTargetException, InterruptedException { try
* { monitor.beginTask("", ); project.create(description, monitor);
*
* if(monitor.isCanceled()){ throw new OperationCanceledException(); }
*
* project.open(IResource.BACKGROUND_REFRESH, new
* SubProgressMonitor(monitor, )); } catch (Exception e) {
* e.printStackTrace(); } finally{ monitor.done(); } } };
* // IWizard getContainer().run() .
*/
// java
IJavaProject javaProject = JavaCore.create(project);
// ////////////////////////////////// JRE ////////////////////////////
try {
// JRE
IClasspathEntry[] jreLibrary = PreferenceConstants.getDefaultJRELibrary();
// build path
IClasspathEntry[] oldClasspathEntries = javaProject.getRawClasspath();
List list = new ArrayList();
list.addAll(Arrays.asList(jreLibrary));
list.addAll(Arrays.asList(oldClasspathEntries));
javaProject.setRawClasspath(list.toArray(new IClasspathEntry[list.size()]), null);
} catch (JavaModelException e) {
e.printStackTrace();
}
// ////////////////////////////////// /////////////////////////////
IFolder binFolder = javaProject.getProject().getFolder("bin");
try {
binFolder.create(true, true, null);
javaProject.setOutputLocation(binFolder.getFullPath(), null);
} catch (CoreException e) {
e.printStackTrace();
}
// ///////////////////////// Java ///////////////////////
try {
IProjectDescription description2 = javaProject.getProject().getDescription();
ICommand command = description2.newCommand();
command.setBuilderName("org.eclipse.jdt.core.javabuilder");
description2.setBuildSpec(new ICommand[] { command });
description2.setNatureIds(new String[] { "org.eclipse.jdt.core.javanature" });
javaProject.getProject().setDescription(description2, null);
} catch (CoreException e) {
e.printStackTrace();
}
// ///////////////////////////// //////////////////////////
// /////////// , PackageFragmentRoot ////////
IFolder srcFolder = javaProject.getProject().getFolder("src");
try {
srcFolder.create(true, true, null);
// this.createFolder(srcFolder);
// SourceLibrary
IClasspathEntry srcClasspathEntry = JavaCore.newSourceEntry(srcFolder.getFullPath());
// build path
IClasspathEntry[] oldClasspathEntries = javaProject.readRawClasspath();
//
List list = new ArrayList();
list.addAll(Arrays.asList(oldClasspathEntries));
list.add(srcClasspathEntry);
// ,
IClasspathEntry temp = JavaCore.newSourceEntry(new Path("/xyz"));
if (list.contains(temp)) {
list.remove(temp);
}
System.out.println(list.size());
javaProject.setRawClasspath(list.toArray(new IClasspathEntry[list.size()]), null);
} catch (CoreException e) {
e.printStackTrace();
}
// /////////////////////////////// //////////////////////////
// IPackageFragmentRoot packageFragmentRoot = javaProject.getPackageFragmentRoot(javaProject.getResource());
// src
try {
// IPackageFragmentRoot
IPackageFragmentRoot packageFragmentRoot = javaProject.findPackageFragmentRoot(new Path("/xyz/src"));
// IPackageFragmentRoot IPackageFragment,IPackageFragment
IPackageFragment packageFragment = packageFragmentRoot.createPackageFragment("com.aptech.plugin", true, null);
// ////////////////////////////////// Java ////////////////////////
String javaCode = "package com.aptech.plugin;public class HelloWorld{public static void main(String[] args){System.out.println(\" \");}}";
packageFragment.createCompilationUnit("HelloWorld.java", javaCode, true, new NullProgressMonitor());
} catch (JavaModelException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public void setActivePart(IAction action, IWorkbenchPart targetPart) {
}
public void selectionChanged(IAction action, ISelection selection) {
}
}
본 고 는 B3log Solo 를 사용 하여 Noday 에서 동시 발표 한 것 이다.
원본 주소:http://www.noday.net/articles/2011/03/24/1300951113758.html
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.