Example usage for org.eclipse.jdt.core IJavaProject setOutputLocation

List of usage examples for org.eclipse.jdt.core IJavaProject setOutputLocation

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IJavaProject setOutputLocation.

Prototype

void setOutputLocation(IPath path, IProgressMonitor monitor) throws JavaModelException;

Source Link

Document

Sets the default output location of this project to the location described by the given workspace-relative absolute path.

Usage

From source file:com.motorola.studio.android.model.ProjectCreationSupport.java

License:Apache License

/**
 * Create android project.//w  ww. ja v a 2 s .  c  om
 * @param project
 * @param androidProject
 * @param description
 * @param monitor
 * @param parameters
 * @param stringDictionary
 * @throws InvocationTargetException
 */
protected static void createProjectAsync(IProject project, AndroidProject androidProject,
        IProjectDescription description, IProgressMonitor monitor, Map<String, Object> parameters,
        Map<String, String> stringDictionary) throws InvocationTargetException {
    monitor.beginTask(AndroidNLS.UI_ProjectCreationSupport_CopyingSamplesMonitorTaskTitle, 1000);
    try {
        // Create project and open it
        project.create(description, new SubProgressMonitor(monitor, 100));
        if (monitor.isCanceled()) {
            undoProjectCreation(project);
            throw new OperationCanceledException();
        }
        project.open(IResource.BACKGROUND_REFRESH, new SubProgressMonitor(monitor, 100));

        ProjectUtils.setupAndroidNatures(project, monitor);

        // Create folders in the project if they don't already exist
        createDefaultDir(project, IAndroidConstants.WS_ROOT, BIN_DIR, new SubProgressMonitor(monitor, 40));
        createDefaultDir(project, IAndroidConstants.WS_ROOT, RES_DIR, new SubProgressMonitor(monitor, 40));
        createDefaultDir(project, IAndroidConstants.WS_ROOT, ASSETS_DIR, new SubProgressMonitor(monitor, 40));
        createDefaultDir(project, IAndroidConstants.WS_ROOT, GEN_DIR, new SubProgressMonitor(monitor, 40));

        switch (androidProject.getSourceType()) {
        case NEW:
            // Create the source folders in the project if they don't already exist
            List<String> sourceFolders = androidProject.getSourceFolders();
            for (String sourceFolder : sourceFolders) {
                createDefaultDir(project, IAndroidConstants.WS_ROOT, sourceFolder,
                        new SubProgressMonitor(monitor, 40));
            }

            // Create the resource folders in the project if they don't already exist.
            int apiLevel = androidProject.getSdkTarget().getVersion().getApiLevel();
            if (apiLevel < 4) {
                createDefaultDir(project, RES_DIR, DRAWABLE_DIR + File.separator,
                        new SubProgressMonitor(monitor, 40));
            } else {
                for (String dpi : DPIS) {
                    createDefaultDir(project, RES_DIR, DRAWABLE_DIR + "-" + dpi + File.separator,
                            new SubProgressMonitor(monitor, 40));
                }
            }
            createDefaultDir(project, RES_DIR, LAYOUT_DIR, new SubProgressMonitor(monitor, 40));
            createDefaultDir(project, RES_DIR, VALUES_DIR, new SubProgressMonitor(monitor, 40));

            // Create files in the project if they don't already exist
            createManifest(project, parameters, stringDictionary, new SubProgressMonitor(monitor, 80));
            // add the default app icon
            addIcon(project, apiLevel, new SubProgressMonitor(monitor, 100));
            // Create the default package components
            String primarySrcFolder = IAndroidConstants.FD_SOURCES;
            if (!sourceFolders.contains(IAndroidConstants.FD_SOURCES)) {
                primarySrcFolder = sourceFolders.get(0);
            }
            addInitialCode(project, primarySrcFolder, parameters, stringDictionary,
                    new SubProgressMonitor(monitor, 200));
            // add the string definition file if needed
            if (stringDictionary.size() > 0) {
                EclipseUtils.createOrUpdateDictionaryFile(project, stringDictionary, null,
                        new SubProgressMonitor(monitor, 100));
            }

            break;
        case EXISTING:
            createDefaultDir(project, IAndroidConstants.WS_ROOT, GEN_DIR, new SubProgressMonitor(monitor, 650));
            break;
        case SAMPLE:
            monitor.setTaskName(AndroidNLS.UI_ProjectCreationSupport_CopyingSamplesMonitorMessage);
            FileUtil.copyDir(androidProject.getSample().getFolder(), project.getLocation().toFile());
            project.refreshLocal(IResource.DEPTH_INFINITE, new SubProgressMonitor(monitor, 650));
            break;
        case WIDGET:
            // Create the source folders in the project if they don't already exist
            List<String> widgetSourceFolders = androidProject.getSourceFolders();
            for (String sourceFolder : widgetSourceFolders) {
                createDefaultDir(project, IAndroidConstants.WS_ROOT, sourceFolder,
                        new SubProgressMonitor(monitor, 40));
            }

            // Create the resource folders in the project if they don't already exist.
            int widgetApiLevel = androidProject.getSdkTarget().getVersion().getApiLevel();
            if (widgetApiLevel < 4) {
                createDefaultDir(project, RES_DIR, DRAWABLE_DIR + File.separator,
                        new SubProgressMonitor(monitor, 40));
            } else {
                for (String dpi : DPIS) {
                    createDefaultDir(project, RES_DIR, DRAWABLE_DIR + "-" + dpi + File.separator,
                            new SubProgressMonitor(monitor, 40));
                }
            }
            createDefaultDir(project, RES_DIR, LAYOUT_DIR, new SubProgressMonitor(monitor, 40));
            createDefaultDir(project, RES_DIR, VALUES_DIR, new SubProgressMonitor(monitor, 40));
            createDefaultDir(project, RES_DIR, XML_DIR, new SubProgressMonitor(monitor, 40));

            // Create files in the project if they don't already exist
            createWidgetManifest(project, parameters, stringDictionary, new SubProgressMonitor(monitor, 80));
            // add the default app icon
            addIcon(project, widgetApiLevel, new SubProgressMonitor(monitor, 100));
            // Create the default package components
            String widgetPrimarySrcFolder = IAndroidConstants.FD_SOURCES;
            if (!widgetSourceFolders.contains(IAndroidConstants.FD_SOURCES)) {
                primarySrcFolder = widgetSourceFolders.get(0);
            }
            addInitialWidgetCode(project, widgetPrimarySrcFolder, parameters, stringDictionary,
                    new SubProgressMonitor(monitor, 200));
            // add the string definition file if needed
            if (stringDictionary.size() > 0) {
                EclipseUtils.createOrUpdateDictionaryFile(project, stringDictionary, null,
                        new SubProgressMonitor(monitor, 100));
            }

            break;
        }

        // Setup class path
        IJavaProject javaProject = JavaCore.create(project);
        setupSourceFolders(javaProject, androidProject.getSourceFolders(), new SubProgressMonitor(monitor, 40));

        // Set output location
        javaProject.setOutputLocation(project.getFolder(BIN_DIR).getFullPath(),
                new SubProgressMonitor(monitor, 40));
        SdkUtils.associate(project, androidProject.getSdkTarget());
        ProjectUtils.fixProject(project);
    } catch (CoreException e) {
        undoProjectCreation(project);
        throw new InvocationTargetException(e);
    } catch (IOException e) {
        undoProjectCreation(project);
        throw new InvocationTargetException(e);
    } finally {
        monitor.done();
    }
}

From source file:com.mtcflow.project.util.MTCProjectSupport.java

License:Open Source License

/**
 * For this marvelous project we need to: - create the default Eclipse
 * project - add the custom project nature - create the folder structure
 * //from  w ww . j  ava  2  s.c  o  m
 * @param projectName
 * @param location
 * @param natureId
 * @return
 */
public static IProject createProject(String projectName, URI location) {
    Assert.isNotNull(projectName);
    Assert.isTrue(projectName.trim().length() > 0);

    try {
        // create eclipse project
        IProject project = createBaseProject(projectName, location);
        project.setDefaultCharset("UTF-8", null);
        addNature(project);
        // create java project
        IJavaProject javaProject = JavaCore.create(project);
        javaProject.setOption(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_5);
        javaProject.setOption(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_5);
        javaProject.setOption(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_5);

        // add bin/ouput folder
        IFolder binFolder = project.getFolder("bin");
        // binFolder.create(false, true, null);
        javaProject.setOutputLocation(binFolder.getFullPath(), null);

        // add libs to project class path
        List<IClasspathEntry> entries = new ArrayList<IClasspathEntry>();
        IVMInstall vmInstall = JavaRuntime.getDefaultVMInstall();

        // create source folder
        IFolder sourceFolder = project.getFolder("src");
        sourceFolder.create(false, true, null);
        IPackageFragmentRoot srcRoot = javaProject.getPackageFragmentRoot(sourceFolder);
        IClasspathEntry[] cEntries = new IClasspathEntry[3];
        cEntries[0] = JavaRuntime.getDefaultJREContainerEntry();
        // cEntries[2] = JavaCore.new
        cEntries[1] = JavaCore.newSourceEntry(srcRoot.getPath());
        cEntries[2] = JavaCore.newContainerEntry(new Path("org.eclipse.pde.core.requiredPlugins"));
        javaProject.setRawClasspath(cEntries, null);
        /*
         * Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name:
         * LePlugine Bundle-SymbolicName: LePlugine Bundle-Version:
         * 1.0.0.qualifier Bundle-RequiredExecutionEnvironment: JavaSE-1.7
         */
        String[] paths = { "transformations/T2M", //$NON-NLS-1$
                "transformations/M2M", //$NON-NLS-1$
                "transformations/M2T", //$NON-NLS-1$
                "transformations/HOT", //$NON-NLS-1$
                "mtcs", //$NON-NLS-1$ 
                "metamodels", //$NON-NLS-1$
                "libraries", //$NON-NLS-1$
                "scripts", //$NON-NLS-1$
                "validations", //$NON-NLS-1$
                "tests", //$NON-NLS-1$
                "models", "META-INF" }; //$NON-NLS-1$
        addToProjectStructure(javaProject.getProject(), paths);
        createTemplateFileInProjectAt(javaProject.getProject(), "build.properties", "build.properties");
        createTemplateFileInProjectAt(javaProject.getProject(), "default.mtc", "/mtcs/default.mtc");
        createTemplateFileInProjectAt(javaProject.getProject(), "default_diagram.mtcd", "/mtcs/default.mtcd");
        Manifest manifest = new Manifest();
        manifest.getMainAttributes().putValue("Manifest-Version", "1.0");
        manifest.getMainAttributes().putValue("Bundle-ManifestVersion", "2");
        manifest.getMainAttributes().putValue("Bundle-Name", projectName);
        manifest.getMainAttributes().putValue("Bundle-SymbolicName", projectName);
        manifest.getMainAttributes().putValue("Bundle-Version", "1.0.0");
        manifest.getMainAttributes().putValue("Require-Bundle",
                "com.mtcflow.model,com.mtcflow.engine,com.mtcflow.engine.core");
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        manifest.write(out);
        javaProject.getProject().getFile("/META-INF/MANIFEST.MF")
                .create(new ByteArrayInputStream(out.toByteArray()), true, null);
        return javaProject.getProject();
    } catch (Exception ex) {
        ex.printStackTrace();
        throw new RuntimeException("Error creating the project", ex);
    }
}

From source file:com.mtcflow.project.util.MTCProjectSupport.java

License:Open Source License

private IJavaProject createProject(String projName) throws Exception {
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();

    if (projName == null || projName.trim().length() == 0)
        return null;

    // create eclipse project
    IProject project = root.getProject(projName);
    if (project.exists())
        project.delete(true, null);/*  w  w  w.  j  av  a 2 s.  c o m*/

    project.create(null);
    project.open(null);

    addNature(project);

    // create java project
    IJavaProject javaProject = JavaCore.create(project);

    // add bin/ouput folder
    IFolder binFolder = project.getFolder("bin");
    binFolder.create(false, true, null);
    javaProject.setOutputLocation(binFolder.getFullPath(), null);

    // add libs to project class path
    List<IClasspathEntry> entries = new ArrayList<IClasspathEntry>();
    IVMInstall vmInstall = JavaRuntime.getDefaultVMInstall();
    LibraryLocation[] locations = JavaRuntime.getLibraryLocations(vmInstall);
    for (LibraryLocation element : locations) {
        entries.add(JavaCore.newLibraryEntry(element.getSystemLibraryPath(), null, null));
    }

    javaProject.setRawClasspath(entries.toArray(new IClasspathEntry[entries.size()]), null);

    // create source folder
    IFolder sourceFolder = project.getFolder("src");
    sourceFolder.create(false, true, null);

    IPackageFragmentRoot srcRoot = javaProject.getPackageFragmentRoot(sourceFolder);
    IClasspathEntry[] oldEntries = javaProject.getRawClasspath();
    IClasspathEntry[] newEntries = new IClasspathEntry[oldEntries.length + 1];
    System.arraycopy(oldEntries, 0, newEntries, 0, oldEntries.length);
    newEntries[oldEntries.length] = JavaCore.newSourceEntry(srcRoot.getPath());
    javaProject.setRawClasspath(newEntries, null);

    return javaProject;
}

From source file:com.nginious.http.plugin.NewProjectWizard.java

License:Apache License

public boolean performFinish() {
    String name = pageOne.getProjectName();
    IProject newProject = ResourcesPlugin.getWorkspace().getRoot().getProject(name);

    if (newProject.exists()) {
        throw new RuntimeException("Project exists!");
    }//w ww .ja  v a  2  s  . c  om

    try {
        int listenPort = pageOne.getListenPort();

        if (!pageOne.validate(listenPort)) {
            return false;
        }

        IProgressMonitor progressMonitor = new NullProgressMonitor();

        // Create project
        newProject.create(progressMonitor);
        newProject.open(progressMonitor);

        // Create folder structure
        String[] paths = { "src", "WebContent", "WebContent/WEB-INF", "WebContent/WEB-INF/classes",
                "WebContent/WEB-INF/lib", "WebContent/WEB-INF/xsp" };
        addToProjectStructure(newProject, paths);

        ClassPathBuilder builder = new ClassPathBuilder(newProject, progressMonitor);
        builder.build(progressMonitor);

        // Set project nature
        IProjectDescription description = newProject.getDescription();
        description.setNatureIds(new String[] { JavaCore.NATURE_ID, NginiousPlugin.NATURE_ID });
        newProject.setDescription(description, null);

        // Create java project
        IJavaProject javaProject = JavaCore.create(newProject);

        // Set classes output folder
        IFolder classesFolder = newProject.getFolder("WebContent/WEB-INF/classes");
        javaProject.setOutputLocation(classesFolder.getFullPath(), null);

        // Set classpath
        IClasspathEntry[] entries = builder.getClassPath();
        javaProject.setRawClasspath(entries, progressMonitor);

        // Set source folder
        IFolder sourceFolder = newProject.getFolder("src");
        IPackageFragmentRoot root = javaProject.getPackageFragmentRoot(sourceFolder);
        IClasspathEntry[] oldEntries = javaProject.getRawClasspath();
        IClasspathEntry[] newEntries = new IClasspathEntry[oldEntries.length + 1];
        System.arraycopy(oldEntries, 0, newEntries, 0, oldEntries.length);
        newEntries[oldEntries.length] = JavaCore.newSourceEntry(root.getPath());
        javaProject.setRawClasspath(newEntries, null);
        BasicNewProjectResourceWizard.updatePerspective(this.configurationElement);

        newProject.setPersistentProperty(NginiousPlugin.LISTEN_PORT_PROP_KEY, Integer.toString(listenPort));
        newProject.setPersistentProperty(NginiousPlugin.PUBLISH_URL_PROP_KEY, pageOne.getPublishUrl());
        newProject.setPersistentProperty(NginiousPlugin.PUBLISH_USERNAME_PROP_KEY,
                pageOne.getPublishUsername());
        newProject.setPersistentProperty(NginiousPlugin.PUBLISH_PASSWORD_PROP_KEY,
                pageOne.getPublishPassword());
        newProject.setPersistentProperty(NginiousPlugin.MIN_MEMORY_PROP_KEY,
                Integer.toString(pageOne.getMinMemory()));
        newProject.setPersistentProperty(NginiousPlugin.MAX_MEMORY_PROP_KEY,
                Integer.toString(pageOne.getMaxMemory()));
    } catch (Exception e) {
        throw new RuntimeException(e.getMessage(), e);
    }

    return true;
}

From source file:com.redhat.ceylon.eclipse.ui.test.CeylonEditorTest.java

License:Open Source License

private IProject createJavaProject(String name) throws CoreException, JavaModelException {
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    IProject project = root.getProject(name);
    project.create(null);//from w ww .jav  a2 s  . c o m
    project.open(null);

    IProjectDescription description = project.getDescription();
    description.setNatureIds(new String[] { JavaCore.NATURE_ID });
    project.setDescription(description, null);

    IJavaProject javaProject = JavaCore.create(project);

    IFolder binFolder = project.getFolder("bin");
    binFolder.create(false, true, null);
    javaProject.setOutputLocation(binFolder.getFullPath(), null);

    List<IClasspathEntry> entries = new ArrayList<IClasspathEntry>();
    IVMInstall vmInstall = JavaRuntime.getDefaultVMInstall();
    LibraryLocation[] locations = JavaRuntime.getLibraryLocations(vmInstall);
    for (LibraryLocation element : locations) {
        entries.add(JavaCore.newLibraryEntry(element.getSystemLibraryPath(), null, null));
    }
    //add libs to project class path
    javaProject.setRawClasspath(entries.toArray(new IClasspathEntry[entries.size()]), null);

    IFolder sourceFolder = project.getFolder("src");
    sourceFolder.create(false, true, null);

    IPackageFragmentRoot packageroot = javaProject.getPackageFragmentRoot(sourceFolder);
    IClasspathEntry[] oldEntries = javaProject.getRawClasspath();
    IClasspathEntry[] newEntries = new IClasspathEntry[oldEntries.length + 1];
    System.arraycopy(oldEntries, 0, newEntries, 0, oldEntries.length);
    newEntries[oldEntries.length] = JavaCore.newSourceEntry(packageroot.getPath());
    javaProject.setRawClasspath(newEntries, null);
    return project;
}

From source file:com.siteview.mde.internal.core.project.ProjectModifyOperation.java

License:Open Source License

/**
 * Configures the build path and output location of the described Java project.
 * If the Java project existed before this operation, new build path entries are
 * added for the bundle class path, if required, but we don't change the exiting
 * build path./*w w  w.  j  av  a2 s. co m*/
 * 
 * @param description desired project description
 * @param before state before the operation
 * @param existed whether the Java project existed before the operation
 */
private void configureJavaProject(IBundleProjectDescription description, IBundleProjectDescription before,
        boolean existed) throws CoreException {
    IProject project = description.getProject();
    IJavaProject javaProject = JavaCore.create(project);
    // create source folders as required
    IBundleClasspathEntry[] bces = description.getBundleClasspath();
    if (bces != null && bces.length > 0) {
        for (int i = 0; i < bces.length; i++) {
            IPath folder = bces[i].getSourcePath();
            if (folder != null) {
                CoreUtility.createFolder(project.getFolder(folder));
            }
        }
    }
    // Set default output folder
    if (description.getDefaultOutputFolder() != null) {
        IPath path = project.getFullPath().append(description.getDefaultOutputFolder());
        javaProject.setOutputLocation(path, null);
    }

    // merge the class path if the project existed before
    IBundleClasspathEntry[] prev = before.getBundleClasspath();
    if (!isEqual(bces, prev)) {
        if (existed) {
            // add entries not already present
            IClasspathEntry[] entries = getSourceFolderEntries(javaProject, description);
            IClasspathEntry[] rawClasspath = javaProject.getRawClasspath();
            List add = new ArrayList();
            for (int i = 0; i < entries.length; i++) {
                IClasspathEntry entry = entries[i];
                boolean present = false;
                for (int j = 0; j < rawClasspath.length; j++) {
                    IClasspathEntry existingEntry = rawClasspath[j];
                    if (existingEntry.getEntryKind() == entry.getEntryKind()) {
                        if (existingEntry.getPath().equals(entry.getPath())) {
                            present = true;
                            break;
                        }
                    }
                }
                if (!present) {
                    add.add(entry);
                }
            }
            // check if the 'required plug-ins' container is present
            boolean addRequired = false;
            if (description.hasNature(IBundleProjectDescription.PLUGIN_NATURE)) {
                addRequired = true;
                for (int i = 0; i < rawClasspath.length; i++) {
                    IClasspathEntry cpe = rawClasspath[i];
                    if (cpe.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                        if (MDECore.REQUIRED_PLUGINS_CONTAINER_PATH.equals(cpe.getPath())) {
                            addRequired = false;
                            break;
                        }
                    }
                }
            }
            if (addRequired) {
                add.add(ClasspathComputer.createContainerEntry());
            }
            if (!add.isEmpty()) {
                List all = new ArrayList();
                for (int i = 0; i < rawClasspath.length; i++) {
                    all.add(rawClasspath[i]);
                }
                all.addAll(add);
                javaProject.setRawClasspath((IClasspathEntry[]) all.toArray(new IClasspathEntry[all.size()]),
                        null);
            }
        } else {
            IClasspathEntry[] entries = getClassPathEntries(javaProject, description);
            javaProject.setRawClasspath(entries, null);
        }
    }
}

From source file:com.siteview.mde.internal.core.SearchablePluginsManager.java

License:Open Source License

public IProject createProxyProject(IProgressMonitor monitor) throws CoreException {
    IWorkspaceRoot root = MDECore.getWorkspace().getRoot();
    IProject project = root.getProject(SearchablePluginsManager.PROXY_PROJECT_NAME);
    if (project.exists()) {
        if (!project.isOpen()) {
            project.open(monitor);/*from  www  . j a  v a2s .  c o m*/
        }
        return project;
    }

    monitor.beginTask(NLS.bind(MDECoreMessages.SearchablePluginsManager_createProjectTaskName,
            SearchablePluginsManager.PROXY_PROJECT_NAME), 5);
    project.create(new SubProgressMonitor(monitor, 1));
    project.open(new SubProgressMonitor(monitor, 1));
    CoreUtility.addNatureToProject(project, JavaCore.NATURE_ID, new SubProgressMonitor(monitor, 1));
    IJavaProject jProject = JavaCore.create(project);
    jProject.setOutputLocation(project.getFullPath(), new SubProgressMonitor(monitor, 1));
    computeClasspath(jProject, new SubProgressMonitor(monitor, 1));
    return project;
}

From source file:com.siteview.mde.internal.ui.wizards.feature.AbstractCreateFeatureOperation.java

License:Open Source License

private void createProject(IProgressMonitor monitor) throws CoreException {
    CoreUtility.createProject(fProject, fLocation, monitor);
    fProject.open(monitor);/*from   w  ww. ja  v a2s.  c  om*/
    IProjectDescription desc = fProject.getWorkspace().newProjectDescription(fProject.getName());
    desc.setLocation(fLocation);
    if (!fProject.hasNature(MDE.FEATURE_NATURE))
        CoreUtility.addNatureToProject(fProject, MDE.FEATURE_NATURE, monitor);

    if (fFeatureData.hasCustomHandler()) {
        if (!fProject.hasNature(JavaCore.NATURE_ID))
            CoreUtility.addNatureToProject(fProject, JavaCore.NATURE_ID, monitor);

        if (fFeatureData.getSourceFolderName() != null
                && fFeatureData.getSourceFolderName().trim().length() > 0) {
            IFolder folder = fProject.getFolder(fFeatureData.getSourceFolderName());
            if (!folder.exists())
                CoreUtility.createFolder(folder);
        }

        IJavaProject jproject = JavaCore.create(fProject);
        jproject.setOutputLocation(fProject.getFullPath().append(fFeatureData.getJavaBuildFolderName()),
                monitor);
        jproject.setRawClasspath(new IClasspathEntry[] {
                JavaCore.newSourceEntry(fProject.getFullPath().append(fFeatureData.getSourceFolderName())),
                JavaCore.newContainerEntry(new Path(JavaRuntime.JRE_CONTAINER)) }, monitor);
    }
}

From source file:de.ovgu.featureide.core.framework.FrameworkProjectCreator.java

License:Open Source License

/**
 * Creates a new subproject inside a folder
 * /* w  ww . ja  va  2s.c o m*/
 * @param name - project name
 * @param destination - folder which contains the subproject
 * @throws CoreException
 */
public static void createSubprojectFolder(String name, IFolder destination) throws CoreException {
    final IProjectDescription description = ResourcesPlugin.getWorkspace().newProjectDescription(name);
    description.setLocation(destination.getLocation());

    final IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(name);
    if (project.exists()) {
        return;
    }
    project.create(description, null);
    project.open(null);

    description.setNatureIds(new String[] { JavaCore.NATURE_ID });
    project.setDescription(description, null);

    final IJavaProject javaProject = JavaCore.create(project);
    javaProject.open(null);

    final IFolder binFolder = project.getFolder("bin");
    binFolder.create(true, true, null);
    javaProject.setOutputLocation(binFolder.getFullPath(), null);

    final List<IClasspathEntry> entries = new ArrayList<IClasspathEntry>();
    final IVMInstall vmInstall = JavaRuntime.getDefaultVMInstall();
    final LibraryLocation[] locations = JavaRuntime.getLibraryLocations(vmInstall);
    for (final LibraryLocation element : locations) {
        entries.add(JavaCore.newLibraryEntry(element.getSystemLibraryPath(), null, null));
    }
    javaProject.setRawClasspath(entries.toArray(new IClasspathEntry[entries.size()]), null);
    final IFolder sourceFolder = project.getFolder("src");
    sourceFolder.create(false, true, null);

    final IPackageFragmentRoot srcRoot = javaProject.getPackageFragmentRoot(sourceFolder);
    final IClasspathEntry[] oldEntries = javaProject.getRawClasspath();
    final int oldLength = oldEntries.length;
    final IClasspathEntry[] newEntries = new IClasspathEntry[oldLength + 2];
    System.arraycopy(oldEntries, 0, newEntries, 0, oldEntries.length);
    newEntries[oldLength] = JavaCore.newSourceEntry(srcRoot.getPath());
    newEntries[oldLength + 1] = JavaCore.newProjectEntry(destination.getProject().getFullPath());
    javaProject.setRawClasspath(newEntries, null);

    final IFile infoXML = destination.getFile("info.xml");
    if (!infoXML.exists()) {
        createInfoXML(destination);
    }
}

From source file:de.plugins.eclipse.depclipse.testcommons.TestingEnvironment.java

License:Open Source License

public IPath setExternalOutputFolder(IPath projectPath, String name, IPath externalOutputLocation) {
    IPath result = null;// w w  w . ja va2s .  c o  m
    try {
        checkAssertion("a workspace must be open", this.isOpen); //$NON-NLS-1$
        IProject p = getProject(projectPath);
        IFolder f = p.getFolder(name);
        f.createLink(externalOutputLocation, IResource.ALLOW_MISSING_LOCAL, null);

        result = f.getFullPath();
        IJavaProject javaProject = JavaCore.create(p);
        javaProject.setOutputLocation(result, null);
    } catch (CoreException e) {
        e.printStackTrace();
        checkAssertion("CoreException", false); //$NON-NLS-1$
    }
    return result;
}