View Javadoc
1   package org.apache.maven.resolver.examples.guice;
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 java.util.Collections;
23  import java.util.HashSet;
24  import java.util.Set;
25  
26  import javax.inject.Named;
27  import javax.inject.Singleton;
28  
29  import org.apache.maven.model.building.DefaultModelBuilderFactory;
30  import org.apache.maven.model.building.ModelBuilder;
31  import org.apache.maven.repository.internal.DefaultArtifactDescriptorReader;
32  import org.apache.maven.repository.internal.DefaultVersionRangeResolver;
33  import org.apache.maven.repository.internal.DefaultVersionResolver;
34  import org.apache.maven.repository.internal.SnapshotMetadataGeneratorFactory;
35  import org.apache.maven.repository.internal.VersionsMetadataGeneratorFactory;
36  import org.eclipse.aether.connector.basic.BasicRepositoryConnectorFactory;
37  import org.eclipse.aether.impl.ArtifactDescriptorReader;
38  import org.eclipse.aether.impl.MetadataGeneratorFactory;
39  import org.eclipse.aether.impl.VersionRangeResolver;
40  import org.eclipse.aether.impl.VersionResolver;
41  import org.eclipse.aether.impl.guice.AetherModule;
42  import org.eclipse.aether.spi.connector.RepositoryConnectorFactory;
43  import org.eclipse.aether.spi.connector.transport.TransporterFactory;
44  import org.eclipse.aether.transport.file.FileTransporterFactory;
45  import org.eclipse.aether.transport.http.HttpTransporterFactory;
46  
47  import com.google.inject.AbstractModule;
48  import com.google.inject.Provides;
49  import com.google.inject.name.Names;
50  
51  /**
52   * Guice module for Demo Resolver snippets.
53   *
54   * Here, we assemble "complete" module by using {@link AetherModule} (see it's Javadoc) and adding bits from
55   * Maven itself (binding those components that completes repository system).
56   */
57  class DemoResolverModule
58      extends AbstractModule
59  {
60  
61      @Override
62      protected void configure()
63      {
64          // NOTE: see org.eclipse.aether.impl.guice.AetherModule Javadoc:
65          // AetherModule alone is "ready-made" but incomplete. To have a complete resolver, we
66          // actually need to bind the missing components making module complete.
67          install( new AetherModule() );
68  
69          // make module "complete" by binding things not bound by AetherModule
70          bind( ArtifactDescriptorReader.class ).to( DefaultArtifactDescriptorReader.class ).in( Singleton.class );
71          bind( VersionResolver.class ).to( DefaultVersionResolver.class ).in( Singleton.class );
72          bind( VersionRangeResolver.class ).to( DefaultVersionRangeResolver.class ).in( Singleton.class );
73          bind( MetadataGeneratorFactory.class ).annotatedWith( Names.named( "snapshot" ) )
74                                                .to( SnapshotMetadataGeneratorFactory.class ).in( Singleton.class );
75  
76          bind( MetadataGeneratorFactory.class ).annotatedWith( Names.named( "versions" ) )
77                                                .to( VersionsMetadataGeneratorFactory.class ).in( Singleton.class );
78  
79          bind( RepositoryConnectorFactory.class ).annotatedWith( Names.named( "basic" ) )
80                  .to( BasicRepositoryConnectorFactory.class );
81          bind( TransporterFactory.class ).annotatedWith( Names.named( "file" ) ).to( FileTransporterFactory.class );
82          bind( TransporterFactory.class ).annotatedWith( Names.named( "http" ) ).to( HttpTransporterFactory.class );
83      }
84  
85      /**
86       * Repository system connectors (needed for remote transport).
87       */
88      @Provides
89      @Singleton
90      Set<RepositoryConnectorFactory> provideRepositoryConnectorFactories(
91              @Named( "basic" ) RepositoryConnectorFactory basic )
92      {
93          Set<RepositoryConnectorFactory> factories = new HashSet<>();
94          factories.add( basic );
95          return Collections.unmodifiableSet( factories );
96      }
97  
98      /**
99       * Repository system transporters (needed for remote transport).
100      */
101     @Provides
102     @Singleton
103     Set<TransporterFactory> provideTransporterFactories( @Named( "file" ) TransporterFactory file,
104                                                          @Named( "http" ) TransporterFactory http )
105     {
106         Set<TransporterFactory> factories = new HashSet<>();
107         factories.add( file );
108         factories.add( http );
109         return Collections.unmodifiableSet( factories );
110     }
111 
112     /**
113      * Repository metadata generators (needed for remote transport).
114      */
115     @Provides
116     @Singleton
117     Set<MetadataGeneratorFactory> provideMetadataGeneratorFactories(
118             @Named( "snapshot" ) MetadataGeneratorFactory snapshot,
119             @Named( "versions" ) MetadataGeneratorFactory versions )
120     {
121         Set<MetadataGeneratorFactory> factories = new HashSet<>( 2 );
122         factories.add( snapshot );
123         factories.add( versions );
124         return Collections.unmodifiableSet( factories );
125     }
126 
127     /**
128      * Simple instance provider for model builder factory. Note: Maven 3.8.1 {@link ModelBuilder} is annotated
129      * and would require much more.
130      */
131     @Provides
132     ModelBuilder provideModelBuilder()
133     {
134         return new DefaultModelBuilderFactory().newInstance();
135     }
136 }