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:org.grails.ide.eclipse.commands.test.AbstractCommandTest.java

License:Open Source License

public static void assertDefaultOutputFolder(IJavaProject javaProject) throws JavaModelException {
    assertEquals(//from   w w  w. j  av a2 s  . com
            "/" + javaProject.getProject().getName() + "/" + GrailsCommandUtils.DEFAULT_GRAILS_OUTPUT_FOLDER,
            javaProject.getOutputLocation().toString());
}

From source file:org.grails.ide.eclipse.commands.test.GrailsCommandUtilTest.java

License:Open Source License

/**
 * This test isn't actually testing stuff from {@link GrailsCommandUtils}, but it is in here because
 * it uses some similar checks that a project is properly setup after doing a "raw" import of project
 * created outside eclipse using grails commands.
 * <p>/*  w ww.  j  a  va 2  s  .  com*/
 * A "raw" import means simply pulling in a project from files from somewhere outside eclipse and doing 
 * nothing more than adding them as a new project to the workspace. As of Grails 1.3.5 this should produce
 * a correctly configured grails project (grails 1.3.5 produces correct .project etc. files). So there
 * should be no need to call 'eclipsify' on such projects.
 */
public void testImportExistingProject() throws Throwable {
    GrailsProjectVersionFixer.globalAskToConvertToGrailsProjectAnswer = true;
    System.out.println(">>>>>>>>>>>>>>>>>>>testImportExistingProject");
    GroovyCompilerVersionCheck.testMode();
    ensureDefaultGrailsVersion(GrailsVersion.V_1_3_5); // GrailsVersion of test project!
    StsTestUtil.setAutoBuilding(true);
    System.out.println("Waiting for autobuild...");
    StsTestUtil.waitForAutoBuild();
    System.out.println("Done autobuild");

    URL bareBonesURL = this.getClass().getClassLoader().getResource("external-project.zip");

    String projectName = "External";
    // Create bare bones project from zip file
    IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();

    IPath unzipLoc = workspaceRoot.getLocation();
    ZipFileUtil.unzip(bareBonesURL, unzipLoc.toFile(), new NullProgressMonitor());

    project = workspaceRoot.getProject(projectName);
    project.create(null);

    if (!project.isOpen()) {
        project.open(null);
    }

    assertTrue(project.isAccessible());
    assertTrue(project.getFolder("grails-app").exists());

    /////////////////////////////////////////////////////////////////////////////////////////////////
    //Check a few things about this test project

    assertTrue(project.hasNature(JavaCore.NATURE_ID)); // Should have Java Nature at this point
    assertTrue(GroovyNature.hasGroovyNature(project)); // Should also have Groovy nature

    final IJavaProject javaProject = JavaCore.create(project);

    //Next we check the classpath related stuff. 
    // The classpath may not be right initially... but should eventually become correct as a background
    // refresh dependency job should get scheduled. 
    // ACondition
    new ACondition() {
        @Override
        public boolean test() throws Exception {
            System.out.println("Checking project config...");
            assertEquals("/" + project.getName() + "/target-eclipse/classes",
                    javaProject.getOutputLocation().toString());

            ///////////////////////////////////
            // Check resolved classpath stuff
            IClasspathEntry[] classPath = javaProject.getResolvedClasspath(false);

            // A whole bunch of libraries should be there, check for just a few of those

            // this one will fail on macs
            //            assertClassPathEntry(IClasspathEntry.CPE_LIBRARY, "jre/lib/rt.jar", classPath);
            assertClassPathEntry(IClasspathEntry.CPE_LIBRARY, "grails-core", classPath);
            assertClassPathEntry(IClasspathEntry.CPE_LIBRARY, "grails-bootstrap", classPath);
            assertClassPathEntry(IClasspathEntry.CPE_LIBRARY, "groovy-all", classPath);
            //assertClassPathEntry(IClasspathEntry.CPE_LIBRARY, "servlet-api", classPath);

            //            System.out.println(">>>Resolved classpath");
            //            for (IClasspathEntry entry : classPath) {
            //               System.out.println(kindString(entry.getEntryKind())+": "+entry.getPath());
            //            }
            //            System.out.println("<<<Resolved classpath");

            ///////////////////////////////////
            // Check raw classpath stuff
            classPath = javaProject.getRawClasspath();
            //            for (IClasspathEntry classpathEntry : classPath) {
            //               System.out.println(kindString(classpathEntry.getEntryKind())+": "+classpathEntry.getPath());
            //            }

            //The usual source folders:
            assertClassPathEntry(IClasspathEntry.CPE_SOURCE, "src/java", classPath);
            assertClassPathEntry(IClasspathEntry.CPE_SOURCE, "src/groovy", classPath);
            assertClassPathEntry(IClasspathEntry.CPE_SOURCE, "grails-app/conf", classPath);
            assertClassPathEntry(IClasspathEntry.CPE_SOURCE, "grails-app/controllers", classPath);
            assertClassPathEntry(IClasspathEntry.CPE_SOURCE, "grails-app/domain", classPath);
            assertClassPathEntry(IClasspathEntry.CPE_SOURCE, "grails-app/services", classPath);
            assertClassPathEntry(IClasspathEntry.CPE_SOURCE, "grails-app/taglib", classPath);
            assertClassPathEntry(IClasspathEntry.CPE_SOURCE, "test/integration", classPath);
            assertClassPathEntry(IClasspathEntry.CPE_SOURCE, "test/unit", classPath);

            //The classpath containers for Java and Grails
            assertClassPathEntry(IClasspathEntry.CPE_CONTAINER, "org.eclipse.jdt.launching.JRE_CONTAINER",
                    classPath);
            assertClassPathEntry(IClasspathEntry.CPE_CONTAINER,
                    "org.grails.ide.eclipse.core.CLASSPATH_CONTAINER", classPath);

            //Installed plugins source folders
            assertDefaultPluginSourceFolders(project);

            assertCharset(project);

            System.out.println("Checking project config => OK!");
            return true;
        }
    }.waitFor(60000);

    System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<testImportExistingProject");
}

From source file:org.grails.ide.eclipse.groovy.debug.core.evaluation.GroovyJDIEvaluator.java

License:Open Source License

private void addClasspathEntries(JDIGroovyClassLoader groovyLoader, IJavaProject currentProject,
        boolean includeAll) throws JavaModelException, CoreException {
    IClasspathEntry[] entries = currentProject.getResolvedClasspath(true);
    IPath workspaceLocation = ResourcesPlugin.getWorkspace().getRoot().getLocation();
    for (int i = 0; i < entries.length; i++) {
        if (!includeAll && !entries[i].isExported()) {
            continue;
        }//from w  w  w . java  2 s  . co m
        switch (entries[i].getEntryKind()) {
        case IClasspathEntry.CPE_LIBRARY:
            try {
                groovyLoader.addURL(entries[i].getPath().toFile().toURL());
            } catch (MalformedURLException e) {
                throw new CoreException(new Status(IStatus.ERROR, GroovyDebugCoreActivator.PLUGIN_ID,
                        e.getLocalizedMessage(), e));
            }
            break;

        case IClasspathEntry.CPE_SOURCE:
            IPath outLocation = entries[i].getOutputLocation();
            if (outLocation != null) {
                // using non-default output location
                try {
                    groovyLoader.addURL(workspaceLocation.append(outLocation).toFile().toURL());
                } catch (MalformedURLException e) {
                    throw new CoreException(new Status(IStatus.ERROR, GroovyDebugCoreActivator.PLUGIN_ID,
                            e.getLocalizedMessage(), e));
                }
            }
            break;

        case IClasspathEntry.CPE_PROJECT:
            IProject otherProject = ResourcesPlugin.getWorkspace().getRoot()
                    .getProject(entries[i].getPath().lastSegment());
            if (otherProject.isAccessible()) {
                IJavaProject otherJavaProject = JavaCore.create(otherProject);
                addClasspathEntries(groovyLoader, otherJavaProject, false);
            }
            break;

        default:
            break;
        }
    }

    // now add default out location
    IPath outLocation = currentProject.getOutputLocation();
    if (outLocation != null) {
        try {
            groovyLoader.addURL(workspaceLocation.append(outLocation).toFile().toURL());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }
}

From source file:org.grails.ide.eclipse.runonserver.GrailsAppModuleDelegate.java

License:Open Source License

private void cacheMembers() throws CoreException {
    if ((cachedMembers == null || cachedWarFile == null)) {

        debug("Recomputing cachedMembers");
        cacheWarFile(); //Ensure the exploded war file is there.

        if (explodedWarFile == null || !explodedWarFile.exists()) {
            debug("For some reason there is no exploded war file... (war command failed?)");
            return;
        }/*from  w w  w  .j  a  v  a  2s .com*/

        cachedMembers = getDirectoryResources(Path.EMPTY, explodedWarFile);

        if (incremental) {
            IJavaProject javaProject = JavaCore.create(getProject());
            IPath outputLocation = javaProject.getOutputLocation();
            IFolder outputFolder = ResourcesPlugin.getWorkspace().getRoot().getFolder(outputLocation);
            IFolder viewsFolder = getProject().getFolder(new Path("grails-app/views"));

            // Replace the stuff from the grails war with whatever stuff we have in our output folder...
            //  However, any files that exist in the .war but not our folder will not be touched.

            replace(cachedMembers, "WEB-INF/classes", outputFolder);
            replace(cachedMembers, "WEB-INF/grails-app/views", viewsFolder);

            removePrecompiledGSPs();
            // TODO: KDV: (deploy) write some code to produce a report of what we are still borrowing from the grails war file.
            //    If we can make the report empty then the war file is obsolete.
            //         if (DEBUG) {
            //            debug(">>> entries in WEB-INF/classes taken from the grails war file");
            //            reportWarEntries(findFolder(cachedMembers, new Path("WEB-INF/classes")));
            //            debug("<<< entries in WEB-INF/classes taken from the grails war file");
            //         }
        }
    }
}

From source file:org.grails.ide.eclipse.runonserver.GrailsAppModuleDelegate.java

License:Open Source License

/**
 * @return true is Greclipse output folder and the grails war output folder are being shared.
 * (This is really not desirable, but we have to deal with it).
 *///from w  w  w.  ja va 2s .  co  m
private boolean isOverlappingOutputFolders() {
    IJavaProject javaProject = JavaCore.create(getProject());
    try {
        IPath javaOutputLocation = javaProject.getOutputLocation().removeFirstSegments(1); //Drop project name
        IPath warOutputLocation = new Path(WAR_OUTPUT_FOLDER);
        return javaOutputLocation.equals(warOutputLocation);
    } catch (JavaModelException e) {
        GrailsCoreActivator.log(e);
        return true; //Assume the worst and proceed.
    }
}

From source file:org.grails.ide.eclipse.ui.internal.importfixes.GrailsOutputFolderFixer.java

License:Open Source License

/**
 * Called by project version fixer when it detects a new grails project in the workspace.
 * <p>//ww w  .j a v  a  2 s. c om
 * This is called after the version fixer has already applied its fixer logic. (But beware that
 * the fixer actually shedules most of its work as jobs so the actually fixes probably
 * have not yet been applied.
 */
public void fix(IProject project) {
    try {
        if (project.hasNature(JavaCore.NATURE_ID)) {
            final IJavaProject javaProject = JavaCore.create(project);
            IPath outputFolder = javaProject.getOutputLocation();
            if (isBadOutputFolder(outputFolder)) {
                WorkspaceJob job = new WorkspaceJob("Fix output folder") {
                    @Override
                    public IStatus runInWorkspace(IProgressMonitor monitor) throws CoreException {
                        try {
                            //We recheck the fixing condition because, it may have changed by the time the job is scheduled.
                            //This is because version fixer in some cases calls eclipsify (also from a scheduled job) and
                            //this also fixes the output folder. It is probably harmless to fix twice, but it does
                            //cause a potential build, so it is best not to do it.
                            IPath outputFolder = javaProject.getOutputLocation();
                            if (isBadOutputFolder(outputFolder)) {
                                GrailsCommandUtils.setDefaultOutputFolder(javaProject);
                            }
                            return Status.OK_STATUS;
                        } catch (Exception e) {
                            return new Status(IStatus.WARNING, GrailsCoreActivator.PLUGIN_ID,
                                    "Error while fixing output folder for project "
                                            + javaProject.getElementName());
                        }
                    }
                };
                job.setRule(ResourcesPlugin.getWorkspace().getRuleFactory().buildRule());
                job.setPriority(Job.INTERACTIVE);
                job.schedule();
            }
        }
    } catch (Exception e) {
        GrailsCoreActivator.log(e);
    }
}

From source file:org.granite.builder.util.ProjectUtil.java

License:Open Source License

public static List<CpEntry> getFullClasspath(IJavaProject project, IClasspathEntry[] classpathEntries)
        throws CoreException {
    List<CpEntry> classpath = new ArrayList<CpEntry>();

    IWorkspaceRoot workspace = project.getProject().getWorkspace().getRoot();
    IPath path = null;//from w  w w .  j a  v  a2s. co m
    try {

        // Output locations.
        if (project.getOutputLocation() != null) {
            path = project.getOutputLocation();
            File file = workspace.findMember(path).getLocation().toFile();
            classpath.add(new CpEntry(path.makeRelative().toString(), path, file,
                    CpEntry.CpeKind.PROJECT_OUTPUT_DIR));
        }
        for (IClasspathEntry cpe : classpathEntries) {
            if (cpe.getEntryKind() == IClasspathEntry.CPE_SOURCE && cpe.getOutputLocation() != null) {
                path = cpe.getOutputLocation();
                File file = workspace.findMember(path).getLocation().toFile();
                classpath.add(new CpEntry(path.makeRelative().toString(), path, file,
                        CpEntry.CpeKind.SOURCE_OUTPUT_DIR));
            }
        }

        // Project jars.
        for (IClasspathEntry cpe : classpathEntries) {
            if (cpe.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                path = cpe.getPath();
                if (path != null) {
                    IResource member = workspace.findMember(path);
                    String description = path.lastSegment();
                    if (path.segmentCount() > 1)
                        description += " - " + path.removeLastSegments(1).makeRelative();
                    if (member != null)
                        classpath.add(new CpEntry(description, path, member.getLocation().toFile(),
                                CpEntry.CpeKind.LIB_JAR));
                    else
                        classpath.add(new CpEntry(description, path, path.toFile(), CpEntry.CpeKind.LIB_JAR));
                }
            }
        }

        // Containers jars/directories.
        for (IClasspathEntry cpe : classpathEntries) {
            if (cpe.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                path = cpe.getPath();
                IClasspathContainer container = JavaCore.getClasspathContainer(path, project);
                String description = container.getDescription();
                CpEntry entry = new CpEntry(description, path, (path != null ? path.toFile() : null),
                        CpEntry.CpeKind.CONTAINER_JAR);

                for (IClasspathEntry ccpe : container.getClasspathEntries()) {
                    path = ccpe.getPath();
                    String label = path.lastSegment();
                    if (path.segmentCount() > 1)
                        label += " - " + path.removeLastSegments(1).makeRelative();

                    File file = path.toFile();
                    IResource resource = workspace.findMember(path);
                    if (resource != null)
                        file = resource.getLocation().toFile();

                    entry.getChildren().add(new CpEntry(label, path, file, CpEntry.CpeKind.LIB_JAR));
                }

                classpath.add(entry);
            }
        }

    } catch (Exception e) {
        String msg = "Problem with classpath location: " + path;
        throw new CoreException(createErrorStatus(msg, e));
    }

    return classpath;
}

From source file:org.granite.builder.util.ProjectUtil.java

License:Open Source License

public static IPath getOutputFolder(IJavaProject project, IClasspathEntry cpe) throws CoreException {
    if (cpe.getOutputLocation() != null)
        return cpe.getOutputLocation();
    return project.getOutputLocation();
}

From source file:org.granite.builder.util.ProjectUtil.java

License:Open Source License

public static List<IPath> getOutputFolders(IJavaProject project) throws CoreException {
    List<IPath> paths = new ArrayList<IPath>();

    for (IClasspathEntry cpe : project.getRawClasspath()) {
        if (cpe.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            IPath output = cpe.getOutputLocation();
            if (output != null)
                paths.add(output);/*from w w w .  j a  v a 2 s.  c om*/
        }
    }
    IPath output = project.getOutputLocation();
    if (output != null)
        paths.add(output);

    return paths;
}

From source file:org.granite.builder.util.ProjectUtil.java

License:Open Source License

public static JavaClassInfo getJavaClassInfo(IJavaProject project, IFile resource) throws CoreException {
    // classPath = "<project name>/<output folder>/<package>/<file>.class"
    IPath classPath = resource.getFullPath().makeRelative();

    // Find output folder: "<project name>/<output folder>".
    IPath outputFolder = null;/*w  ww .  j  av  a  2 s.  c  o  m*/
    if (project.getOutputLocation() != null && project.getOutputLocation().isPrefixOf(classPath))
        outputFolder = project.getOutputLocation();
    else {
        for (IClasspathEntry cpe : project.getRawClasspath()) {
            if (cpe.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                IPath output = cpe.getOutputLocation();
                if (output != null && output.isPrefixOf(classPath)) {
                    outputFolder = output;
                    break;
                }
            }
        }
    }

    if (outputFolder == null)
        return null;

    // Compute class name.
    IPath relativeClassPath = classPath.removeFirstSegments(outputFolder.segmentCount());
    IPath relativeClassName = relativeClassPath.removeFileExtension();
    String className = relativeClassName.toPortableString().replace('/', '.');

    // Compute java source path: "<package>/<class name>[$<inner class>].java".
    String javaFullPath = relativeClassName.addFileExtension("java").toPortableString();
    String javaFilePath = javaFullPath;
    int iDollar = javaFilePath.indexOf('$');
    if (iDollar != -1)
        javaFilePath = javaFilePath.substring(0, iDollar) + ".java";

    IJavaElement javaElement = project.findElement(new Path(javaFilePath));
    if (javaElement == null)
        return null;

    IJavaElement sourceFolder = javaElement.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
    if (sourceFolder == null)
        return null;

    IPath folderPath = sourceFolder.getPath().makeRelative();
    if (folderPath.segmentCount() > 0)
        folderPath = folderPath.removeFirstSegments(1);

    // Fix issues with project not in the workspace directory.
    outputFolder = project.getProject().getWorkspace().getRoot().findMember(outputFolder).getLocation();

    return new JavaClassInfo(folderPath.toPortableString(), javaFullPath, className,
            new File(outputFolder.toPortableString(), relativeClassPath.toPortableString()));
}