2
votes

I'm using ant-ivy to resolve dependencies from the maven repository. And i'm using the same ant-ivy to publish new artifacts into that repository, so i'm generating .pom file in ant too.

The generated .pom file is very simple and looks like this(PROJECT_A):

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>COMPANY</groupId>
    <artifactId>PROJECT_A</artifactId>
    <packaging>jar</packaging>
    <version>1.0</version>

    <dependencies>
        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>3.2.1</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>commons-configuration</groupId>
            <artifactId>commons-configuration</artifactId>
            <version>1.7</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.16</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>1.9.5</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

So it just has some dependencies in compile scope and some in test scope. Now my ivy.xml file for that project(and the source of that .pom above) looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<ivy-module version="2.0" xmlns:m="http://ant.apache.org/ivy/maven">
    <info organisation="COMPANY" module="PROJECT_A" revision="1.0" />
    <configurations defaultconf="default,sources" defaultconfmapping="sources->sources;%->default">
        <conf name="test" visibility="private"/>
        <conf name="default" description="list of dependencies"/>
        <conf name="sources" description="source files for all dependencies" />
    </configurations>
    <publications>
        <artifact type="jar" conf="default" />
        <artifact type="sources" ext="jar" m:classifier="sources" conf="sources" />
        <artifact type="pom" ext="pom" conf="default" />
    </publications>
    <dependencies>
        <!-- General -->
        <dependency org="commons-collections" name="commons-collections" rev="3.2.1" transitive="false"/>
        <dependency org="commons-configuration" name="commons-configuration" rev="1.7" transitive="false"/>
        <dependency org="commons-lang" name="commons-lang" rev="2.6" transitive="false"/>
        <dependency org="log4j" name="log4j" rev="1.2.16" transitive="false"/>

        <!-- dependencies for junit testing -->
        <dependency org="junit" name="junit" rev="latest.release" conf="test" />
        <dependency org="org.mockito" name="mockito-all" rev="latest.release" conf="test" /> <!-- it's useful by itself, plus it has hamcrest in it which junit needs -->
    </dependencies>
</ivy-module>

Again, very simple - 3 configurations, the default has all the dependencies, test is for testing dependencies and sources to publish sources.

And it all works quite well, apart of one thing - i'm declaring my dependencies as not transitive in the PROJECT_A, then when i'm pushing the pom to the repository those dependencies are listed there in the scope compile. Therefore the other project(PROJECT_B) which will have PROJECT_A as a dependency, will have all the transitive dependencies of the PROJECT_A as well, and i don't want that at all i just want those which are explicitly declared in the ivy.xml of PROJECT_A.

I've tried playing with the scopes and mappings, but it seems i really don't understand what i'm doing there as it doesn't make any sense. I would like to modify that scheme somehow so that when i include the PROJECT_A as a dependency, it'll only include the actual dependencies of the PROJECT_A declared in the ivy.xml, so transitive flag will be taken into account.

One more thing, i'm creating that .pom file like that:

<ivy:makepom ivyfile="generated-ivy.xml" pomfile="${ant.project.name}.pom" templatefile="${template.pom}" artifactPackaging="jar">
    <mapping conf="default" scope="compile" />
    <mapping conf="test" scope="test" />
</ivy:makepom>
1
Problem is you're publishing into a Maven repository, so you must live with its metadata format (POM). Excluding transitive dependencies doesn't work the same way, ivy is more flexible in my opinion.Mark O'Connor

1 Answers

0
votes

Mark the dependency as <optional> to make it non-transitive.

Dirty hack, but that's maven.