Example usage for org.eclipse.jdt.apt.core.util IFactoryPath addVarJar

List of usage examples for org.eclipse.jdt.apt.core.util IFactoryPath addVarJar

Introduction

In this page you can find the example usage for org.eclipse.jdt.apt.core.util IFactoryPath addVarJar.

Prototype

public void addVarJar(IPath jarPath);

Source Link

Document

Add a jar file in the workspace, specified with a classpath variable, to the head of the factory path.

Usage

From source file:org.jboss.tools.maven.apt.internal.AbstractAptConfiguratorDelegate.java

License:Open Source License

/**
 * Configures APT for the specified Maven project.
 *///from   w  w w . ja v  a  2s.  co  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.maven.ide.querydsl.AptConfigurator.java

License:Open Source License

/**
 * @author Ivica Loncar//w w  w . ja v  a2s.c  o  m
 */
@Override
public void configure(final ProjectConfigurationRequest p_request, final IProgressMonitor p_monitor)
        throws CoreException {
    super.configure(p_request, p_monitor);

    IProject project = p_request.getProject();
    if (project.hasNature(JavaCore.NATURE_ID)) {
        // enable annotation processing
        IJavaProject javaProject = JavaCore.create(project);
        AptConfig.setEnabled(javaProject, true);

        MavenFacade mavenFacade = new MavenFacade(p_request, p_monitor);
        // Associate annotation processor jar. Since querydsl doesn't use generic apt plugin nor specifies extra dependencies we have to hardcode it.
        QueryDslConfiguration queryDslConfiguration = getQueryDslConfiguration(mavenFacade);
        // IRepositoryRegistry repositoryRegistry = MavenPlugin.getRepositoryRegistry();
        // List<IRepository> repositories = repositoryRegistry.getRepositories(IRepositoryRegistry.SCOPE_LOCAL | IRepositoryRegistry.SCOPE_SETTINGS | IRepositoryRegistry.SCOPE_PROJECT);

        IMaven maven = MavenPlugin.getMaven();

        IPath m2RepoVar = JavaCore.getClasspathVariable(M2_REPO);
        IFactoryPath factoryPath = AptConfig.getFactoryPath(javaProject);

        ArtifactMetadata[] artifactsMetadata = queryDslConfiguration.getProcessorArtifacts();
        for (ArtifactMetadata artifactMetadata : artifactsMetadata) {
            Artifact artifact = mavenFacade.resolve(artifactMetadata);

            File file = artifact.getFile();
            if ((file == null) || !file.exists() || !file.canRead()) {
                throw new IllegalStateException("Cannot find file for artifact " + artifact + " file:" + file);
            }
            String filePath = file.getAbsolutePath();
            IPath path = Path.fromOSString(filePath);

            IPath relativeToM2Repo = path.makeRelativeTo(m2RepoVar);

            IPath jarPath = Path.fromOSString("M2_REPO").append(relativeToM2Repo);

            factoryPath.addVarJar(jarPath);
        }

        AptConfig.setFactoryPath(javaProject, factoryPath);

        Map<String, String> options = new HashMap<String, String>() {
            {
                // we would like to override existing files
                put("defaultOverride", "true");
            }
        };
        AptConfig.setProcessorOptions(options, javaProject);

        AptConfig.setGenSrcDir(javaProject, queryDslConfiguration.getOutputDirectory());
    }
}

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  ww  w.  ja  v a2 s.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);
    }
}