List of usage examples for org.eclipse.jdt.core IJavaProject readRawClasspath
IClasspathEntry[] readRawClasspath();
.classpath
file from disk, or null
if unable to read the file. From source file:net.sf.jasperreports.eclipse.wizard.project.ProjectUtil.java
License:Open Source License
public static void createJRClasspathContainer(IProgressMonitor monitor, IJavaProject javaProject) throws JavaModelException { List<IClasspathEntry> centries = new ArrayList<IClasspathEntry>(); IClasspathEntry[] entries = javaProject.readRawClasspath(); for (IClasspathEntry en : entries) { if (en.getPath().equals(JRClasspathContainer.ID)) return; }/*from ww w . java 2 s . c o m*/ centries.addAll(Arrays.asList(entries)); createJRClasspathContainer(null, centries, javaProject); }
From source file:net.sf.jasperreports.eclipse.wizard.project.ProjectUtil.java
License:Open Source License
public static void addFileToClasspath(IProgressMonitor monitor, IFile file) throws CoreException { if (file.getProject().getNature(JavaCore.NATURE_ID) != null) { IJavaProject jprj = JavaCore.create(file.getProject()); List<IClasspathEntry> centries = new ArrayList<IClasspathEntry>(); IClasspathEntry[] entries = jprj.readRawClasspath(); for (IClasspathEntry en : entries) { if (en.getPath().equals(file.getFullPath())) return; }/*from w w w .j a va 2 s . c o m*/ centries.add(JavaCore.newLibraryEntry(file.getFullPath(), null, new Path("/"))); centries.addAll(Arrays.asList(entries)); jprj.setRawClasspath(centries.toArray(new IClasspathEntry[centries.size()]), monitor); } }
From source file:org.ebayopensource.turmeric.eclipse.maven.core.m2eclipse.MavenProjectConfigurator.java
License:Open Source License
@Override public void configure(ProjectConfigurationRequest request, IProgressMonitor monitor) throws CoreException { if (request == null) { return;//w w w .j a va2 s. c om } if (request.isProjectImport() == true) { IProject project = request.getProject(); SupportedProjectType projectType = null; if (isValidInterfaceProject(project) && TurmericServiceUtils.isSOAInterfaceProject(project) == false) { projectType = SupportedProjectType.INTERFACE; } else if (isValidImplementationProject(project) && TurmericServiceUtils.isSOAImplProject(project) == false) { projectType = SupportedProjectType.IMPL; } else if (isValidConsumerProject(project) && TurmericServiceUtils.isSOAConsumerProject(project) == false) { projectType = SupportedProjectType.CONSUMER; } else if (isValidTypeLibraryProject(project) && TurmericServiceUtils.isSOATypeLibraryProject(project) == false) { projectType = SupportedProjectType.TYPE_LIBRARY; } else if (isValidErrorLibraryProject(project) && TurmericServiceUtils.isSOAErrorLibraryProject(project) == false) { projectType = SupportedProjectType.ERROR_LIBRARY; } else { //OK this is not a Turmeric project after all. return; } String natureId = GlobalRepositorySystem.instanceOf().getActiveRepositorySystem() .getProjectNatureId(projectType); if (StringUtils.isNotBlank(natureId)) { //it is a SOA project JDTUtil.addNatures(project, monitor, natureId); InputStream input = null; try { input = request.getPom().getContents(); Model model = MavenCoreUtils.mavenEclipseAPI().parsePom(input); Build build = model.getBuild(); final IJavaProject javaProject = JavaCore.create(project); List<IPath> srcDirs = JDTUtil.getSourceDirectories(project); List<IPath> additionalSrcDirs = new ArrayList<IPath>(); Plugin: for (Plugin plugin : build.getPlugins()) { if (plugin.getArtifactId().equals("build-helper-maven-plugin")) { for (PluginExecution exec : plugin.getExecutions()) { if ("add-source".equals(exec.getId()) && exec.getConfiguration() != null) { String xml = exec.getConfiguration().toString(); InputStream ins = null; try { ins = new ByteArrayInputStream(xml.getBytes()); Document doc = JDOMUtil.readXML(ins); Element elem = doc.getRootElement().getChild("sources"); if (elem != null) { for (Object obj : elem.getChildren("source")) { if (obj instanceof Element) { IPath src = new Path(((Element) obj).getTextTrim()); if (srcDirs.contains(src) == false) { additionalSrcDirs.add(src); } } } } } finally { IOUtils.closeQuietly(ins); } break Plugin; } } } } if (additionalSrcDirs.isEmpty() == false) { final List<IClasspathEntry> entries = ListUtil.arrayList(javaProject.readRawClasspath()); IPath outputDir = project.getFolder(build.getOutputDirectory()).getFullPath(); List<String> missingDirs = new ArrayList<String>(); for (IPath path : additionalSrcDirs) { IFolder folder = project.getFolder(path); if (folder.exists() == false) { missingDirs.add(path.toString()); } IPath srcPath = project.getFolder(path).getFullPath(); if (containsSourcePath(entries, srcPath) == false) { entries.add(JavaCore.newSourceEntry(srcPath, new IPath[0], new IPath[0], outputDir.makeAbsolute())); } } if (missingDirs.isEmpty() == false) { WorkspaceUtil.createFolders(project, missingDirs, monitor); } javaProject.setRawClasspath(entries.toArray(new IClasspathEntry[0]), monitor); } } catch (Exception e) { throw new CoreException(EclipseMessageUtils.createErrorStatus(e)); } finally { IOUtils.closeQuietly(input); } } } }
From source file:org.ebayopensource.turmeric.eclipse.maven.sconfig.TurmerStandardProjectConfigurator.java
License:Open Source License
@Override public void configure(ProjectConfigurationRequest projRequest, IProgressMonitor monitor) throws CoreException { if (projRequest == null) { return;/* www . j a v a2s. com*/ } SupportedProjectType projectType = null; IProject project = projRequest.getProject(); if (isInterfaceProject(projRequest)) { projectType = SupportedProjectType.INTERFACE; } else if (isImplementationProject(projRequest)) { projectType = SupportedProjectType.IMPL; } else if (isErrorLibProject(projRequest)) { projectType = SupportedProjectType.ERROR_LIBRARY; } else if (isTypeLibProject(projRequest)) { projectType = SupportedProjectType.TYPE_LIBRARY; } else if (isConsumerLibProject(projRequest)) { projectType = SupportedProjectType.CONSUMER; } else { return; } String natureId = GlobalRepositorySystem.instanceOf().getActiveRepositorySystem() .getProjectNatureId(projectType); JDTUtil.addNatures(project, monitor, natureId); List<IPath> additionalSrcDirs = new ArrayList<IPath>(); additionalSrcDirs.add(new Path("target/generated-sources/codegen")); additionalSrcDirs.add(new Path("target/generated-resources/codegen")); final IJavaProject javaProject = JavaCore.create(project); final List<IClasspathEntry> entries = ListUtil.arrayList(javaProject.readRawClasspath()); for (IPath path : additionalSrcDirs) { IFolder folder = project.getFolder(path); if (folder.exists()) { IPath srcPath = project.getFolder(path).getFullPath(); if (containsSourcePath(entries, srcPath) == false) { entries.add(JavaCore.newSourceEntry(srcPath, new IPath[0])); } } } javaProject.setRawClasspath(entries.toArray(new IClasspathEntry[0]), monitor); }
From source file:org.ebayopensource.turmeric.eclipse.utils.plugin.JDTUtil.java
License:Open Source License
/** * Raw classpath./*w ww .ja va 2 s . c o m*/ * * @param project the project * @param readFromDisk the read from disk * @return the list * @throws JavaModelException the java model exception */ public static List<IClasspathEntry> rawClasspath(final IJavaProject project, final boolean readFromDisk) throws JavaModelException { final List<IClasspathEntry> entries = ListUtil.list(); if (project == null || !project.getProject().isAccessible()) return entries; if (!readFromDisk && project.getProject().getFile(".classpath").isAccessible() && project.getProject().getFile(".classpath").isSynchronized(IResource.DEPTH_INFINITE)) ListUtil.add(entries, project.getRawClasspath()); else ListUtil.add(entries, project.readRawClasspath()); return entries; }
From source file:org.ebayopensource.turmeric.eclipse.utils.plugin.JDTUtil.java
License:Open Source License
/** * Gets the source directories./* w w w . ja va 2 s . com*/ * * @param project the project * @return the source directories */ public static List<IPath> getSourceDirectories(final IProject project) { final IJavaProject jProject = JavaCore.create(project); final List<IPath> srcEntries = new ArrayList<IPath>(); for (final IClasspathEntry entry : jProject.readRawClasspath()) { if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) { srcEntries.add(entry.getPath()); } } return srcEntries; }
From source file:org.eclim.plugin.jdt.project.JavaProjectManager.java
License:Open Source License
@Override public List<Error> update(IProject project, CommandLine commandLine) throws Exception { String buildfile = commandLine.getValue(Options.BUILD_FILE_OPTION); IJavaProject javaProject = JavaUtils.getJavaProject(project); javaProject.getResource().refreshLocal(IResource.DEPTH_INFINITE, null); // validate that .classpath xml is well formed and valid. PluginResources resources = (PluginResources) Services.getPluginResources(PluginResources.NAME); List<Error> errors = XmlUtils.validateXml(javaProject.getProject().getName(), CLASSPATH, resources.getResource(CLASSPATH_XSD).toString()); if (errors.size() > 0) { return errors; }/*from ww w . j a v a 2 s . c om*/ String dotclasspath = javaProject.getProject().getFile(CLASSPATH).getRawLocation().toOSString(); // ivy.xml, pom.xml, etc updated. if (buildfile != null) { try { IClasspathEntry[] entries = mergeWithBuildfile(javaProject, buildfile); errors = setClasspath(javaProject, entries, dotclasspath); } catch (IllegalStateException ise) { errors.add(new Error(ise.getMessage(), buildfile, 1, 1, false)); } // .classpath updated. } else { // if an exception occurs reading the classpath then eclipse will return a // default classpath which we would otherwise then write back into the // .classpath file. This hack prevents that and will return a relevent // error message as a validation error. try { ((JavaProject) javaProject).readFileEntriesWithException(null); } catch (Exception e) { errors.add(new Error(e.getMessage(), dotclasspath, 1, 1, false)); return errors; } IClasspathEntry[] entries = javaProject.readRawClasspath(); errors = setClasspath(javaProject, entries, dotclasspath); } if (errors.size() > 0) { return errors; } return null; }
From source file:org.eclipse.andmore.internal.build.builders.ResourceManagerBuilder.java
License:Open Source License
@SuppressWarnings("unchecked") @Override/*from w w w . ja v a2 s .c o m*/ protected IProject[] build(int kind, Map args, IProgressMonitor monitor) throws CoreException { // Get the project. final IProject project = getProject(); IJavaProject javaProject = JavaCore.create(project); // Clear the project of the generic markers removeMarkersFromContainer(project, AndmoreAndroidConstants.MARKER_ADT); // check for existing target marker, in which case we abort. // (this means: no SDK, no target, or unresolvable target.) try { abortOnBadSetup(javaProject, null); } catch (AbortBuildException e) { return null; } // Check the compiler compliance level, displaying the error message // since this is the first builder. Pair<Integer, String> result = ProjectHelper.checkCompilerCompliance(project); String errorMessage = null; switch (result.getFirst().intValue()) { case ProjectHelper.COMPILER_COMPLIANCE_LEVEL: errorMessage = Messages.Requires_Compiler_Compliance_s; break; case ProjectHelper.COMPILER_COMPLIANCE_SOURCE: errorMessage = Messages.Requires_Source_Compatibility_s; break; case ProjectHelper.COMPILER_COMPLIANCE_CODEGEN_TARGET: errorMessage = Messages.Requires_Class_Compatibility_s; break; } if (errorMessage != null) { errorMessage = String.format(errorMessage, result.getSecond() == null ? "(no value)" : result.getSecond()); if (JavaCore.VERSION_1_7.equals(result.getSecond())) { // If the user is trying to target 1.7 but compiling with something older, // the error message can be a bit misleading; instead point them in the // direction of updating the project's build target. Sdk currentSdk = Sdk.getCurrent(); if (currentSdk != null) { IAndroidTarget target = currentSdk.getTarget(project.getProject()); if (target != null && target.getVersion().getApiLevel() < 19) { errorMessage = "Using 1.7 requires compiling with Android 4.4 " + "(KitKat); currently using " + target.getVersion(); } ProjectState projectState = Sdk.getProjectState(project); if (projectState != null) { BuildToolInfo buildToolInfo = projectState.getBuildToolInfo(); if (buildToolInfo == null) { buildToolInfo = currentSdk.getLatestBuildTool(); } if (buildToolInfo != null && buildToolInfo.getRevision().getMajor() < 19) { errorMessage = "Using 1.7 requires using Android Build Tools " + "version 19 or later; currently using " + buildToolInfo.getRevision(); } } } } markProject(AndmoreAndroidConstants.MARKER_ADT, errorMessage, IMarker.SEVERITY_ERROR); AndmoreAndroidPlugin.printErrorToConsole(project, errorMessage); return null; } // Check that the SDK directory has been setup. String osSdkFolder = AndmoreAndroidPlugin.getOsSdkFolder(); if (osSdkFolder == null || osSdkFolder.length() == 0) { AndmoreAndroidPlugin.printErrorToConsole(project, Messages.No_SDK_Setup_Error); markProject(AndmoreAndroidConstants.MARKER_ADT, Messages.No_SDK_Setup_Error, IMarker.SEVERITY_ERROR); return null; } // check the 'gen' source folder is present boolean hasGenSrcFolder = false; // whether the project has a 'gen' source folder setup IClasspathEntry[] classpaths = javaProject.readRawClasspath(); if (classpaths != null) { for (IClasspathEntry e : classpaths) { if (e.getEntryKind() == IClasspathEntry.CPE_SOURCE) { IPath path = e.getPath(); if (path.segmentCount() == 2 && path.segment(1).equals(SdkConstants.FD_GEN_SOURCES)) { hasGenSrcFolder = true; break; } } } } boolean genFolderPresent = false; // whether the gen folder actually exists IResource resource = project.findMember(SdkConstants.FD_GEN_SOURCES); genFolderPresent = resource != null && resource.exists(); if (hasGenSrcFolder == false && genFolderPresent) { // No source folder setup for 'gen' in the project, but there's already a // 'gen' resource (file or folder). String message; if (resource.getType() == IResource.FOLDER) { // folder exists already! This is an error. If the folder had been created // by the NewProjectWizard, it'd be a source folder. message = String.format( "%1$s already exists but is not a source folder. Convert to a source folder or rename it.", resource.getFullPath().toString()); } else { // resource exists but is not a folder. message = String.format( "Resource %1$s is in the way. ADT needs a source folder called 'gen' to work. Rename or delete resource.", resource.getFullPath().toString()); } AndmoreAndroidPlugin.printErrorToConsole(project, message); markProject(AndmoreAndroidConstants.MARKER_ADT, message, IMarker.SEVERITY_ERROR); return null; } else if (hasGenSrcFolder == false || genFolderPresent == false) { // either there is no 'gen' source folder in the project (older SDK), // or the folder does not exist (was deleted, or was a fresh svn checkout maybe.) // In case we are migrating from an older SDK, we go through the current source // folders and delete the generated Java files. List<IPath> sourceFolders = BaseProjectHelper.getSourceClasspaths(javaProject); IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); for (IPath path : sourceFolders) { IResource member = root.findMember(path); if (member != null) { removeDerivedResources(member, monitor); } } // create the new source folder, if needed IFolder genFolder = project.getFolder(SdkConstants.FD_GEN_SOURCES); if (genFolderPresent == false) { AndmoreAndroidPlugin.printBuildToConsole(BuildVerbosity.VERBOSE, project, "Creating 'gen' source folder for generated Java files"); genFolder.create(true /* force */, true /* local */, new SubProgressMonitor(monitor, 10)); } // add it to the source folder list, if needed only (or it will throw) if (hasGenSrcFolder == false) { IClasspathEntry[] entries = javaProject.getRawClasspath(); entries = ProjectHelper.addEntryToClasspath(entries, JavaCore.newSourceEntry(genFolder.getFullPath())); javaProject.setRawClasspath(entries, new SubProgressMonitor(monitor, 10)); } // refresh specifically the gen folder first, as it may break the build // if it doesn't arrive in time then refresh the whole project as usual. genFolder.refreshLocal(IResource.DEPTH_ZERO, new SubProgressMonitor(monitor, 10)); project.refreshLocal(IResource.DEPTH_INFINITE, new SubProgressMonitor(monitor, 10)); // it seems like doing this fails to properly rebuild the project. the Java builder // running right after this builder will not see the gen folder, and will not be // restarted after this build. Therefore in this particular case, we start another // build asynchronously so that it's rebuilt after this build. launchJob(new Job("rebuild") { @Override protected IStatus run(IProgressMonitor m) { try { project.build(IncrementalProjectBuilder.INCREMENTAL_BUILD, m); return Status.OK_STATUS; } catch (CoreException e) { return e.getStatus(); } } }); } // convert older projects which use bin as the eclipse output folder into projects // using bin/classes IFolder androidOutput = BaseProjectHelper.getAndroidOutputFolder(project); IFolder javaOutput = BaseProjectHelper.getJavaOutputFolder(project); if (androidOutput.exists() == false || javaOutput == null || javaOutput.getParent().equals(androidOutput) == false) { // get what we want as the new java output. IFolder newJavaOutput = androidOutput.getFolder(SdkConstants.FD_CLASSES_OUTPUT); if (androidOutput.exists() == false) { androidOutput.create(true /*force*/, true /*local*/, monitor); } if (newJavaOutput.exists() == false) { newJavaOutput.create(true /*force*/, true /*local*/, monitor); } // set the java output to this project. javaProject.setOutputLocation(newJavaOutput.getFullPath(), monitor); // need to do a full build. Can't build while we're already building, so launch a // job to build it right after this build launchJob(new Job("rebuild") { @Override protected IStatus run(IProgressMonitor jobMonitor) { try { project.build(IncrementalProjectBuilder.CLEAN_BUILD, jobMonitor); return Status.OK_STATUS; } catch (CoreException e) { return e.getStatus(); } } }); } // check that we have bin/res/ IFolder binResFolder = androidOutput.getFolder(SdkConstants.FD_RESOURCES); if (binResFolder.exists() == false) { binResFolder.create(true /* force */, true /* local */, new SubProgressMonitor(monitor, 10)); project.refreshLocal(IResource.DEPTH_ONE, new SubProgressMonitor(monitor, 10)); } // Check the preference to be sure we are supposed to refresh // the folders. if (AdtPrefs.getPrefs().getBuildForceResResfresh()) { AndmoreAndroidPlugin.printBuildToConsole(BuildVerbosity.VERBOSE, project, Messages.Refreshing_Res); // refresh the res folder. IFolder resFolder = project.getFolder(AndmoreAndroidConstants.WS_RESOURCES); resFolder.refreshLocal(IResource.DEPTH_INFINITE, monitor); // Also refresh the assets folder to make sure the ApkBuilder // will now it's changed and will force a new resource packaging. IFolder assetsFolder = project.getFolder(AndmoreAndroidConstants.WS_ASSETS); assetsFolder.refreshLocal(IResource.DEPTH_INFINITE, monitor); } return null; }
From source file:org.eclipse.andmore.internal.build.BuildHelper.java
License:Open Source License
/** * Computes all the project output and dependencies that must go into building the apk. * * @param resMarker//from www.j a va 2 s . c om * @throws CoreException */ private void gatherPaths(ResourceMarker resMarker) throws CoreException { IWorkspaceRoot wsRoot = ResourcesPlugin.getWorkspace().getRoot(); // get a java project for the project. IJavaProject javaProject = JavaCore.create(mProject); // get the output of the main project IPath path = javaProject.getOutputLocation(); IResource outputResource = wsRoot.findMember(path); if (outputResource != null && outputResource.getType() == IResource.FOLDER) { mCompiledCodePaths.add(outputResource.getLocation().toOSString()); } // we could use IJavaProject.getResolvedClasspath directly, but we actually // want to see the containers themselves. IClasspathEntry[] classpaths = javaProject.readRawClasspath(); if (classpaths != null) { for (IClasspathEntry e : classpaths) { // ignore non exported entries, unless they're in the DEPEDENCIES container, // in which case we always want it (there may be some older projects that // have it as non exported). if (e.isExported() || (e.getEntryKind() == IClasspathEntry.CPE_CONTAINER && e.getPath().toString().equals(AndmoreAndroidConstants.CONTAINER_DEPENDENCIES))) { handleCPE(e, javaProject, wsRoot, resMarker); } } } }
From source file:org.eclipse.andmore.internal.resources.manager.ProjectClassLoader.java
License:Open Source License
/** * Returns an array of external jar files used by the project. * @return an array of OS-specific absolute file paths *//*from w w w .ja v a 2s . c o m*/ private final URL[] getExternalJars() { // get a java project from it IJavaProject javaProject = JavaCore.create(mJavaProject.getProject()); ArrayList<URL> oslibraryList = new ArrayList<URL>(); IClasspathEntry[] classpaths = javaProject.readRawClasspath(); if (classpaths != null) { for (IClasspathEntry e : classpaths) { if (e.getEntryKind() == IClasspathEntry.CPE_LIBRARY || e.getEntryKind() == IClasspathEntry.CPE_VARIABLE) { // if this is a classpath variable reference, we resolve it. if (e.getEntryKind() == IClasspathEntry.CPE_VARIABLE) { e = JavaCore.getResolvedClasspathEntry(e); } handleClassPathEntry(e, oslibraryList); } else if (e.getEntryKind() == IClasspathEntry.CPE_CONTAINER) { // get the container. try { IClasspathContainer container = JavaCore.getClasspathContainer(e.getPath(), javaProject); // ignore the system and default_system types as they represent // libraries that are part of the runtime. if (container != null && container.getKind() == IClasspathContainer.K_APPLICATION) { IClasspathEntry[] entries = container.getClasspathEntries(); for (IClasspathEntry entry : entries) { // TODO: Xav -- is this necessary? if (entry.getEntryKind() == IClasspathEntry.CPE_VARIABLE) { entry = JavaCore.getResolvedClasspathEntry(entry); } handleClassPathEntry(entry, oslibraryList); } } } catch (JavaModelException jme) { // can't resolve the container? ignore it. AndmoreAndroidPlugin.log(jme, "Failed to resolve ClasspathContainer: %s", e.getPath()); } } } } return oslibraryList.toArray(new URL[oslibraryList.size()]); }