2020import java .io .InputStream ;
2121import java .util .Collection ;
2222import java .util .Collections ;
23- import java .util .HashMap ;
2423import java .util .HashSet ;
25- import java .util .Map ;
2624import java .util .Objects ;
2725import java .util .Optional ;
2826
7169import org .apache .maven .artifact .Artifact ;
7270import org .apache .maven .model .DependencyManagement ;
7371import org .apache .maven .model .Model ;
74- import org .apache .maven .model .building .FileModelSource ;
75- import org .apache .maven .model .building .ModelProcessor ;
7672import org .apache .maven .project .MavenProject ;
73+ import org .apache .maven .project .ProjectBuilder ;
74+ import org .apache .maven .project .ProjectBuildingException ;
75+ import org .apache .maven .project .ProjectBuildingResult ;
7776
7877import org .eclipse .m2e .core .internal .IMavenToolbox ;
7978import org .eclipse .m2e .core .internal .MavenPluginActivator ;
@@ -106,13 +105,26 @@ public org.apache.maven.model.Model readMavenModel(InputStream reader) throws Co
106105 }
107106
108107 /**
109- * Read the model from the provided pom file (might not exits) pointer
108+ * Shortcut for {@link #readMavenModel(File, IProgressMonitor) with 2nd argument being {@code null}.
110109 *
111- * @param pomFile the file pointer
112- * @return a maven model, or <code>null</code> if the pointer do not point to any valid maven directory
113- * @throws CoreException if the file points to a valid maven directory but the pom could not be read
110+ * @deprecated Use {@link #readMavenModel(File, IProgressMonitor)} instead.
114111 */
112+ @ Deprecated
115113 public org .apache .maven .model .Model readMavenModel (File pomFile ) throws CoreException {
114+ return readMavenModel (pomFile , null );
115+ }
116+
117+ /**
118+ * Read the (effective) model from the provided POM file
119+ *
120+ * @param pomFile either the path to the directory containing a pom.xml or the path to the pom.xml itself
121+ * @param progressMonitor the progress monitor, may be {@code null}
122+ * @return the effective maven model, or <code>null</code> if the pomFile does not point to an existing pom.file or
123+ * container directory
124+ * @throws CoreException if the file points to a valid maven directory but the pom could not be read
125+ */
126+ public org .apache .maven .model .Model readMavenModel (File pomFile , IProgressMonitor progressMonitor )
127+ throws CoreException {
116128 File baseDir = pomFile .isDirectory () ? pomFile : pomFile .getParentFile ();
117129 Objects .requireNonNull (baseDir , "not a directory and not a parent, invalid file?" );
118130 IComponentLookup lookup = containerManager .getComponentLookup (baseDir );
@@ -121,17 +133,20 @@ public org.apache.maven.model.Model readMavenModel(File pomFile) throws CoreExce
121133 if (locatePom .isEmpty ()) {
122134 return null ;
123135 }
124- ModelProcessor modelProcessor = lookup .lookup (ModelProcessor .class );
125136
126137 File pom = locatePom .get ();
127- Model model ;
128- try {
129- model = modelProcessor .read (pom , new HashMap <>(Map .of (ModelProcessor .SOURCE , new FileModelSource (pom ))));
130- } catch (IOException ex ) {
131- throw new CoreException (Status .error (ex .getMessage (), ex ));
132- }
133- model .setPomFile (pom );
134- return model ;
138+ IMavenExecutionContext context = maven .createExecutionContext ();
139+ return context .execute ((IMavenExecutionContext innerContext , IProgressMonitor monitor ) -> {
140+ ProjectBuilder projectBuilder = lookup .lookup (ProjectBuilder .class );
141+
142+ try {
143+ ProjectBuildingResult result = projectBuilder .build (pom , innerContext .getExecutionRequest ()
144+ .getProjectBuildingRequest ().setRepositorySession (innerContext .getRepositorySession ()));
145+ return result .getProject ().getModel ();
146+ } catch (ProjectBuildingException ex ) {
147+ throw new CoreException (Status .error (ex .getMessage (), ex ));
148+ }
149+ }, progressMonitor );
135150 }
136151
137152 public org .apache .maven .model .Model readMavenModel (IFile pomFile ) throws CoreException {
0 commit comments