kumetix
kumetix

Reputation: 1073

How to make 3rd party dependencies of module x to arrive together with it into the classpath of module y which depends on module x?

I have a module in my system, we'll call it someService. This service depends on some infra module, we'll call it someInfra. The infra module depends on some 3rd party jars.

The problem is, when I build someService, I get someInfra.jar into my classpath, but I don't get its dependencies, which leads eventually to a ClassNotFoundException in runtime.

Upon the execution of ./gradlew clean build, I would like to have all jars needed for a successful runtime execution.

I think maven plugin is somehow related to the issue. What should I do?

UPDATE: per Mark request I now add two build.gradle files, the first is of someInfra, the second is of someService

someInfra build.gradle

group="x.y
version="0.1.1-s3"

buildscript {
    repositories {
        maven {
            url "${artifactory_contextUrl}/plugins"
            credentials {
                username = "${artifactory_user}"
                password = "${artifactory_password}"
            }
        }

    }
    dependencies {
        classpath(group: 'org.jfrog.buildinfo',  name: 'build-info-extractor-gradle', version: '2.0.9')
        classpath(group: 'org.springframework.boot', name: 'spring-boot-gradle-plugin', version: '1.0.0.RELEASE')
    }
}

apply plugin: 'artifactory'
apply plugin: 'java'
apply plugin: 'pmd'
apply plugin: 'findbugs'
apply plugin: 'maven'

artifactory {
    contextUrl = "${artifactory_contextUrl}"
    publish {
        repository {
            repoKey = 'libs-release-local'
            username = "${artifactory_user}"
            password = "${artifactory_password}"
            maven = true
        }
    }
    resolve {
        repository {
            repoKey = 'libs'
            username = "${artifactory_user}"
            password = "${artifactory_password}"
            maven = true
        }
    }
}

sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8

configurations {
    provided
}

sourceSets {
    main {
        compileClasspath += configurations.provided
    }
}

dependencies {
    compile group: 'org.slf4j',                 name: 'slf4j-api',          version: '1.7.+'
    compile (group: 'org.springframework',      name: 'spring-context',     version: '4.0.3.RELEASE')
    compile (group: 'org.apache.curator',       name: 'curator-framework',  version: '2.5.+') {
        exclude group: 'org.slf4j'
        exclude group: 'log4j'
    }
    compile ('ch.qos.logback:logback-classic:1.1.2')

    testCompile group: 'junit',                 name: 'junit',              version: '4.+'
    testCompile group: 'org.springframework',   name: 'spring-test',        version: '4.0.3.RELEASE'
    testCompile group: 'org.mockito',           name: 'mockito-all',        version: '1.9.+'
}

test {
    scanForTestClasses = false
}

pmd {
    toolVersion = '5.1.0'
    ignoreFailures = true
    ruleSets = []
    sourceSets = [sourceSets.main]
    ruleSetFiles = files("${rootDir}/gradle/pmd.xml")
}

findbugs {
    toolVersion = '3.0.0-SNAPSHOT'
    sourceSets = [sourceSets.main]
    ignoreFailures = true
    reportsDir = file("$project.buildDir/reports/findbugs")
    includeFilter = file("${rootDir}/gradle/findbugs.xml")
}

someService build.gradle

group="x.y"
version="0.1.4-s3"

buildscript {
    repositories {
        maven {
            url "${artifactory_contextUrl}/plugins"
            credentials {
                username = "${artifactory_user}"
                password = "${artifactory_password}"
            }
        }

    }
    dependencies {
        classpath(group: 'org.jfrog.buildinfo',  name: 'build-info-extractor-gradle', version: '2.0.9')
        classpath(group: 'org.springframework.boot', name: 'spring-boot-gradle-plugin', version: '1.0.0.RELEASE')

    }
}

apply plugin: 'artifactory'

artifactory {
    contextUrl = "${artifactory_contextUrl}"
    publish {
        repository {
            repoKey = 'libs-release-local'
            username = "${artifactory_user}"
            password = "${artifactory_password}"
            maven = true
        }
    }
    resolve {
        repository {
            repoKey = 'libs'
            username = "${artifactory_user}"
            password = "${artifactory_password}"
            maven = true
        }
    }
}

apply plugin: 'java'
apply plugin: 'spring-boot'
apply plugin: 'pmd'
apply plugin: 'findbugs'

sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8

configurations {
    provided
}

sourceSets {
    main {
        compileClasspath += configurations.provided
    }
}

dependencies {
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-web'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-actuator'
    compile group: 'org.springframework', name: 'spring-remoting', version: '2.0.8'
    compile group: 'org.springframework.hateoas', name: 'spring-hateoas', version: '0.9.0.RELEASE'
    compile group: "org.hibernate", name: "hibernate-validator"
    compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.1'
    compile group: 'org.apache.commons', name: 'commons-collections4', version: '4.0'
    compile group: 'com.mangofactory' , name: 'swagger-springmvc', version: '0.8.4'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-remote-shell', version: '1.0.0.RELEASE'

    compile group: 'x.y', name: 'someInfra', version: '0.1.1-s3'

    testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test'

    runtime group: 'org.postgresql', name: 'postgresql', version:'9.3-1101-jdbc41'
    runtime group: 'org.hsqldb', name: 'hsqldb'
}

test {
    scanForTestClasses = false
    include '**/ApplicationTestSuite.class'
}

pmd {
    toolVersion = '5.1.0'
    ignoreFailures = true
    ruleSets = []
    sourceSets = [sourceSets.main]
    ruleSetFiles = files("${rootDir}/gradle/pmd.xml")
}

findbugs {
    toolVersion = '3.0.0-SNAPSHOT'
    sourceSets = [sourceSets.main]
    ignoreFailures = true
    reportsDir = file("$project.buildDir/reports/findbugs")
    includeFilter = file("${rootDir}/gradle/findbugs.xml")
}

Notice how someInfra depends on org.apache.curator:curator-framework.

When I execute someService main method (using spring-boot), I fail on

Caused by: java.lang.ClassNotFoundException: org.apache.curator.RetryPolicy
    at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 37 more

UPDATE: I now notice a weird behavior in which someInfra,jar is wrapped WITHOUT any of its dependencies, while someService.jar is wrapped with all its dependencies. Weird. both build.gradle are pretty much the same, same goes for settings.gradle, gradle.properties, gradle\wrapper\gradle-wrapper.properties (using 1.9). What the hell is going on here...

Upvotes: 0

Views: 305

Answers (2)

pl47ypus
pl47ypus

Reputation: 401

add the following after the artifactory code block

apply plugin: 'maven'

uploadArchives {
    uploadDescriptor = true 
}

Upvotes: 1

Bryan Moore
Bryan Moore

Reputation: 49

I don't have enough reputation to comment, so I'm adding this as an answer. Since you mentioned maven plug in, perhaps this is what you need. You can add this into your pom.xml in the build/plugins section to get a jar with all the dependencies added.

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
    </configuration>
</plugin>

See also this stackoverflow question: Including dependencies in a jar with Maven

Upvotes: 0

Related Questions