2008年4月8日火曜日

Javaプロジェクトを作る

プログラム中でJavaプロジェクトを生成する方法。
ライブラリをコピーしたりする部分も含まれているが、、、きにしな~い。

private void doFinish(final String projectName, String srcFolderName,
String binFolderName, String libFolderName,
final IProgressMonitor monitor) throws CoreException {

// プロジェクトを作成
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IProject project = root.getProject(projectName);
project.create(monitor);
project.open(monitor);

// Java Nature をプロジェクトに追加
IProjectDescription description = project.getDescription();
String[] natures = description.getNatureIds();
String[] newNatures = new String[natures.length + 1];
System.arraycopy(natures, 0, newNatures, 0, natures.length);
newNatures[natures.length] = JavaCore.NATURE_ID;
description.setNatureIds(newNatures);
project.setDescription(description, monitor);
// Java プロジェクトを作成
final IJavaProject javaProject = JavaCore.create(project);

Set entries = new HashSet();
// Adding source path
IPath sourcePath = javaProject.getPath().append(srcFolderName);
//ソースフォルダを作成
IFolder sourceDir = project.getFolder(new Path(srcFolderName));
if (!sourceDir.exists()) {
sourceDir.create(false, true, null);
}
// 出力先フォルダを作成
IPath outputPath = javaProject.getPath().append(binFolderName);
IFolder outputDir = project.getFolder(new Path(binFolderName));
if (!outputDir.exists()) {
outputDir.create(false, true, null);
}
// ソースフォルダ、出力フォルダを設定
IClasspathEntry srcEntry = JavaCore.newSourceEntry(sourcePath,
new IPath[] {}, outputPath);
entries.add(srcEntry);

// ライブラリ を追加
final IPath libPath = javaProject.getPath().append(libFolderName);
final IFolder libDir = project.getFolder(new Path(libFolderName));
if (!libDir.exists()) {
libDir.create(false, true, null);
}
final IPath conNativePath = javaProject.getPath().append(
libFolderName + "/native");
IFolder conNativeDir = project.getFolder(new Path(libFolderName
+ "/native"));
if (!conNativeDir.exists()) {
conNativeDir.create(false, true, null);
}
// ライブラリをコピー
String[] jars = new String[] { "gluegen-rt.jar", "jmf.jar", "jogl.jar",
"javassist.jar", "NyARToolKit.jar" };
for (String string : jars) {
InputStream is = getClass().getResourceAsStream(
"/resources/lib/" + string);
IFile f = libDir.getFile(string);
f.create(is, true, null);
}
String[] libs = new String[] { "gluegen-rt.dll", "jmacm.dll",
"jmam.dll", "jmcvid.dll", "jmdaud.dll", "jmdaudc.dll",
"jmddraw.dll", "jmfjawt.dll", "jmg723.dll", "jmgdi.dll",
"jmgsm.dll", "jmh261.dll", "jmh263enc.dll", "jmjpeg.dll",
"jmmci.dll", "jmmpa.dll", "jmmpegv.dll", "jmutil.dll",
"jmvcm.dll", "jmvfw.dll", "jmvh263.dll", "jogl_awt.dll",
"jogl_cg.dll", "jogl.dll", "jsound.dll" };
for (String string : libs) {
InputStream is = getClass().getResourceAsStream(
"/resources/native/" + string);
IFile f = conNativeDir.getFile(string);
f.create(is, true, null);
}

IClasspathContainer libContainer = new IClasspathContainer() {
public IClasspathEntry[] getClasspathEntries() {
System.out.println("get!");
List ices = new ArrayList();
try {
IResource[] rs = libDir.members();
for (IResource r : rs) {
if (r instanceof IFile) {
IFile f = (IFile) r;
System.out.println("\t" + f);
IClasspathEntry entry = JavaCore.newLibraryEntry(f
.getFullPath(), null, null, false);
ices.add(entry);
}
}
} catch (CoreException e) {
e.printStackTrace();
}
return ices.toArray(new IClasspathEntry[ices.size()]);
}

public String getDescription() {
return "Application library container";
}

public int getKind() {
return IClasspathContainer.K_APPLICATION;
}

public IPath getPath() {
return libPath;
}
};

JavaCore.setClasspathContainer(libPath,
new IJavaProject[] { javaProject }, // value for 'myProject'
new IClasspathContainer[] { libContainer }, null);
// no source, no source, not exported

IClasspathEntry libEntry = JavaCore.newContainerEntry(libPath, null,
new IClasspathAttribute[] { JavaCore.newClasspathAttribute(
JavaRuntime.CLASSPATH_ATTR_LIBRARY_PATH_ENTRY,
conNativePath.toString().substring(1)) }, false);
entries.add(libEntry);

// デフォルトJREを追加
entries.add(JavaRuntime.getDefaultJREContainerEntry());
javaProject.setRawClasspath(entries.toArray(new IClasspathEntry[entries
.size()]), monitor);

monitor.worked(1);
}

0 件のコメント: