View Javadoc
1   package org.apache.maven.internal.impl;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import javax.inject.Inject;
23  import javax.inject.Named;
24  import javax.inject.Singleton;
25  
26  import org.apache.maven.api.annotations.Nonnull;
27  import org.apache.maven.api.services.ArtifactInstaller;
28  import org.apache.maven.api.services.ArtifactInstallerException;
29  import org.apache.maven.api.services.ArtifactInstallerRequest;
30  import org.eclipse.aether.RepositorySystem;
31  import org.eclipse.aether.installation.InstallRequest;
32  import org.eclipse.aether.installation.InstallResult;
33  import org.eclipse.aether.installation.InstallationException;
34  
35  import static org.apache.maven.internal.impl.Utils.cast;
36  import static org.apache.maven.internal.impl.Utils.nonNull;
37  
38  @Named
39  @Singleton
40  public class DefaultArtifactInstaller implements ArtifactInstaller
41  {
42  
43      private final RepositorySystem repositorySystem;
44  
45      @Inject
46      DefaultArtifactInstaller( @Nonnull RepositorySystem repositorySystem )
47      {
48          this.repositorySystem = nonNull( repositorySystem );
49      }
50  
51      @Override
52      public void install( ArtifactInstallerRequest request ) throws ArtifactInstallerException, IllegalArgumentException
53      {
54          nonNull( request, "request can not be null" );
55          DefaultSession session = cast( DefaultSession.class, request.getSession(),
56                  "request.session should be a " + DefaultSession.class );
57          try
58          {
59              InstallRequest installRequest = new InstallRequest()
60                      .setArtifacts( session.toArtifacts( request.getArtifacts() ) );
61  
62              InstallResult result = repositorySystem.install( session.getSession(), installRequest );
63          }
64          catch ( InstallationException e )
65          {
66              throw new ArtifactInstallerException( e.getMessage(), e );
67          }
68      }
69  }