You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by gn...@apache.org on 2022/02/24 16:02:26 UTC

[maven-install-plugin] branch mvn4 updated: Switch a few core plugins to the new api

This is an automated email from the ASF dual-hosted git repository.

gnodet pushed a commit to branch mvn4
in repository https://gitbox.apache.org/repos/asf/maven-install-plugin.git


The following commit(s) were added to refs/heads/mvn4 by this push:
     new 13ae945  Switch a few core plugins to the new api
13ae945 is described below

commit 13ae9451083b3f3419942dc9f5e9cfbfedef3f6d
Author: Guillaume Nodet <gn...@gmail.com>
AuthorDate: Thu Feb 24 17:00:56 2022 +0100

    Switch a few core plugins to the new api
---
 pom.xml                                            |  26 +-
 .../maven/plugins/install/AbstractInstallMojo.java |  37 ++-
 .../maven/plugins/install/InstallFileMojo.java     | 337 +++++++--------------
 .../apache/maven/plugins/install/InstallMojo.java  |  60 ++--
 .../maven/plugins/install/InstallMojoTest.java     |  31 +-
 5 files changed, 176 insertions(+), 315 deletions(-)

diff --git a/pom.xml b/pom.xml
index cc34616..7d53b80 100644
--- a/pom.xml
+++ b/pom.xml
@@ -74,34 +74,14 @@
   <dependencies>
     <dependency>
       <groupId>org.apache.maven</groupId>
-      <artifactId>maven-plugin-api</artifactId>
-      <version>${mavenVersion}</version>
-      <scope>provided</scope>
-    </dependency>
-    <dependency>
-      <groupId>org.apache.maven</groupId>
-      <artifactId>maven-artifact</artifactId>
-      <version>${mavenVersion}</version>
-      <scope>provided</scope>
-    </dependency>
-    <!-- This is here to override 3.0 coming with maven-artifact-transfer -->
-    <dependency>
-      <groupId>org.apache.maven</groupId>
-      <artifactId>maven-core</artifactId>
+      <artifactId>maven-core-api</artifactId>
       <version>${mavenVersion}</version>
       <scope>provided</scope>
     </dependency>
     <dependency>
       <groupId>org.apache.maven.shared</groupId>
-      <artifactId>maven-artifact-transfer</artifactId>
-      <version>2.0.0-SNAPSHOT</version>
-    </dependency>
-
-    <!-- dependencies to annotations -->
-    <dependency>
-      <groupId>org.apache.maven.plugin-tools</groupId>
-      <artifactId>maven-plugin-annotations</artifactId>
-      <scope>provided</scope>
+      <artifactId>maven-shared-utils</artifactId>
+      <version>4.0.0-SNAPSHOT</version>
     </dependency>
 
     <dependency>
diff --git a/src/main/java/org/apache/maven/plugins/install/AbstractInstallMojo.java b/src/main/java/org/apache/maven/plugins/install/AbstractInstallMojo.java
index 96d0f53..fdec6ff 100644
--- a/src/main/java/org/apache/maven/plugins/install/AbstractInstallMojo.java
+++ b/src/main/java/org/apache/maven/plugins/install/AbstractInstallMojo.java
@@ -21,14 +21,15 @@ package org.apache.maven.plugins.install;
 
 import java.io.File;
 
-import org.apache.maven.artifact.Artifact;
-import org.apache.maven.execution.MavenSession;
-import org.apache.maven.plugin.AbstractMojo;
-import org.apache.maven.plugins.annotations.Component;
-import org.apache.maven.plugins.annotations.Parameter;
-import org.apache.maven.project.ProjectBuildingRequest;
-import org.apache.maven.project.artifact.ProjectArtifactMetadata;
-import org.apache.maven.shared.transfer.repository.RepositoryManager;
+import org.apache.maven.api.Artifact;
+import org.apache.maven.api.Metadata;
+import org.apache.maven.api.Session;
+import org.apache.maven.api.plugin.annotations.Component;
+import org.apache.maven.api.plugin.annotations.Mojo;
+import org.apache.maven.api.plugin.annotations.Parameter;
+import org.apache.maven.api.services.LocalRepositoryManager;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * Common fields for installation mojos.
@@ -36,41 +37,39 @@ import org.apache.maven.shared.transfer.repository.RepositoryManager;
  * @author <a href="mailto:brett@apache.org">Brett Porter</a>
  */
 public abstract class AbstractInstallMojo
-    extends AbstractMojo
+        implements org.apache.maven.api.plugin.Mojo
 {
 
+    protected Logger logger = LoggerFactory.getLogger( getClass() );
+
     @Component
-    protected RepositoryManager repositoryManager;
+    protected LocalRepositoryManager repositoryManager;
 
     @Parameter( defaultValue = "${session}", required = true, readonly = true )
-    protected MavenSession session;
+    protected Session session;
 
     /**
      * Gets the path of the specified artifact within the local repository. Note that the returned path need not exist
      * (yet).
      *
-     * @param buildingRequest {@link ProjectBuildingRequest}.
      * @param artifact The artifact whose local repo path should be determined, must not be <code>null</code>.
      * @return The absolute path to the artifact when installed, never <code>null</code>.
      */
-    protected File getLocalRepoFile( ProjectBuildingRequest buildingRequest, Artifact artifact )
+    protected File getLocalRepoFile( Artifact artifact )
     {
-        String path = repositoryManager.getPathForLocalArtifact( buildingRequest, artifact );
-        return new File( repositoryManager.getLocalRepositoryBasedir( buildingRequest ), path );
+        return session.getPathForLocalArtifact( artifact ).toFile();
     }
 
     /**
      * Gets the path of the specified artifact metadata within the local repository. Note that the returned path need
      * not exist (yet).
      *
-     * @param buildingRequest {@link ProjectBuildingRequest}.
      * @param metadata The artifact metadata whose local repo path should be determined, must not be <code>null</code>.
      * @return The absolute path to the artifact metadata when installed, never <code>null</code>.
      */
-    protected File getLocalRepoFile( ProjectBuildingRequest buildingRequest, ProjectArtifactMetadata metadata )
+    protected File getLocalRepoFile( Metadata metadata )
     {
-        String path = repositoryManager.getPathForLocalMetadata( buildingRequest, metadata );
-        return new File( repositoryManager.getLocalRepositoryBasedir( buildingRequest ), path );
+        return session.getPathForLocalMetadata( metadata ).toFile();
     }
 
 }
diff --git a/src/main/java/org/apache/maven/plugins/install/InstallFileMojo.java b/src/main/java/org/apache/maven/plugins/install/InstallFileMojo.java
index 32865b2..1a029f2 100644
--- a/src/main/java/org/apache/maven/plugins/install/InstallFileMojo.java
+++ b/src/main/java/org/apache/maven/plugins/install/InstallFileMojo.java
@@ -21,45 +21,34 @@ package org.apache.maven.plugins.install;
 
 import java.io.File;
 import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
-import java.io.OutputStream;
 import java.io.Reader;
 import java.io.Writer;
-import java.util.Enumeration;
+import java.nio.file.Files;
+import java.util.ArrayList;
+import java.util.List;
 import java.util.jar.JarEntry;
 import java.util.jar.JarFile;
 import java.util.regex.Pattern;
 
-import org.apache.maven.artifact.Artifact;
-import org.apache.maven.artifact.handler.DefaultArtifactHandler;
+import org.apache.maven.api.Artifact;
+import org.apache.maven.api.Session;
+import org.apache.maven.api.plugin.MojoException;
+import org.apache.maven.api.plugin.annotations.Component;
+import org.apache.maven.api.plugin.annotations.Mojo;
+import org.apache.maven.api.plugin.annotations.Parameter;
+import org.apache.maven.api.services.ArtifactFactory;
+import org.apache.maven.api.services.ArtifactFactoryRequest;
+import org.apache.maven.api.services.ArtifactInstaller;
+import org.apache.maven.api.services.ArtifactManager;
 import org.apache.maven.model.Model;
 import org.apache.maven.model.Parent;
-import org.apache.maven.model.building.ModelBuildingException;
-import org.apache.maven.model.building.ModelSource;
-import org.apache.maven.model.building.StringModelSource;
 import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
 import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
-import org.apache.maven.plugin.MojoExecutionException;
-import org.apache.maven.plugin.MojoFailureException;
-import org.apache.maven.plugins.annotations.Component;
-import org.apache.maven.plugins.annotations.Mojo;
-import org.apache.maven.plugins.annotations.Parameter;
-import org.apache.maven.project.DefaultProjectBuildingRequest;
-import org.apache.maven.project.MavenProject;
-import org.apache.maven.project.MavenProjectHelper;
-import org.apache.maven.project.ProjectBuilder;
-import org.apache.maven.project.ProjectBuildingException;
-import org.apache.maven.project.ProjectBuildingRequest;
-import org.apache.maven.project.artifact.ProjectArtifactMetadata;
-import org.apache.maven.shared.transfer.project.install.ProjectInstaller;
-import org.apache.maven.shared.transfer.project.install.ProjectInstallerRequest;
-import org.apache.maven.shared.utils.Os;
-import org.apache.maven.shared.utils.ReaderFactory;
 import org.apache.maven.shared.utils.WriterFactory;
-import org.apache.maven.shared.utils.io.IOUtil;
 import org.codehaus.plexus.util.FileUtils;
+import org.codehaus.plexus.util.xml.XmlStreamReader;
 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
 
 /**
@@ -67,7 +56,7 @@ import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
  * 
  * @author <a href="mailto:brett@apache.org">Brett Porter</a>
  */
-@Mojo( name = "install-file", requiresProject = false, aggregator = true, threadSafe = true )
+@Mojo( name = "install-file", requiresProject = false, aggregator = true )
 public class InstallFileMojo
     extends AbstractInstallMojo
 {
@@ -159,47 +148,40 @@ public class InstallFileMojo
     private File localRepositoryPath;
 
     /**
-     * Used for attaching the artifacts to install to the project.
+     * Used to install the project created.
      */
     @Component
-    private MavenProjectHelper projectHelper;
+    private ArtifactInstaller installer;
 
-    /**
-     * Used for creating the project to which the artifacts to install will be attached.
-     */
     @Component
-    private ProjectBuilder projectBuilder;
+    private ArtifactManager artifactManager;
 
     /**
-     * Used to install the project created.
-     */
-    @Component
-    private ProjectInstaller installer;
-
-    /**
-     * @see org.apache.maven.plugin.Mojo#execute()
+     * @see org.apache.maven.api.plugin.Mojo#execute()
      */
     public void execute()
-        throws MojoExecutionException, MojoFailureException
+        throws MojoException
     {
 
         if ( !file.exists() )
         {
             String message = "The specified file '" + file.getPath() + "' does not exist";
-            getLog().error( message );
-            throw new MojoFailureException( message );
+            logger.error( message );
+            throw new MojoException( message );
         }
 
-        ProjectBuildingRequest buildingRequest = session.getProjectBuildingRequest();
+        Session session = this.session;
+
+        List<Artifact> installableArtifacts = new ArrayList<>();
 
         // ----------------------------------------------------------------------
         // Override the default localRepository variable
         // ----------------------------------------------------------------------
         if ( localRepositoryPath != null )
         {
-            buildingRequest = repositoryManager.setLocalRepositoryBasedir( buildingRequest, localRepositoryPath );
+            session = session.withLocalRepository( session.createLocalRepository( localRepositoryPath.toPath() ) );
 
-            getLog().debug( "localRepoPath: " + repositoryManager.getLocalRepositoryBasedir( buildingRequest ) );
+            logger.debug( "localRepoPath: {}", localRepositoryPath );
         }
 
         File temporaryPom = null;
@@ -214,95 +196,99 @@ public class InstallFileMojo
             pomFile = temporaryPom;
         }
 
-        MavenProject project = createMavenProject();
-        
-        // We need to set a new ArtifactHandler otherwise 
+        // We need to set a new ArtifactHandler otherwise
         // the extension will be set to the packaging type
         // which is sometimes wrong.
-        DefaultArtifactHandler ah = new DefaultArtifactHandler( packaging );
-        ah.setExtension( FileUtils.getExtension( file.getName() ) );
-
-        project.getArtifact().setArtifactHandler( ah );
-        Artifact artifact = project.getArtifact();
-
-        if ( file.equals( getLocalRepoFile( buildingRequest, artifact ) ) )
+        Artifact artifact = session.getService( ArtifactFactory.class )
+                .create( ArtifactFactoryRequest.builder()
+                        .session( session )
+                        .groupId( groupId )
+                        .artifactId( artifactId )
+                        .classifier( classifier )
+                        .version( version )
+                        .extension( FileUtils.getExtension( file.getName() ) )
+                        .type( packaging )
+                        .build() );
+
+        if ( file.equals( getLocalRepoFile( artifact ) ) )
         {
-            throw new MojoFailureException( "Cannot install artifact. "
+            throw new MojoException( "Cannot install artifact. "
                 + "Artifact is already in the local repository.\n\nFile in question is: " + file + "\n" );
         }
 
-        if ( classifier == null )
-        {
-            artifact.setFile( file );
-            if ( "pom".equals( packaging ) )
-            {
-                project.setFile( file );
-            }
-        }
-        else
-        {
-            projectHelper.attachArtifact( project, packaging, classifier, file );
-        }
+        artifactManager.setPath( artifact, file.toPath() );
+        installableArtifacts.add( artifact );
 
         if ( !"pom".equals( packaging ) )
         {
+            Artifact pomArtifact = session.getService( ArtifactFactory.class )
+                    .create( ArtifactFactoryRequest.builder()
+                            .session( session )
+                            .groupId( groupId )
+                            .artifactId( artifactId )
+                            .classifier( classifier )
+                            .version( version )
+                            .type( "pom" )
+                            .build() );
             if ( pomFile != null )
             {
-                if ( classifier == null )
-                {
-                    artifact.addMetadata( new ProjectArtifactMetadata( artifact, pomFile ) );
-                }
-                else
-                {
-                    project.setFile( pomFile );
-                }
+                artifactManager.setPath( pomArtifact, pomFile.toPath() );
+                installableArtifacts.add( artifact );
             }
             else
             {
                 temporaryPom = generatePomFile();
-                ProjectArtifactMetadata pomMetadata = new ProjectArtifactMetadata( artifact, temporaryPom );
+                artifactManager.setPath( pomArtifact, temporaryPom.toPath() );
                 if ( Boolean.TRUE.equals( generatePom )
-                    || ( generatePom == null && !getLocalRepoFile( buildingRequest, pomMetadata ).exists() ) )
+                    || ( generatePom == null && !getLocalRepoFile( pomArtifact ).exists() ) )
                 {
-                    getLog().debug( "Installing generated POM" );
-                    if ( classifier == null )
-                    {
-                        artifact.addMetadata( pomMetadata );
-                    }
-                    else
-                    {
-                        project.setFile( temporaryPom );
-                    }
+                    logger.debug( "Installing generated POM" );
+                    installableArtifacts.add( artifact );
                 }
                 else if ( generatePom == null )
                 {
-                    getLog().debug( "Skipping installation of generated POM, already present in local repository" );
+                    logger.debug( "Skipping installation of generated POM, already present in local repository" );
                 }
             }
         }
 
         if ( sources != null )
         {
-            projectHelper.attachArtifact( project, "jar", "sources", sources );
+            Artifact sourcesArtifact = session.getService( ArtifactFactory.class )
+                    .create( ArtifactFactoryRequest.builder()
+                            .session( session )
+                            .groupId( groupId )
+                            .artifactId( artifactId )
+                            .classifier( "sources" )
+                            .version( version )
+                            .type( "jar" )
+                            .build() );
+            artifactManager.setPath( sourcesArtifact, sources.toPath() );
+            installableArtifacts.add( artifact );
         }
 
         if ( javadoc != null )
         {
-            projectHelper.attachArtifact( project, "jar", "javadoc", javadoc );
+            Artifact sourcesArtifact = session.getService( ArtifactFactory.class )
+                    .create( ArtifactFactoryRequest.builder()
+                            .session( session )
+                            .groupId( groupId )
+                            .artifactId( artifactId )
+                            .classifier( "javadoc" )
+                            .version( version )
+                            .type( "jar" )
+                            .build() );
+            artifactManager.setPath( sourcesArtifact, javadoc.toPath() );
+            installableArtifacts.add( artifact );
         }
 
         try
         {
-            // CHECKSTYLE_OFF: LineLength
-            ProjectInstallerRequest projectInstallerRequest =
-                new ProjectInstallerRequest().setProject( project );
-            // CHECKSTYLE_ON: LineLength
-
-            installer.install( buildingRequest, projectInstallerRequest );
+            installer.install( session, installableArtifacts );
         }
         catch ( Exception e )
         {
-            throw new MojoExecutionException( e.getMessage(), e );
+            throw new MojoException( e.getMessage(), e );
         }
         finally
         {
@@ -314,124 +300,49 @@ public class InstallFileMojo
         }
     }
 
-    /**
-     * Creates a Maven project in-memory from the user-supplied groupId, artifactId and version. When a classifier is
-     * supplied, the packaging must be POM because the project with only have attachments. This project serves as basis
-     * to attach the artifacts to install to.
-     * 
-     * @return The created Maven project, never <code>null</code>.
-     * @throws MojoExecutionException When the model of the project could not be built.
-     * @throws MojoFailureException When building the project failed.
-     */
-    private MavenProject createMavenProject()
-        throws MojoExecutionException, MojoFailureException
-    {
-        if ( groupId == null || artifactId == null || version == null || packaging == null )
-        {
-            throw new MojoExecutionException( "The artifact information is incomplete: 'groupId', 'artifactId', "
-                + "'version' and 'packaging' are required." );
-        }
-        ModelSource modelSource = new StringModelSource( "<project><modelVersion>4.0.0</modelVersion><groupId>"
-            + groupId + "</groupId><artifactId>" + artifactId + "</artifactId><version>" + version
-            + "</version><packaging>" + ( classifier == null ? packaging : "pom" ) + "</packaging></project>" );
-        ProjectBuildingRequest pbr = new DefaultProjectBuildingRequest( session.getProjectBuildingRequest() );
-        pbr.setProcessPlugins( false );
-        try
-        {
-            return projectBuilder.build( modelSource, pbr ).getProject();
-        }
-        catch ( ProjectBuildingException e )
-        {
-            if ( e.getCause() instanceof ModelBuildingException )
-            {
-                throw new MojoExecutionException( "The artifact information is not valid:" + Os.LINE_SEP
-                    + e.getCause().getMessage() );
-            }
-            throw new MojoFailureException( "Unable to create the project.", e );
-        }
-    }
 
     private File readingPomFromJarFile()
-        throws MojoExecutionException
+        throws MojoException
     {
+
         File pomFile = null;
 
-        JarFile jarFile = null;
-        try
+        try ( JarFile jarFile = new JarFile( file ) )
         {
             Pattern pomEntry = Pattern.compile( "META-INF/maven/.*/pom\\.xml" );
 
-            jarFile = new JarFile( file );
+            JarEntry entry = jarFile.stream()
+                    .filter( e -> pomEntry.matcher( e.getName() ).matches() )
+                    .findFirst().orElse( null );
 
-            Enumeration<JarEntry> jarEntries = jarFile.entries();
-
-            while ( jarEntries.hasMoreElements() )
+            if ( entry != null )
             {
-                JarEntry entry = jarEntries.nextElement();
+                logger.debug( "Using " + entry.getName() + " as pomFile" );
 
-                if ( pomEntry.matcher( entry.getName() ).matches() )
+                String base = file.getName();
+                if ( base.indexOf( '.' ) > 0 )
                 {
-                    getLog().debug( "Using " + entry.getName() + " as pomFile" );
-
-                    InputStream pomInputStream = null;
-                    OutputStream pomOutputStream = null;
-
-                    try
-                    {
-                        pomInputStream = jarFile.getInputStream( entry );
-
-                        String base = file.getName();
-                        if ( base.indexOf( '.' ) > 0 )
-                        {
-                            base = base.substring( 0, base.lastIndexOf( '.' ) );
-                        }
-                        pomFile = File.createTempFile( base, ".pom" );
-
-                        pomOutputStream = new FileOutputStream( pomFile );
-
-                        IOUtil.copy( pomInputStream, pomOutputStream );
-
-                        pomOutputStream.close();
-                        pomOutputStream = null;
-
-                        pomInputStream.close();
-                        pomInputStream = null;
-
-                        processModel( readModel( pomFile ) );
+                    base = base.substring( 0, base.lastIndexOf( '.' ) );
+                }
+                pomFile = File.createTempFile( base, ".pom" );
 
-                        break;
-                    }
-                    finally
-                    {
-                        IOUtil.close( pomInputStream );
-                        IOUtil.close( pomOutputStream );
-                    }
+                try ( InputStream pomInputStream = jarFile.getInputStream( entry ) )
+                {
+                    Files.copy( pomInputStream, pomFile.toPath() );
                 }
+
+                processModel( readModel( pomFile ) );
             }
 
             if ( pomFile == null )
             {
-                getLog().info( "pom.xml not found in " + file.getName() );
+                logger.info( "pom.xml not found in " + file.getName() );
             }
         }
         catch ( IOException e )
         {
             // ignore, artifact not packaged by Maven
         }
-        finally
-        {
-            if ( jarFile != null )
-            {
-                try
-                {
-                    jarFile.close();
-                }
-                catch ( IOException e )
-                {
-                    // we did our best
-                }
-            }
-        }
         return pomFile;
     }
 
@@ -440,35 +351,27 @@ public class InstallFileMojo
      * 
      * @param pomFile The path of the POM file to parse, must not be <code>null</code>.
      * @return The model from the POM file, never <code>null</code>.
-     * @throws MojoExecutionException If the POM could not be parsed.
+     * @throws MojoException If the POM could not be parsed.
      */
     private Model readModel( File pomFile )
-        throws MojoExecutionException
+        throws MojoException
     {
-        Reader reader = null;
-        try
+        try ( Reader reader = new XmlStreamReader( pomFile ) )
         {
-            reader = ReaderFactory.newXmlReader( pomFile );
             final Model model = new MavenXpp3Reader().read( reader );
-            reader.close();
-            reader = null;
             return model;
         }
         catch ( FileNotFoundException e )
         {
-            throw new MojoExecutionException( "File not found " + pomFile, e );
+            throw new MojoException( "File not found " + pomFile, e );
         }
         catch ( IOException e )
         {
-            throw new MojoExecutionException( "Error reading POM " + pomFile, e );
+            throw new MojoException( "Error reading POM " + pomFile, e );
         }
         catch ( XmlPullParserException e )
         {
-            throw new MojoExecutionException( "Error parsing POM " + pomFile, e );
-        }
-        finally
-        {
-            IOUtil.close( reader );
+            throw new MojoException( "Error parsing POM " + pomFile, e );
         }
     }
 
@@ -533,32 +436,24 @@ public class InstallFileMojo
      * the generated file when no longer needed.
      * 
      * @return The path to the generated POM file, never <code>null</code>.
-     * @throws MojoExecutionException If the POM file could not be generated.
+     * @throws MojoException If the POM file could not be generated.
      */
     private File generatePomFile()
-        throws MojoExecutionException
+        throws MojoException
     {
-        Model model = generateModel();
-
-        Writer writer = null;
         try
         {
+            Model model = generateModel();
             File pomFile = File.createTempFile( "mvninstall", ".pom" );
-
-            writer = WriterFactory.newXmlWriter( pomFile );
-            new MavenXpp3Writer().write( writer, model );
-            writer.close();
-            writer = null;
-
+            try ( Writer writer = WriterFactory.newXmlWriter( pomFile ) )
+            {
+                new MavenXpp3Writer().write( writer, model );
+            }
             return pomFile;
         }
         catch ( IOException e )
         {
-            throw new MojoExecutionException( "Error writing temporary POM file: " + e.getMessage(), e );
-        }
-        finally
-        {
-            IOUtil.close( writer );
+            throw new MojoException( "Error writing temporary POM file: " + e.getMessage(), e );
         }
     }
 
diff --git a/src/main/java/org/apache/maven/plugins/install/InstallMojo.java b/src/main/java/org/apache/maven/plugins/install/InstallMojo.java
index b01c114..03ed848 100644
--- a/src/main/java/org/apache/maven/plugins/install/InstallMojo.java
+++ b/src/main/java/org/apache/maven/plugins/install/InstallMojo.java
@@ -19,24 +19,20 @@ package org.apache.maven.plugins.install;
  * under the License.
  */
 
-import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
 import java.util.concurrent.atomic.AtomicInteger;
 
-import org.apache.maven.plugin.MojoExecutionException;
-import org.apache.maven.plugin.MojoFailureException;
-import org.apache.maven.plugins.annotations.Component;
-import org.apache.maven.plugins.annotations.LifecyclePhase;
-import org.apache.maven.plugins.annotations.Mojo;
-import org.apache.maven.plugins.annotations.Parameter;
-import org.apache.maven.project.MavenProject;
-import org.apache.maven.project.ProjectBuildingRequest;
-import org.apache.maven.shared.transfer.artifact.install.ArtifactInstallerException;
-import org.apache.maven.shared.transfer.project.NoFileAssignedException;
-import org.apache.maven.shared.transfer.project.install.ProjectInstaller;
-import org.apache.maven.shared.transfer.project.install.ProjectInstallerRequest;
+import org.apache.maven.api.Project;
+import org.apache.maven.api.plugin.MojoException;
+import org.apache.maven.api.plugin.annotations.Component;
+import org.apache.maven.api.plugin.annotations.LifecyclePhase;
+import org.apache.maven.api.plugin.annotations.Mojo;
+import org.apache.maven.api.plugin.annotations.Parameter;
+import org.apache.maven.api.services.ProjectInstaller;
+import org.apache.maven.api.services.ProjectInstallerException;
+import org.apache.maven.api.services.ProjectInstallerRequest;
 
 /**
  * Installs the project's main artifact, and any other artifacts attached by other plugins in the lifecycle, to the
@@ -44,7 +40,7 @@ import org.apache.maven.shared.transfer.project.install.ProjectInstallerRequest;
  * 
  * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
  */
-@Mojo( name = "install", defaultPhase = LifecyclePhase.INSTALL, threadSafe = true )
+@Mojo( name = "install", defaultPhase = LifecyclePhase.INSTALL )
 public class InstallMojo
     extends AbstractInstallMojo
 {
@@ -61,10 +57,10 @@ public class InstallMojo
     /**
      */
     @Parameter( defaultValue = "${project}", readonly = true, required = true )
-    private MavenProject project;
+    private Project project;
 
     @Parameter( defaultValue = "${reactorProjects}", required = true, readonly = true )
-    private List<MavenProject> reactorProjects;
+    private List<Project> reactorProjects;
 
     /**
      * Whether every project should be installed during its own install-phase or at the end of the multimodule build. If
@@ -89,23 +85,24 @@ public class InstallMojo
     private ProjectInstaller installer;
 
     public void execute()
-        throws MojoExecutionException, MojoFailureException
+        throws MojoException
     {
         boolean addedInstallRequest = false;
         if ( skip )
         {
-            getLog().info( "Skipping artifact installation" );
+            logger.info( "Skipping artifact installation" );
         }
         else
         {
             // CHECKSTYLE_OFF: LineLength
             ProjectInstallerRequest projectInstallerRequest =
-                new ProjectInstallerRequest().setProject( project );
+                ProjectInstallerRequest.builder().project( project )
+                        .build();
             // CHECKSTYLE_ON: LineLength
 
             if ( !installAtEnd )
             {
-                installProject( session.getProjectBuildingRequest(), projectInstallerRequest );
+                installProject( projectInstallerRequest );
             }
             else
             {
@@ -121,37 +118,28 @@ public class InstallMojo
             {
                 while ( !INSTALLREQUESTS.isEmpty() )
                 {
-                    installProject( session.getProjectBuildingRequest(), INSTALLREQUESTS.remove( 0 ) );
+                    installProject( INSTALLREQUESTS.remove( 0 ) );
                 }
             }
         }
         else if ( addedInstallRequest )
         {
-            getLog().info( "Installing " + project.getGroupId() + ":" + project.getArtifactId() + ":"
+            logger.info( "Installing " + project.getGroupId() + ":" + project.getArtifactId() + ":"
                 + project.getVersion() + " at end" );
         }
     }
 
-    private void installProject( ProjectBuildingRequest pbr, ProjectInstallerRequest pir )
-        throws MojoFailureException, MojoExecutionException
+    private void installProject( ProjectInstallerRequest pir )
+        throws MojoException
     {
         try
         {
-            installer.install( session.getProjectBuildingRequest(), pir );
+            installer.install( pir );
         }
-        catch ( IOException e )
+        catch ( ProjectInstallerException e )
         {
-            throw new MojoFailureException( "IOException", e );
+            throw new MojoException( "ProjectInstallerException", e );
         }
-        catch ( ArtifactInstallerException e )
-        {
-            throw new MojoExecutionException( "ArtifactInstallerException", e );
-        }
-        catch ( NoFileAssignedException e )
-        {
-            throw new MojoExecutionException( "NoFileAssignedException", e );
-        }
-
     }
 
     public void setSkip( boolean skip )
diff --git a/src/test/java/org/apache/maven/plugins/install/InstallMojoTest.java b/src/test/java/org/apache/maven/plugins/install/InstallMojoTest.java
index ed73d2c..4be8436 100644
--- a/src/test/java/org/apache/maven/plugins/install/InstallMojoTest.java
+++ b/src/test/java/org/apache/maven/plugins/install/InstallMojoTest.java
@@ -26,17 +26,16 @@ import java.io.File;
 import java.util.Collections;
 import java.util.List;
 
+import org.apache.maven.api.Project;
+import org.apache.maven.api.Session;
+import org.apache.maven.api.plugin.MojoException;
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.artifact.metadata.ArtifactMetadata;
-import org.apache.maven.execution.MavenSession;
-import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.testing.AbstractMojoTestCase;
 import org.apache.maven.plugins.install.stubs.AttachedArtifactStub0;
 import org.apache.maven.plugins.install.stubs.InstallArtifactStub;
 import org.apache.maven.project.DefaultProjectBuildingRequest;
-import org.apache.maven.project.MavenProject;
 import org.apache.maven.project.ProjectBuildingRequest;
-import org.apache.maven.shared.transfer.repository.RepositoryManager;
 import org.apache.maven.shared.utils.io.FileUtils;
 import org.eclipse.aether.DefaultRepositorySystemSession;
 import org.eclipse.aether.internal.impl.EnhancedLocalRepositoryManagerFactory;
@@ -85,7 +84,7 @@ public class InstallMojoTest
         File file = new File( getBasedir(), "target/test-classes/unit/basic-install-test/target/"
             + "maven-install-test-1.0-SNAPSHOT.jar" );
 
-        MavenProject project = (MavenProject) getVariableValueFromObject( mojo, "project" );
+        Project project = (Project) getVariableValueFromObject( mojo, "project" );
         updateMavenProject( project );
 
         setVariableValueToObject( mojo, "reactorProjects", Collections.singletonList( project ) );
@@ -117,7 +116,7 @@ public class InstallMojoTest
 
         assertNotNull( mojo );
 
-        MavenProject project = (MavenProject) getVariableValueFromObject( mojo, "project" );
+        Project project = (Project) getVariableValueFromObject( mojo, "project" );
         updateMavenProject( project );
 
         setVariableValueToObject( mojo, "reactorProjects", Collections.singletonList( project ) );
@@ -159,7 +158,7 @@ public class InstallMojoTest
         File file = new File( getBasedir(), "target/test-classes/unit/configured-install-test/target/"
             + "maven-install-test-1.0-SNAPSHOT.jar" );
 
-        MavenProject project = (MavenProject) getVariableValueFromObject( mojo, "project" );
+        Project project = (Project) getVariableValueFromObject( mojo, "project" );
         updateMavenProject( project );
 
         setVariableValueToObject( mojo, "reactorProjects", Collections.singletonList( project ) );
@@ -185,7 +184,7 @@ public class InstallMojoTest
 
         assertNotNull( mojo );
 
-        MavenProject project = (MavenProject) getVariableValueFromObject( mojo, "project" );
+        Project project = (Project) getVariableValueFromObject( mojo, "project" );
         updateMavenProject( project );
 
         setVariableValueToObject( mojo, "reactorProjects", Collections.singletonList( project ) );
@@ -203,7 +202,7 @@ public class InstallMojoTest
 
             fail( "Did not throw mojo execution exception" );
         }
-        catch ( MojoExecutionException e )
+        catch ( MojoException e )
         {
             //expected
         }
@@ -221,7 +220,7 @@ public class InstallMojoTest
 
         assertNotNull( mojo );
 
-        MavenProject project = (MavenProject) getVariableValueFromObject( mojo, "project" );
+        Project project = (Project) getVariableValueFromObject( mojo, "project" );
         updateMavenProject( project );
 
         setVariableValueToObject( mojo, "reactorProjects", Collections.singletonList( project ) );
@@ -256,8 +255,8 @@ public class InstallMojoTest
 
         File file = new File( getBasedir(), "target/test-classes/unit/basic-install-checksum/" + "maven-test-jar.jar" );
 
-        MavenProject project = (MavenProject) getVariableValueFromObject( mojo, "project" );
-        MavenSession mavenSession = createMavenSession();
+        Project project = (Project) getVariableValueFromObject( mojo, "project" );
+        Session mavenSession = createMavenSession();
         updateMavenProject( project );
 
         setVariableValueToObject( mojo, "reactorProjects", Collections.singletonList( project ) );
@@ -313,7 +312,7 @@ public class InstallMojoTest
         File file = new File( getBasedir(), "target/test-classes/unit/basic-install-test/target/"
             + "maven-install-test-1.0-SNAPSHOT.jar" );
 
-        MavenProject project = (MavenProject) getVariableValueFromObject( mojo, "project" );
+        Project project = (Project) getVariableValueFromObject( mojo, "project" );
         updateMavenProject( project );
 
         setVariableValueToObject( mojo, "reactorProjects", Collections.singletonList( project ) );
@@ -345,9 +344,9 @@ public class InstallMojoTest
         return parameter.replace( '.', '/' );
     }
     
-    private MavenSession createMavenSession() throws NoLocalRepositoryManagerException
+    private Session createMavenSession() throws NoLocalRepositoryManagerException
     {
-        MavenSession session = mock( MavenSession.class );
+        Session session = mock( Session.class );
         DefaultRepositorySystemSession repositorySession  = new DefaultRepositorySystemSession();
         repositorySession.setLocalRepositoryManager(
                 new EnhancedLocalRepositoryManagerFactory().newInstance(
@@ -360,7 +359,7 @@ public class InstallMojoTest
         return session;
     }
     
-    private void updateMavenProject( MavenProject project )
+    private void updateMavenProject( Project project )
     {
        project.setGroupId( project.getArtifact().getGroupId() );
        project.setArtifactId( project.getArtifact().getArtifactId() );