1212 *******************************************************************************/
1313package org .eclipse .m2e .pde .connector ;
1414
15+ import java .util .ArrayList ;
16+ import java .util .Arrays ;
17+ import java .util .List ;
18+ import java .util .Map ;
19+
1520import org .apache .maven .project .MavenProject ;
21+ import org .eclipse .core .resources .IContainer ;
22+ import org .eclipse .core .resources .IFile ;
23+ import org .eclipse .core .resources .IFolder ;
1624import org .eclipse .core .resources .IProject ;
1725import org .eclipse .core .runtime .CoreException ;
26+ import org .eclipse .core .runtime .IPath ;
1827import org .eclipse .core .runtime .IProgressMonitor ;
28+ import org .eclipse .core .runtime .SubMonitor ;
29+ import org .eclipse .m2e .core .internal .M2EUtils ;
1930import org .eclipse .m2e .core .project .IMavenProjectFacade ;
2031import org .eclipse .m2e .core .project .configurator .AbstractCustomizableLifecycleMapping ;
32+ import org .eclipse .m2e .core .project .configurator .AbstractProjectConfigurator ;
2133import org .eclipse .m2e .core .project .configurator .ILifecycleMapping ;
2234import org .eclipse .m2e .core .project .configurator .ProjectConfigurationRequest ;
35+ import org .eclipse .m2e .jdt .IClasspathDescriptor ;
36+ import org .eclipse .m2e .jdt .internal .AbstractJavaProjectConfigurator ;
37+ import org .eclipse .pde .core .build .IBuild ;
38+ import org .eclipse .pde .core .build .IBuildEntry ;
39+ import org .eclipse .pde .core .build .IBuildModel ;
40+ import org .eclipse .pde .core .plugin .IPluginModelBase ;
41+ import org .eclipse .pde .core .plugin .PluginRegistry ;
42+ import org .eclipse .pde .internal .core .ClasspathComputer ;
43+ import org .eclipse .pde .internal .core .build .WorkspaceBuildModel ;
44+ import org .eclipse .pde .internal .core .project .PDEProject ;
2345
46+ @ SuppressWarnings ("restriction" )
2447public class TychoLifecycleMapping extends AbstractCustomizableLifecycleMapping implements ILifecycleMapping {
2548
2649 @ Override
@@ -36,4 +59,92 @@ public void configure(ProjectConfigurationRequest request, IProgressMonitor moni
3659 }
3760 super .configure (request , monitor );
3861 }
62+
63+ @ Override
64+ public List <AbstractProjectConfigurator > getProjectConfigurators (IMavenProjectFacade projectFacade ,
65+ IProgressMonitor monitor ) {
66+ String packaging = projectFacade .getPackaging ();
67+ if ("eclipse-plugin" .equals (packaging ) || "eclipse-test-plugin" .equals (packaging )) {
68+ List <AbstractProjectConfigurator > list = new ArrayList <>(
69+ super .getProjectConfigurators (projectFacade , monitor ));
70+ list .add (new EclipsePluginProjectConfigurator ());
71+ return list ;
72+ } else {
73+ return super .getProjectConfigurators (projectFacade , monitor );
74+ }
75+ }
76+
77+ private static final class EclipsePluginProjectConfigurator extends AbstractJavaProjectConfigurator {
78+
79+ private static final String JAVA_SE = "JavaSE-" ;
80+
81+ @ Override
82+ protected void addProjectSourceFolders (IClasspathDescriptor classpath , Map <String , String > options ,
83+ ProjectConfigurationRequest request , IProgressMonitor monitor ) throws CoreException {
84+ SubMonitor subMonitor = SubMonitor .convert (monitor , 100 );
85+ MavenProject mavenProject = request .mavenProject ();
86+ IProject project = request .mavenProjectFacade ().getProject ();
87+ IBuild build = getBuild (project );
88+ if (build == null ) {
89+ super .addProjectSourceFolders (classpath , options , request , monitor );
90+ } else {
91+ IBuildEntry sourceEntry = build .getEntry (IBuildEntry .JAR_PREFIX + "." );
92+ if (sourceEntry == null ) {
93+ return ;
94+ }
95+ String outputDirectory ;
96+ IBuildEntry outputEntry = build .getEntry (IBuildEntry .OUTPUT_PREFIX + "." );
97+ if (outputEntry != null && outputEntry .getTokens ().length > 0 ) {
98+ outputDirectory = outputEntry .getTokens ()[0 ];
99+ } else {
100+ outputDirectory = mavenProject .getBuild ().getOutputDirectory ();
101+ }
102+ IFolder outputFolder = getFolder (project , outputDirectory );
103+ M2EUtils .createFolder (outputFolder , true , subMonitor .split (10 ));
104+ IPath [] inclusion = new IPath [0 ];
105+ IPath [] exclusion = new IPath [0 ];
106+ String mainSourceEncoding = null ;
107+ addSourceDirs (classpath , project , Arrays .asList (sourceEntry .getTokens ()), outputFolder .getFullPath (), inclusion ,
108+ exclusion , mainSourceEncoding , subMonitor .split (10 ), false );
109+ }
110+ }
111+
112+ @ Override
113+ protected IContainer getOutputLocation (ProjectConfigurationRequest request , IProject project )
114+ throws CoreException {
115+ IBuild build = getBuild (project );
116+ if (build == null ) {
117+ return super .getOutputLocation (request , project );
118+ }
119+ IBuildEntry outputEntry = build .getEntry (IBuildEntry .OUTPUT_PREFIX + "." );
120+ if (outputEntry != null && outputEntry .getTokens ().length > 0 ) {
121+ return getFolder (project , outputEntry .getTokens ()[0 ]);
122+ } else {
123+ return super .getOutputLocation (request , project );
124+ }
125+ }
126+
127+ @ Override
128+ protected void addJavaProjectOptions (Map <String , String > options , ProjectConfigurationRequest request ,
129+ IProgressMonitor monitor ) throws CoreException {
130+ IProject project = request .mavenProjectFacade ().getProject ();
131+ IPluginModelBase model = PluginRegistry .findModel (project );
132+ if (model != null ) {
133+ ClasspathComputer .setClasspath (project , model );
134+ } else {
135+ super .addJavaProjectOptions (options , request , monitor );
136+ }
137+ }
138+
139+ }
140+
141+ private static IBuild getBuild (IProject project ) throws CoreException {
142+ IFile buildFile = PDEProject .getBuildProperties (project );
143+ IBuildModel buildModel = null ;
144+ if (buildFile .exists ()) {
145+ buildModel = new WorkspaceBuildModel (buildFile );
146+ buildModel .load ();
147+ }
148+ return (buildModel != null ) ? buildModel .getBuild () : null ;
149+ }
39150}
0 commit comments