如何使模块x的第三方依赖项与它一起到达依赖于模块x的模块y的类路径?

时间:2014-08-28 17:17:42

标签: java gradle dependencies dependency-management

我的系统中有一个模块,我们称之为someService。此服务取决于某些infra模块,我们称之为someInfra。 infra模块取决于一些第三方罐子。

问题是,当我构建someService时,我将someInfra.jar放入我的类路径中,但是我没有得到它的依赖项,最终导致运行时ClassNotFoundException

执行./gradlew clean build后,我想拥有成功运行时执行所需的所有jar。

我认为maven插件与此问题有某种关联。我该怎么办?

更新:根据标记请求,我现在添加两个build.gradle个文件,第一个是someInfra,第二个是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")
}

请注意someInfra取决于org.apache.curator:curator-framework的方式。

当我执行someService main方法(使用spring-boot)时,我失败了

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

更新:我现在注意到一种奇怪的行为,其中someInfra,jar被包装而没有任何依赖,而someService.jar包含其所有依赖项。奇怪的。两个build.gradle几乎相同,settings.gradlegradle.propertiesgradle\wrapper\gradle-wrapper.properties(使用1.9)也是如此。这到底是怎么回事......

2 个答案:

答案 0 :(得分:1)

在神器代码块

之后添加以下内容
apply plugin: 'maven'

uploadArchives {
    uploadDescriptor = true 
}

答案 1 :(得分:0)

我没有足够的声誉来评论,因此我将此作为答案添加。既然你提到了maven插件,也许这就是你需要的。您可以将它添加到build / plugins部分的pom.xml中,以获得添加了所有依赖项的jar。

<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>

另请参阅此stackoverflow问题:   Including dependencies in a jar with Maven