我的系统中有一个模块,我们将其称为 someService
。该服务依赖于一些基础设施模块,我们将其称为someInfra
。基础设施模块依赖于一些第三方 jar。
问题是,当我构建 someService
时,我得到someInfra.jar
到我的类路径中,但我没有得到它的依赖项,这最终导致 ClassNotFoundException
在运行时。
执行 ./gradlew clean build
后,我希望拥有成功运行时执行所需的所有 jar。
我认为 Maven 插件与这个问题有某种关系。我该怎么办?
更新:根据马克请求,我现在添加两个 build.gradle
文件,第一个是 someInfra
,第二个是someService
一些基础设施 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.gradle
也是如此。 , gradle.properties
, gradle\wrapper\gradle-wrapper.properties
(使用 1.9)。这到底是怎么回事……
最佳答案
在工件代码块后添加以下内容
apply plugin: 'maven'
uploadArchives {
uploadDescriptor = true
}
关于java - 如何使模块 x 的第 3 方依赖项与其一起到达依赖于模块 x 的模块 y 的类路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25554471/