Programmatically resolving Maven dependencies outside of a plugin - get RepositorySystemSession and RepositorySystem

前端 未结 6 680
Happy的楠姐
Happy的楠姐 2020-12-24 08:55

Maybe this is going to be a larger task than I had originally thought, but regardless, I\'m trying to load a MavenProject from a file and then resolve its depen

6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-24 09:21

    This is covered in "Aether/Setting Aether Up" for the RepositorySystem and in "Aether/Creating a Repository System Session" in the eclipse wiki.

    There is not a ton of documentation, but you can create a RepositorySystem as follows:

    // verbatim copy from the Setting Aether Up link
    private static RepositorySystem newRepositorySystem()
    {
        DefaultServiceLocator locator = MavenRepositorySystemUtils.newServiceLocator();
        locator.addService( RepositoryConnectorFactory.class, BasicRepositoryConnectorFactory.class );
        locator.addService( TransporterFactory.class, FileTransporterFactory.class );
        locator.addService( TransporterFactory.class, HttpTransporterFactory.class );
    
        return locator.getService( RepositorySystem.class );
    }
    

    Do note that this requires the dependencies detailed in "Getting Aether", most notably maven-aether-provider.

    When you have your repository system the tutorial goes on to create a RepositorySystemSession with the following factory method:

    // copied verbatim from "Creating a Repository System Session"
    private static RepositorySystemSession newSession( RepositorySystem system )
    {
        DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession();
    
        LocalRepository localRepo = new LocalRepository( "target/local-repo" );
        session.setLocalRepositoryManager( system.newLocalRepositoryManager( session, localRepo ) );
    
        return session;
    }
    

提交回复
热议问题