This repository has been archived by the owner on Feb 18, 2022. It is now read-only.
/
AbstractAptProjectConfigurator.java
152 lines (126 loc) · 6.34 KB
/
AbstractAptProjectConfigurator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*******************************************************************************
* Copyright (c) 2011 Knowledge Computing Corp. and others
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Karl M. Davis (Knowledge Computing Corp.) - initial API and implementation
* Red Hat, Inc - refactoring and abstraction of the logic
*******************************************************************************/
package org.jboss.tools.maven.apt.internal;
import org.jboss.tools.maven.apt.MavenJdtAptPlugin;
import org.jboss.tools.maven.apt.preferences.AnnotationProcessingMode;
import org.jboss.tools.maven.apt.preferences.IPreferencesManager;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jdt.core.JavaCore;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.MojoExecution;
import org.eclipse.m2e.core.lifecyclemapping.model.IPluginExecutionMetadata;
import org.eclipse.m2e.core.project.IMavenProjectFacade;
import org.eclipse.m2e.core.project.configurator.AbstractBuildParticipant;
import org.eclipse.m2e.core.project.configurator.AbstractProjectConfigurator;
import org.eclipse.m2e.core.project.configurator.ProjectConfigurationRequest;
import org.eclipse.m2e.jdt.IClasspathDescriptor;
import org.eclipse.m2e.jdt.IJavaProjectConfigurator;
/**
* <p>
* This {@link AbstractProjectConfigurator} implementation will set the APT configuration for an Eclipse Java project.
* </p>
* <p>
* Please note that the <code>maven-compiler-plugin</code> (at least as of version 2.3.2) will automatically perform
* annotation processing and generate annotation sources. This processing will include all annotation processors in the
* project's compilation classpath.
* </p>
* <p>
* However, there are a couple of problems that prevent the <code>maven-compiler-plugin</code>'s annotation processing
* from being sufficient when run within m2eclipse:
* </p>
* <ul>
* <li>The generated annotation sources are not added to the Maven project's source folders (nor should they be) and are
* thus not found by m2eclipse.</li>
* <li>Due to contention between Eclipse's JDT compilation and <code>maven-compiler-plugin</code> compilation, the Java
* compiler used by Eclipse may not recognize when the generated annotation sources/classes are out of date.</li>
* </ul>
* <p>
* The {@link AbstractAptProjectConfigurator} works around those limitations by configuring Eclipse's built-in annotation
* processing: APT. Unfortunately, the APT configuration will not allow for libraries, such as m2eclipse's
* "Maven Dependencies" to be used in the search path for annotation processors. Instead, the
* {@link AbstractAptProjectConfigurator} adds all of the project's <code>.jar</code> dependencies to the annotation processor
* search path.
* </p>
*/
public abstract class AbstractAptProjectConfigurator extends AbstractProjectConfigurator implements IJavaProjectConfigurator {
protected abstract AptConfiguratorDelegate getDelegate(AnnotationProcessingMode mode);
/**
* {@inheritDoc}
*/
@Override
public void configure(ProjectConfigurationRequest request, IProgressMonitor monitor) throws CoreException {
// This method may be called with null parameters to ensure its API is correct. We
// can ignore such calls.
if(request == null || monitor == null)
return;
// Get the objects needed for APT configuration
IMavenProjectFacade mavenProjectFacade = request.getMavenProjectFacade();
IProject eclipseProject = mavenProjectFacade.getProject();
AnnotationProcessingMode mode = getAnnotationProcessorMode(eclipseProject);
MavenSession mavenSession = request.getMavenSession();
AptConfiguratorDelegate configuratorDelegate = getDelegate(mode);
configuratorDelegate.setSession(mavenSession);
configuratorDelegate.setFacade(mavenProjectFacade);
// Configure APT
if (!configuratorDelegate.isIgnored(monitor)) {
configuratorDelegate.configureProject(monitor);
}
}
/**
* {@inheritDoc}
*/
public void configureClasspath(IMavenProjectFacade facade, IClasspathDescriptor classpath, IProgressMonitor monitor) {
/*
* Implementations of this method are supposed to configure the Maven project
* classpath: the "Maven Dependencies" container. We don't have any need to do
* that here.
*/
}
/**
* {@inheritDoc}
*/
public void configureRawClasspath(ProjectConfigurationRequest request, IClasspathDescriptor classpath,
IProgressMonitor monitor) throws CoreException {
/*
* We need to prevent/recover from the JavaProjectConfigurator removing the
* generated annotation sources directory from the classpath: it will be added
* when we configure the Eclipse APT preferences and then removed when the
* JavaProjectConfigurator runs.
*/
// Get the various project references we'll need
IProject eclipseProject = request.getProject();
if(!eclipseProject.hasNature(JavaCore.NATURE_ID))
return;
AptConfiguratorDelegate delegate = getDelegate(eclipseProject);
delegate.setFacade(request.getMavenProjectFacade());
delegate.setSession(request.getMavenSession());
// If this isn't a Java project, we have nothing to do
if (!delegate.isIgnored(monitor)) {
delegate.configureClasspath(classpath, monitor);
}
}
public AbstractBuildParticipant getBuildParticipant(IMavenProjectFacade projectFacade, MojoExecution execution,
IPluginExecutionMetadata executionMetadata) {
AptConfiguratorDelegate configuratorDelegate = getDelegate(projectFacade.getProject());
return configuratorDelegate.getMojoExecutionBuildParticipant(execution);
}
private AptConfiguratorDelegate getDelegate(IProject project) {
AnnotationProcessingMode mode = getAnnotationProcessorMode(project);
return getDelegate(mode);
}
protected AnnotationProcessingMode getAnnotationProcessorMode(IProject project) {
IPreferencesManager preferencesManager = MavenJdtAptPlugin.getDefault().getPreferencesManager();
return preferencesManager.getAnnotationProcessorMode(project);
}
}