List of usage examples for org.eclipse.jdt.apt.core.util AptConfig getDefaultFactoryPath
public static IFactoryPath getDefaultFactoryPath(IJavaProject jproj)
From source file:org.jboss.tools.maven.apt.AptProjectConfigurator.java
License:Open Source License
/** * Configures APT for the specified Maven project. * //from w w w . j a v a2 s .c om * @param eclipseProject an {@link IProject} reference to the Eclipse project being configured * @param mavenProject {@link IMavenProjectFacade} reference to the Maven project being configured * @param monitor the {@link IProgressMonitor} to use * @throws CoreException Any {@link CoreException}s thrown will be passed through. */ private void configureAptForProject(IProject eclipseProject, MavenSession mavenSession, IMavenProjectFacade mavenProjectFacade, IProgressMonitor monitor) throws CoreException { IJavaProject javaProject = JavaCore.create(eclipseProject); File generatedSourcesDirectory = getGeneratedSourcesDirectory(mavenSession, mavenProjectFacade, monitor); // If this isn't a Java project, we have nothing to do if (!eclipseProject.hasNature(JavaCore.NATURE_ID)) return; // If this project has no valid compiler plugin config, we have nothing to do if (generatedSourcesDirectory == null) return; // Get the project's dependencies List<Artifact> artifacts = getProjectArtifacts(mavenProjectFacade); List<File> resolvedJarArtifacts = filterToResolvedJars(artifacts); // Inspect the dependencies to see if any contain APT processors boolean isAnnotationProcessingEnabled = isAnnotationProcessingEnabled(mavenSession, mavenProjectFacade, monitor) //Will be ignored when org.bsc.maven:maven-processor-plugin is used && containsAptProcessors(resolvedJarArtifacts); // Enable/Disable APT (depends on whether APT processors were found) AptConfig.setEnabled(javaProject, isAnnotationProcessingEnabled); //If no annotation processor were found, we should leave. if (!isAnnotationProcessingEnabled) { return; } // Configure APT output path File generatedSourcesRelativeDirectory = convertToProjectRelativePath(eclipseProject, generatedSourcesDirectory); String generatedSourcesRelativeDirectoryPath = generatedSourcesRelativeDirectory.getPath(); AptConfig.setGenSrcDir(javaProject, generatedSourcesRelativeDirectoryPath); /* * Add all of the compile-scoped JAR artifacts to a new IFactoryPath (in * addition to the workspace's default entries). * * Please note that--until JDT-APT supports project factory path entries * (as opposed to just JARs)--this will be a bit wonky. Specifically, any * project dependencies will be excluded, but their transitive JAR * dependencies will be included. * * Also note: we add the artifacts in reverse order as * IFactoryPath.addExternalJar(File) adds items to the top of the factory * list. */ List<File> resolvedJarArtifactsInReverseOrder = new ArrayList<File>(resolvedJarArtifacts); Collections.reverse(resolvedJarArtifactsInReverseOrder); IFactoryPath factoryPath = AptConfig.getDefaultFactoryPath(javaProject); for (File resolvedJarArtifact : resolvedJarArtifactsInReverseOrder) { factoryPath.addExternalJar(resolvedJarArtifact); } // Apply that IFactoryPath to the project AptConfig.setFactoryPath(javaProject, factoryPath); // Apply and processor option found on the compiler plugins Map<String, String> processorOptions = getProcessorOptions(mavenSession, mavenProjectFacade, monitor); if (!processorOptions.isEmpty()) { AptConfig.setProcessorOptions(processorOptions, javaProject); } }
From source file:org.jboss.tools.maven.apt.internal.AbstractAptConfiguratorDelegate.java
License:Open Source License
/** * Configures APT for the specified Maven project. */// w w w.j a va 2s. c o m public void configureProject(IProgressMonitor monitor) throws CoreException { IProject eclipseProject = mavenFacade.getProject(); AnnotationProcessorConfiguration configuration = getAnnotationProcessorConfiguration(monitor); if (configuration == null) { return; } // In case the Javaconfigurator was not called yet (eg. maven-processor-plugin being bound to process-sources, // that project configurator runs first) We need to add the Java Nature before setting the APT config. if (!eclipseProject.hasNature(JavaCore.NATURE_ID)) { AbstractProjectConfigurator.addNature(eclipseProject, JavaCore.NATURE_ID, monitor); } File generatedSourcesDirectory = configuration.getOutputDirectory(); // If this project has no valid generatedSourcesDirectory, we have nothing to do if (generatedSourcesDirectory == null) return; IJavaProject javaProject = JavaCore.create(eclipseProject); //The plugin dependencies are added first to the classpath LinkedHashSet<File> resolvedJarArtifacts = new LinkedHashSet<File>(configuration.getDependencies()); // Get the project's dependencies List<Artifact> artifacts = getProjectArtifacts(mavenFacade); resolvedJarArtifacts.addAll(filterToResolvedJars(artifacts)); // Inspect the dependencies to see if any contain APT processors boolean isAnnotationProcessingEnabled = configuration.isAnnotationProcessingEnabled() && containsAptProcessors(resolvedJarArtifacts); // Enable/Disable APT (depends on whether APT processors were found) AptConfig.setEnabled(javaProject, isAnnotationProcessingEnabled); //If no annotation processor is disabled, we can leave. if (!isAnnotationProcessingEnabled) { return; } LOG.debug("Enabling APT support on {}", eclipseProject.getName()); // Configure APT output path File generatedSourcesRelativeDirectory = convertToProjectRelativePath(eclipseProject, generatedSourcesDirectory); String generatedSourcesRelativeDirectoryPath = generatedSourcesRelativeDirectory.getPath(); AptConfig.setGenSrcDir(javaProject, generatedSourcesRelativeDirectoryPath); /* * Add all of the compile-scoped JAR artifacts to a new IFactoryPath (in * addition to the workspace's default entries). * * Please note that--until JDT-APT supports project factory path entries * (as opposed to just JARs)--this will be a bit wonky. Specifically, any * project dependencies will be excluded, but their transitive JAR * dependencies will be included. * * Also note: we add the artifacts in reverse order as * IFactoryPath.addExternalJar(File) adds items to the top of the factory * list. */ List<File> resolvedJarArtifactsInReverseOrder = new ArrayList<File>(resolvedJarArtifacts); Collections.reverse(resolvedJarArtifactsInReverseOrder); IFactoryPath factoryPath = AptConfig.getDefaultFactoryPath(javaProject); IPath m2RepoPath = JavaCore.getClasspathVariable(M2_REPO); for (File resolvedJarArtifact : resolvedJarArtifactsInReverseOrder) { IPath absolutePath = new Path(resolvedJarArtifact.getAbsolutePath()); //reference jars in a portable way if (m2RepoPath != null && m2RepoPath.isPrefixOf(absolutePath)) { IPath relativePath = absolutePath.removeFirstSegments(m2RepoPath.segmentCount()).makeRelative() .setDevice(null); IPath variablePath = new Path(M2_REPO).append(relativePath); factoryPath.addVarJar(variablePath); } else { //fall back on using absolute references. factoryPath.addExternalJar(resolvedJarArtifact); } } Map<String, String> currentOptions = AptConfig.getProcessorOptions(javaProject); Map<String, String> newOptions = configuration.getAnnotationProcessorOptions(); if (!currentOptions.equals(newOptions)) { AptConfig.setProcessorOptions(newOptions, javaProject); } // Apply that IFactoryPath to the project AptConfig.setFactoryPath(javaProject, factoryPath); }
From source file:org.springframework.ide.eclipse.boot.properties.editor.util.AptUtils.java
License:Open Source License
/** * Enable's JDT APT on a JavaProject. Note: if the project's classpath contains * no APT services in jar-dependencies then this does nothing. *///from www.j ava 2s . c o m public static void configureApt(IJavaProject jp) { boolean shouldEnable = false; //becomes true if we find at least one annotation processor. try { IFactoryPath factoryPath = AptConfig.getDefaultFactoryPath(jp); for (File jarFile : JavaProjectUtil.getNonSystemJarDependencies(jp, true)) { if (!AnnotationServiceLocator.getAptServiceEntries(jarFile).isEmpty()) { shouldEnable = true; } IPath absolutePath = new Path(jarFile.getAbsolutePath()); IPath variablePath = useClasspathVariable(absolutePath); if (variablePath != null) { factoryPath.addVarJar(variablePath); } else { factoryPath.addExternalJar(jarFile); } } if (shouldEnable) { AptConfig.setEnabled(jp, true); AptConfig.setFactoryPath(jp, factoryPath); } else { AptConfig.setEnabled(jp, false); } } catch (Exception e) { SpringPropertiesEditorPlugin.log(e); } }