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

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

Introduction

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

Prototype

IPath getOutputLocation() throws JavaModelException;

Source Link

Document

Returns the default output location for this project as a workspace- relative absolute path.

Usage

From source file:fr.obeo.acceleo.tools.resources.Resources.java

License:Open Source License

/**
 * Gets the default output of the project.
 * //  w  w w  .j a v a2 s  . c o m
 * @param project
 *            is the project
 * @return the default output of the project, or null if it doesn't exist
 */
public static IFolder getOutputFolder(IProject project) {
    final IJavaProject projet = JavaCore.create(project);
    try {
        IPath output = projet.getOutputLocation();
        if (output != null && output.segmentCount() > 1) {
            IFolder folder = project.getWorkspace().getRoot().getFolder(output);
            if (folder.exists()) {
                return folder;
            } else {
                return null;
            }
        } else {
            return null;
        }
    } catch (JavaModelException e) {
        return null;
    }
}

From source file:fr.obeo.ariadne.ide.connector.java.internal.explorer.JavaExplorer.java

License:Open Source License

/**
 * Launches the exploration of the given Java project.
 * // w w w .j  av a  2s .c  o  m
 * @param project
 *            The Java project to explore
 * @param monitor
 *            The progress monitor
 * @return The Component representing the Java project.
 */
public Component doExplore(IProject project, IProgressMonitor monitor) {
    Component ariadneComponent = this.getOrCreateComponent(project);
    IJavaProject iJavaProject = JavaCore.create(project);
    try {
        IClasspathEntry[] rawClasspath = iJavaProject.getRawClasspath();
        for (IClasspathEntry iClasspathEntry : rawClasspath) {
            // We have the source folders of the project.
            IPath inputFolderPath = iClasspathEntry.getPath();
            IPath outputFolderPath = iClasspathEntry.getOutputLocation();

            if (outputFolderPath == null) {
                outputFolderPath = iJavaProject.getOutputLocation();
            }

            // Create the classpath entry
            ClasspathEntry classpathEntry = CodeFactory.eINSTANCE.createClasspathEntry();
            classpathEntry.setInputFolder(inputFolderPath.toString());
            classpathEntry.setOutputFolder(outputFolderPath.toString());
            ariadneComponent.getClasspathEntries().add(classpathEntry);

            int entryKind = iClasspathEntry.getEntryKind();
            if (IClasspathEntry.CPE_SOURCE == entryKind) {
                // Explore its content located in its input folder
                this.exploreClasspathEntry(iJavaProject, iClasspathEntry, classpathEntry, monitor);
            }
        }
    } catch (JavaModelException e) {
        e.printStackTrace();
    }

    return ariadneComponent;
}

From source file:hornetq_project_gen.importWizards.hornetq.ImportWizard.java

License:Open Source License

public boolean performFinish() {
    MavenModelHelper helper = mainPage.getHelper();

    if (helper == null)
        return false;

    IProgressMonitor progressMonitor = new GenericProgressMonitor();
    IWorkspaceRoot rootWs = ResourcesPlugin.getWorkspace().getRoot();
    try {// w ww.ja va2  s .  c  om
        IProject project = rootWs.getProject(mainPage.getProjectName());
        project.create(progressMonitor);
        project.open(progressMonitor);
        //add nature
        IProjectDescription description = project.getDescription();
        description.setNatureIds(new String[] { JavaCore.NATURE_ID });
        project.setDescription(description, progressMonitor);

        IJavaProject javaProject = JavaCore.create(project);
        //setup bin output folder
        if (javaProject.getOutputLocation() == null) {
            IFolder binFolder = project.getFolder("bin");
            binFolder.create(false, true, null);
            javaProject.setOutputLocation(binFolder.getFullPath(), null);
        }

        //all source files
        Map<String, IClasspathEntry> sources = new HashMap<String, IClasspathEntry>();
        helper.getProjectSources(project, sources);

        //all external jars
        helper.getDependencies(sources);

        //jre lib
        IClasspathEntry[] jreEntries = PreferenceConstants.getDefaultJRELibrary();
        for (int i = 0; i < jreEntries.length; i++) {
            sources.put(jreEntries[i].getPath().toOSString(), jreEntries[i]);
        }

        //copy missing entries
        helper.findMissingEntries(project, sources);

        //copy resources
        helper.copyResources(project, sources);

        //set project classpath
        IClasspathEntry[] allEntries = sources.values().toArray(new IClasspathEntry[0]);
        javaProject.setRawClasspath(allEntries, progressMonitor);

        //add a builder
        final String BUILDER_ID = "hornetq_project_gen.hqbuilder";
        IProjectDescription desc = project.getDescription();
        ICommand[] commands = desc.getBuildSpec();
        boolean found = false;

        for (int i = 0; i < commands.length; ++i) {
            if (commands[i].getBuilderName().equals(BUILDER_ID)) {
                found = true;
                break;
            }
        }
        if (!found) {
            //add builder to project
            ICommand command = desc.newCommand();
            command.setBuilderName(BUILDER_ID);
            ICommand[] newCommands = new ICommand[commands.length + 1];

            // Add it before other builders.
            System.arraycopy(commands, 0, newCommands, 1, commands.length);
            newCommands[0] = command;
            desc.setBuildSpec(newCommands);
            project.setDescription(desc, null);
        }

    } catch (CoreException e) {
        e.printStackTrace();
        return false;
    }

    return true;
}

From source file:hornetq_project_gen.importWizards.wildfly.WildFlyImportWizard.java

License:Open Source License

public boolean performFinish() {
    MavenModelHelper helper = mainPage.getHelper();

    if (helper == null)
        return false;

    IProgressMonitor progressMonitor = new GenericProgressMonitor();
    IWorkspaceRoot rootWs = ResourcesPlugin.getWorkspace().getRoot();
    try {/*from ww w . j  a  v  a2 s  .co m*/
        IProject project = rootWs.getProject(mainPage.getProjectName());
        project.create(progressMonitor);
        project.open(progressMonitor);
        //add nature
        IProjectDescription description = project.getDescription();
        description.setNatureIds(new String[] { JavaCore.NATURE_ID });
        project.setDescription(description, progressMonitor);

        IJavaProject javaProject = JavaCore.create(project);
        //setup bin output folder
        if (javaProject.getOutputLocation() == null) {
            IFolder binFolder = project.getFolder("bin");
            binFolder.create(false, true, null);
            javaProject.setOutputLocation(binFolder.getFullPath(), null);
        }

        //all source files
        Map<String, IClasspathEntry> sources = new HashMap<String, IClasspathEntry>();
        helper.getProjectSources(project, sources);

        //all external jars
        helper.getDependencies(sources);

        //jre lib
        IClasspathEntry[] jreEntries = PreferenceConstants.getDefaultJRELibrary();
        for (int i = 0; i < jreEntries.length; i++) {
            sources.put(jreEntries[i].getPath().toOSString(), jreEntries[i]);
        }

        //need jconsole for cli
        String jHome = System.getProperty("java.home");
        File jreHome = new File(jHome);
        File libDir = new File(jreHome.getParentFile(), "lib");
        File jConsoleJar = new File(libDir, "jconsole.jar");

        String key = jConsoleJar.getAbsolutePath();
        if (!sources.containsKey(key)) {
            IPath jarPath = Path.fromOSString(key);
            IClasspathEntry entry = JavaCore.newLibraryEntry(jarPath, null, null, false);
            sources.put(key, entry);
        }

        //copy missing entries
        helper.findMissingEntries(project, sources);

        //copy resources
        helper.copyResources(project, sources);

        //set project classpath
        IClasspathEntry[] allEntries = sources.values().toArray(new IClasspathEntry[0]);
        javaProject.setRawClasspath(allEntries, progressMonitor);

        //add a builder
        final String BUILDER_ID = "hornetq_project_gen.hqbuilder";
        IProjectDescription desc = project.getDescription();
        ICommand[] commands = desc.getBuildSpec();
        boolean found = false;

        for (int i = 0; i < commands.length; ++i) {
            if (commands[i].getBuilderName().equals(BUILDER_ID)) {
                found = true;
                break;
            }
        }
        if (!found) {
            //add builder to project
            ICommand command = desc.newCommand();
            command.setBuilderName(BUILDER_ID);
            ICommand[] newCommands = new ICommand[commands.length + 1];

            // Add it before other builders.
            System.arraycopy(commands, 0, newCommands, 1, commands.length);
            newCommands[0] = command;
            desc.setBuildSpec(newCommands);
            project.setDescription(desc, null);
        }

    } catch (CoreException e) {
        e.printStackTrace();
        return false;
    }

    return true;
}

From source file:info.evanchik.eclipse.felix.FelixLaunchConfiguration.java

License:Open Source License

/**
 * Gets the path to the specified bundle in the following manner:<br>
 * <br>//from   w  w  w .  jav a2  s.  co  m
 * <ol>
 * <li>If the bundle is found in the Plug-in Registry and is not a workspace
 * resource, return the path to the bundle</li>
 * <li>If the bundle is in the Plug-in Registry but is a workspace resource,
 * return the path to the path to the output location that contains the
 * package specified ({@code project/output folder})</li>
 * <li>If the bundle is not found in the Plug-in Registry then look for it
 * in the OSGi platform</li>
 * </ol>
 *
 * @param bundleName
 *            the symbolic name of the bundle
 * @param packageName
 *            the name of the package used to locate the output folder
 * @return a fully qualified path to the requested bundle or null if it does
 *         not exist
 * @throws CoreException
 */
private static String getBundlePath(String bundleName, String packageName) throws CoreException {
    final IPluginModelBase model = PluginRegistry.findModel(bundleName);
    if (model != null) {
        final IResource resource = model.getUnderlyingResource();

        if (!isWorkspaceModel(model)) {
            return model.getInstallLocation();
        }

        final IProject project = resource.getProject();
        if (project.hasNature(JavaCore.NATURE_ID)) {
            final IJavaProject jProject = JavaCore.create(project);
            final IClasspathEntry[] entries = jProject.getRawClasspath();

            for (int i = 0; i < entries.length; i++) {
                final int kind = entries[i].getEntryKind();
                if (kind == IClasspathEntry.CPE_SOURCE || kind == IClasspathEntry.CPE_LIBRARY) {
                    final IPackageFragmentRoot[] roots = jProject.findPackageFragmentRoots(entries[i]);

                    for (int j = 0; j < roots.length; j++) {
                        if (roots[j].getPackageFragment(packageName).exists()) {
                            // if source folder, find the output folder
                            if (kind == IClasspathEntry.CPE_SOURCE) {
                                IPath path = entries[i].getOutputLocation();
                                if (path == null) {
                                    path = jProject.getOutputLocation();
                                }

                                path = path.removeFirstSegments(1);

                                return project.getLocation().append(path).toOSString();
                            }
                            // else if is a library jar, then get the
                            // location of the jar itself
                            final IResource jar = roots[j].getResource();
                            if (jar != null) {
                                return jar.getLocation().toOSString();
                            }
                        }
                    }
                }
            }
        }
    }

    final Bundle bundle = Platform.getBundle(bundleName);
    if (bundle != null) {
        try {
            URL url = FileLocator.resolve(bundle.getEntry("/")); //$NON-NLS-1$
            url = FileLocator.toFileURL(url);
            String path = url.getFile();
            if (path.startsWith("file:")) { //$NON-NLS-1$
                path = path.substring(5);
            }

            path = new File(path).getAbsolutePath();

            if (path.endsWith("!")) { //$NON-NLS-1$
                path = path.substring(0, path.length() - 1);
            }

            return path;
        } catch (IOException e) {
        }
    }

    return null;
}

From source file:io.sarl.tests.api.WorkbenchTestHelper.java

License:Apache License

private static IPath computeBinaryPathForBundle(IPath bundlePath, IPath workspaceRoot) {
    // Determine the path from the output folders of the Java projects in the current workspace.
    IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(bundlePath.lastSegment());
    IPath newBundlePath = null;/*from w w w .j  a  v  a  2s  .  co  m*/
    try {
        if (project != null && project.hasNature(JavaCore.NATURE_ID)) {
            IJavaProject javaProject = JavaCore.create(project);
            newBundlePath = javaProject.getOutputLocation();
            if (newBundlePath != null) {
                newBundlePath = workspaceRoot.append(newBundlePath);
                // Test if the bundle path exists
                if (newBundlePath != null && !newBundlePath.toFile().exists()) {
                    newBundlePath = null;
                }
            }
        }
    } catch (Exception e) {
        // Ignore the exceptions since they are not useful (hopefully)
    }

    if (newBundlePath != null) {
        assert (newBundlePath.isAbsolute()) : "The bundle path is not absolute: " + newBundlePath; //$NON-NLS-1$
        return newBundlePath;
    }

    // Detect the binary folder in the bundle.
    //
    // TODO: Replace by a dynamic detection based on Jdt API.
    File localFile = bundlePath.toFile();
    File binFolder = new File(new File(localFile, "target"), "classes"); //$NON-NLS-1$//$NON-NLS-2$
    if (binFolder.exists()) {
        newBundlePath = bundlePath.append("target").append("classes"); //$NON-NLS-1$//$NON-NLS-2$
    } else {
        binFolder = new File(localFile, "bin"); //$NON-NLS-1$
        if (binFolder.exists()) {
            newBundlePath = bundlePath.append("bin"); //$NON-NLS-1$
        } else {
            newBundlePath = bundlePath;
        }
    }

    assert (newBundlePath.isAbsolute()) : "The bundle path is not absolute: " + bundlePath; //$NON-NLS-1$
    return newBundlePath;
}

From source file:jasima_gui.EclipseProjectClassLoader.java

License:Open Source License

protected Resource findResource(final String name, IJavaProject proj, boolean onlyExported)
        throws CoreException {
    IClasspathEntry[] classpath = proj.getResolvedClasspath(true);

    byte[] content;

    content = readResource(proj.getOutputLocation().makeRelative(), name);
    if (content != null) {
        return new Resource(proj.getOutputLocation().makeRelative(), content);
    }//from w w w . j av  a  2s. co  m

    for (IClasspathEntry entry : classpath) {
        if (onlyExported && !entry.isExported())
            continue;

        if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
            content = readResource(entry.getPath(), name);
            if (content != null) {
                return new Resource(entry.getPath(), content);
            }
        } else if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
            IProject projEntry = (IProject) ResourcesPlugin.getWorkspace().getRoot()
                    .findMember(entry.getPath());
            Resource result = findResource(name, JavaCore.create(projEntry), true);
            if (result != null) {
                return result;
            }
        }
    }
    return null;
}

From source file:me.gladwell.eclipse.m2e.android.test.AndroidMavenPluginTest.java

License:Open Source License

public void testConfigureNonAndroidProject() throws Exception {
    deleteProject(SIMPLE_PROJECT_NAME);/*from  w  w w.j  a va2  s . c  om*/
    IProject project = importAndroidProject(SIMPLE_PROJECT_NAME);

    assertFalse("configurer added android nature", project.hasNature(AdtConstants.NATURE_DEFAULT));
    IJavaProject javaProject = JavaCore.create(project);
    assertFalse("output location set to android value for non-android project", javaProject.getOutputLocation()
            .toString().equals("/" + SIMPLE_PROJECT_NAME + "/target/android-classes"));

    for (IClasspathEntry entry : javaProject.getRawClasspath()) {
        assertFalse("classpath contains reference to gen directory",
                entry.getPath().toOSString().contains("gen"));
    }
}

From source file:mt.com.southedge.jclockwork.plugin.classloader.JClockWorkClassLoader.java

License:Open Source License

/**
 * Creates a class loader including the classes found in the java project. Also adds the binary location to the
 * created class loader./* www  . j a va2 s. c  o m*/
 * 
 * @param jp the java project
 * @return a class loader.
 * @throws CoreException core exception.
 */
public ClassLoader createClassLoader(IJavaProject jp) throws CoreException {

    IClasspathEntry[] javacp = jp.getResolvedClasspath(false);
    URL[] url = new URL[javacp.length + 1]; // Need also to add binary location

    for (int i = 0; i < javacp.length; i++) {
        try {
            IPath path = javacp[i].getPath();
            IResource resource = ResourcesPlugin.getWorkspace().getRoot().findMember(path);

            if (resource != null) {
                path = resource.getLocation();
            }

            url[i] = path.toFile().getAbsoluteFile().toURL();
        } catch (MalformedURLException e) {
            continue;
        }
    }

    IPath binPath = jp.getOutputLocation();
    binPath = ResourcesPlugin.getWorkspace().getRoot().findMember(binPath).getLocation();

    try {
        url[url.length - 1] = binPath.toFile().toURL();
    } catch (MalformedURLException e) {
        // Should not happen.
        e.printStackTrace();
    }
    return new URLClassLoader(url);
}

From source file:net.kbserve.pyjdt.properties.models.RootContainer.java

License:Open Source License

@Override
public String getRealPath(IProject project) {
    // TODO: What about the source path?
    IJavaProject javaProject = JavaCore.create(project);
    try {/*from  w w w. ja  v  a  2 s. c o m*/
        return makeStringPath(prependWorkspaceLoc(javaProject.getOutputLocation()).makeAbsolute());
    } catch (JavaModelException e) {
        throw new RuntimeException(e);
    }
}