2 Replies Latest reply: Jul 13, 2012 3:24 AM by Karel Piwko RSS

Best way to work with Maven Dependencies

gboro54 Novice

Hi,

 

  I have a multi module maven project with some modules being dependent on others. I am using the MavenDependencyResolver as follows:

JavaArchive imported = DependencyResolvers.use(MavenDependencyResolver.class).goOffline().artifact("com.company.billing:billing-common:1.0.0-SNAPSHOT").resolveAs(JavaArchive.class).iterator().next();

 

 

The problem with the above is that when I do a release I will need to change the version explicitly. Additionally I use code as follows to resolve now JEE parts of the module(amongst other things)

 

ShrinkWrap
                                        .create(EnterpriseArchive.class, "phlx-mtd-test-ear.ear")
                                        .addAsLibraries(
                                                            DependencyResolvers.use(MavenDependencyResolver.class)
                                                                                .includeDependenciesFromPom("pom.xml")
                                                                                .goOffline()
                                                                                .resolveAs(JavaArchive.class, filter))

 

 

The problem above is performing a mvn release:prepare release:perform it cannot resolve my newly built dependencies with the following exception:

 

[INFO] Jul 6, 2012 7:46:32 AM org.jboss.shrinkwrap.resolver.impl.maven.LogRepositoryListener artifactDescriptorMissing
[INFO] WARNING: Missing artifact descriptor for com.company.billing:domain:jar:1.0.0.Alpha
[INFO] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.995 sec <<< FAILURE!

 

 

Any help would be grealy appricated!

 

TIA

  • 1. Re: Best way to work with Maven Dependencies
    Oliver Fischer Newbie

    Hi,

     

    this is quite easy, if you do in the following way

     

    First create a POM below src/test/resources and call it arquillian-deps.xml.

     

    Your POM should be similar to this one:

     

     

    <?xml version="1.0" encoding="UTF-8"?>

    <project xmlns="http://maven.apache.org/POM/4.0.0"

                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

                xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

        <modelVersion>4.0.0</modelVersion>

     

        <parent>

            <groupId>com.yourcompanygroupId>

            <artifactId>xyz</artifactId>

            <version>0.0.3-SNAPSHOT</version>

        </parent>

     

        <artifactId>xyz.yourcompany.testdependencies</artifactId>

     

        <dependencies>

            <dependency>

                <groupId>com.yourcompany</groupId>

                <artifactId>abc.service</artifactId>

                <version>${project.version}</version>

                <type>ejb</type>

            </dependency>

        </dependencies>

    </project>

     

    As you see, this POM inherits the parent POM of your project. With Maven 3 it also inherits the version of your project. As the result, this POM defines a dependency to com:yourcompany:abc.service:0.0.3-SNAPSHOT.

     

    In your deployment method initialise your MavenDependecyResolver like this:

     

    String pom = RegistrationServiceArquillianModuleTest.class.getResource("/arquillian-deps.xml").getFile();

     

    EnterpriseArchive earArchive = ShrinkWrap.create(EnterpriseArchive.class, createEarName());

     

    MavenDependencyResolver resolver = DependencyResolvers.use(MavenDependencyResolver.class)

                    .includeDependenciesFromPom(pom)

                    .goOffline();

     

    Now you have only one place there to manage your dependencies, the arquillian-deps.xml.

     

    For me it works perfect even with more dependencies.


  • 2. Re: Best way to work with Maven Dependencies
    Karel Piwko Master

    Oliver, you're approach should generally work. Two notes though. It's hard to tell which ShrinkWrap version are you using, but in 2.0.0-alpha-1 following improvents are present:

     

    1/ You should be able to simplify access to the pom via classpath: protocol:

     

    DependencyResolvers.use(MavenDependencyResolver.class).loadEffectivePom("classpath:arquillian-deps.xml")
                            .importAllDependencies().resolveAs(JavaArchive.class));
    

     

    2/ There is a experimental classpath resolution a.k.a. reactor support in 2.0.0-alpha-1 activated by default. This means that you're able to resolve dependencies defined in your multimodule project even if they are not yet in any repository, like sybling modules.

     

    To answer original question, you can alway load a version from a pom file and then you can omit it when asking for dependency:

     

    DependencyResolvers.use(MavenDependencyResolver.class).loadEffectivePom("classpath:arquillian-deps.xml").artifact("com.company.billing:billing-common").resolveAs(...);
    

     

    Reformatted.