Reading Maven POM metadata with ShrinkWrap Resolver


#1

Hi,

I’m usng shrinkwrap res and I’d like to read an artifact’s metadata such as name, license, description, etc… I haven’t found a way in the API, but perhaps there’s a way to do it?


#2

Hi Stephane,

the only way is now using experimental API and and get PasedPomFile.

A nice way would most likely be included in 2.x stream.

Karel


#3

I tried:

    MavenResolverSystem resolver = getResolver();
    MavenWorkingSession mavenWorkingSession = ((MavenWorkingSessionContainer)resolver).getMavenWorkingSession();
    final MavenStrategyStage mss = resolver.resolve(groupId+":"+artifactId+":"+version);
    final MavenFormatStage mfs = mss.using(SCOPED_STRATEGY);
    final MavenResolvedArtifact info = mfs.asSingleResolvedArtifact();
    System.err.println(mavenWorkingSession.getParsedPomFile());

And I got:

java.lang.IllegalArgumentException: Maven Project Object Model must not be null
at org.jboss.shrinkwrap.resolver.impl.maven.util.Validate.notNull(Validate.java:46)
at org.jboss.shrinkwrap.resolver.impl.maven.pom.ParsedPomFileImpl.(ParsedPomFileImpl.java:52)
at org.jboss.shrinkwrap.resolver.impl.maven.MavenWorkingSessionImpl.getParsedPomFile(MavenWorkingSessionImpl.java:319)
at com.redhat.ceylon.cmr.maven.AetherUtils.search(AetherUtils.java:281)


#4

From the source code it looks like I would have to call loadPomFromFile myself, but I don’t know how to obtain the File corresponding to the pom I’m resolving? I only have the maven coordinates. I tried resolving group:artifact:pom:version but that failed.


#5

This is the dirty dirty hack… :smile:

package org.aslak.arquillian.test;

import java.io.File;
import java.lang.reflect.Field;

import org.apache.maven.model.Model;
import org.jboss.shrinkwrap.resolver.api.maven.Maven;
import org.jboss.shrinkwrap.resolver.api.maven.MavenResolvedArtifact;
import org.jboss.shrinkwrap.resolver.api.maven.MavenResolverSystem;
import org.jboss.shrinkwrap.resolver.api.maven.pom.ParsedPomFile;
import org.jboss.shrinkwrap.resolver.impl.maven.MavenWorkingSessionContainer;
import org.jboss.shrinkwrap.resolver.impl.maven.pom.ParsedPomFileImpl;
import org.junit.Test;

public class ResolverTestCase {

    @Test
    public void should() throws Exception {
        // Resolve the 'artifact', Resolver filters poms automatically so they can't be resolved directly
        MavenResolvedArtifact f = Maven.resolver()
                    .resolve("org.wildfly:wildfly-dist:zip:8.2.0.Final")
                        .withoutTransitivity()
                .asSingleResolvedArtifact();
        
        // Make a pom file out of the resolved artifact
        File pom = new File(f.asFile().getAbsolutePath().replace(".zip", ".pom"));
        
        // Load the pom file as a 'source' for configuration in Resolver
        MavenResolverSystem system = Maven.resolver();
        system.loadPomFromFile(pom);

        // Hack around a bit to get to the internal Maven Model object
        ParsedPomFile parsedPom = ((MavenWorkingSessionContainer)system).getMavenWorkingSession().getParsedPomFile();
        Field modelField = ParsedPomFileImpl.class.getDeclaredField("model");
        modelField.setAccessible(true);

        Model model = (Model)modelField.get(parsedPom);
       
        System.out.println(model);
    }
}

#6

Ah yes, well, it not more or less dirty than my current solution of s/.jar/.pom/ and then parse it :frowning:


ShrinkWrap Resolver Metadata API