java - Find all direct dependencies of an artifact on Maven Central -


for artifact on maven central (or other given nexus repository), want make list of direct dependencies.

at first, thought reading pom.xml , gather entries dependency section. noticed these entries might have no version (supplied dependency management) or entries might come parent poms.

my second idea build kind of artificial maven project , gather dependencies mvn dependency:tree. might become complicated.

what direct (but reliable) way?

outside of maven plugin, can programmatically using aether. there method readartifactdescriptor this:

gets information artifact direct dependencies , potential relocations.

first, add aether dependencies pom:

<dependencies>     <dependency>         <groupid>org.eclipse.aether</groupid>         <artifactid>aether-impl</artifactid>         <version>${aetherversion}</version>     </dependency>     <dependency>         <groupid>org.eclipse.aether</groupid>         <artifactid>aether-connector-basic</artifactid>         <version>${aetherversion}</version>     </dependency>     <dependency>         <groupid>org.eclipse.aether</groupid>         <artifactid>aether-transport-file</artifactid>         <version>${aetherversion}</version>     </dependency>     <dependency>         <groupid>org.eclipse.aether</groupid>         <artifactid>aether-transport-http</artifactid>         <version>${aetherversion}</version>     </dependency>     <dependency>         <groupid>org.apache.maven</groupid>         <artifactid>maven-aether-provider</artifactid>         <version>${mavenversion}</version>     </dependency> </dependencies> <properties>     <aetherversion>1.1.0</aetherversion>     <mavenversion>3.3.9</mavenversion> </properties> 

then can have:

public static void main(final string[] args) throws exception {     defaultservicelocator locator = mavenrepositorysystemutils.newservicelocator();     repositorysystem system = newrepositorysystem(locator);     repositorysystemsession session = newsession(system);      remoterepository central = new remoterepository.builder("central", "default", "http://repo1.maven.org/maven2/").build();      artifact artifact = new defaultartifact("groupid:artifactid:version");     artifactdescriptorrequest request = new artifactdescriptorrequest(artifact, arrays.aslist(central), null);     artifactdescriptorresult result = system.readartifactdescriptor(session, request);      (dependency dependency : result.getdependencies()) {         system.out.println(dependency);     } }  private static repositorysystem newrepositorysystem(defaultservicelocator locator) {     locator.addservice(repositoryconnectorfactory.class, basicrepositoryconnectorfactory.class);     locator.addservice(transporterfactory.class, filetransporterfactory.class);     locator.addservice(transporterfactory.class, httptransporterfactory.class);     return locator.getservice(repositorysystem.class); }  private static repositorysystemsession newsession(repositorysystem system) {     defaultrepositorysystemsession session = mavenrepositorysystemutils.newsession();     localrepository localrepo = new localrepository("target/local-repo");     session.setlocalrepositorymanager(system.newlocalrepositorymanager(session, localrepo));     // set possible proxies , mirrors     session.setproxyselector(new defaultproxyselector().add(new proxy(proxy.type_http, "host", 3625), arrays.aslist("localhost", "127.0.0.1")));     session.setmirrorselector(new defaultmirrorselector().add("my-mirror", "http://mirror", "default", false, "external:*", null));     return session; } 

what creating aether repository system , telling read artifact descriptor of given artifact. artifact constructed constructor new defaultartifact("...") giving wanted coordinates.

a request object created artifact , list of repositories fetch from. in above sample, maven central added, add more remoterepository creating them remoterepository.builder class. after calling readartifactdescriptor, result contains list of direct dependencies, can retrieved getdependencies().

proxies , mirrors can configured of defaultproxyselector , defaultmirrorselector respectively. defaultproxyselector.add takes proxy argument, can created constructor passing type (like "http"), host, port , optionally authentication (take @ authenticationbuilder class create authentication objects), , list of non-proxied host. in same way, defaultmirrorselector.add takes id, url, type (which "default" maven, have p2 example), whether repository manager, actual repository ids mirrored (according mirror specification), , repository type not mirrored.


Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -