我的系统中有一个模块,我们称之为someService。此服务依赖于某些下位模块,我们将称之为someInfra。下面的模块依赖于一些第三方的罐子。
问题是,当我构建someService时,我让someInfra.jar进入我的类路径,但是我没有得到它的依赖关系,这最终导致了运行时的ClassNotFoundException。
在执行./gradlew clean build时,我希望拥有成功的运行时执行所需的所有jars。
我认为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)时,我失败了。
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)。这里到底发生了什么..。
发布于 2014-09-08 09:28:18
在工件代码块之后添加以下内容
apply plugin: 'maven'
uploadArchives {
uploadDescriptor = true
}发布于 2014-08-28 17:38:42
我没有足够的声誉来评论,所以我加入这个回答。既然您提到了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>还请参见这个堆栈溢出问题:Including dependencies in a jar with Maven
https://stackoverflow.com/questions/25554471
复制相似问题