我是Gradle的新手。
当我运行 build 时,该版本将 compile 和 testCompile 依赖项都复制到了,而不是将特定的依赖项复制到特定的目录中。 > src / test / resources 。
我只希望将 test compile 依赖项复制到 src / test / resources ,并将所有其他依赖项复制到 main / src / resources 。
谢谢
apply plugin: 'java'
apply plugin: 'war'
String spring_version = "3.1.2.RELEASE@jar" ;
String spring_sec_version = "3.1.2.RELEASE@jar" ;
String hibernate_version = "4.1.7.Final@jar" ;
repositories {
mavenCentral()
}
// "org.springframework:spring-oxm:"+spring_version,
List spring = [
"org.springframework:spring-context:"+spring_version,
"org.springframework:spring-beans:"+spring_version,
"org.springframework:spring-core:"+spring_version,
"org.springframework:spring-aop:"+spring_version,
"org.springframework:spring-expression:"+spring_version,
"org.springframework:spring-jdbc:"+spring_version,
"org.springframework:spring-tx:"+spring_version,
"org.springframework:spring-web:"+spring_version,
"org.springframework:spring-webmvc:"+spring_version,
"org.springframework:spring-orm:"+spring_version,
"org.springframework:spring-asm:"+spring_version
]
List spring_security = [
"org.springframework.security:spring-security core:"+spring_sec_version,
"org.springframework.security:spring-security-config:"+spring_sec_version,
"org.springframework.security:spring-security-web:"+spring_sec_version
]
List spring_aop = [
"aopalliance:aopalliance:1.0@jar",
"cglib:cglib-nodep:2.2.2@jar"
]
List validation_lib = [
"org.hibernate:hibernate-validator:4.3.0.Final@jar",
"javax.validation:validation-api:1.0.0.GA@jar"
]
List log_lib = [
"org.jboss.logging:jboss-logging:3.1.2.GA@jar",
"org.slf4j:slf4j-api:1.6.6@jar",
"org.slf4j:slf4j-jdk14:1.6.6@jar",
"org.slf4j:log4j-over-slf4j:1.6.6@jar",
"org.slf4j:jcl-over-slf4j:1.6.6@jar",
"log4j:log4j:1.2.17@jar"
]
List hibernate_api = [
"org.hibernate.javax.persistence:hibernate-jpa-2.0-api:1.0.1.Final@jar"
]
List hibernate = [
"org.jboss.spec.javax.transaction:jboss-transaction-api_1.1_spec:1.0.1.Final@jar",
"org.hibernate.common:hibernate-commons-annotations:4.0.1.Final@jar",
"org.javassist:javassist:3.16.1-GA",
"org.hibernate:hibernate-entitymanager:"+hibernate_version,
"org.hibernate:hibernate-core:"+hibernate_version
]
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.10'
testCompile group: 'org.mockito', name: 'mockito-all', version: '1.10.19'
compile hibernate_api
compile hibernate
compile spring
compile spring_security
compile spring_aop
compile "org.codehaus.jackson:jackson-jaxrs:1.9.9" //lib jackson for JSON
compile validation_lib
compile log_lib
runtime "postgresql:postgresql:9.1-901.jdbc4@jar"
compile "rhino:js:1.7R2@jar"
}
task copyDependencies(type: Copy) {
from (configurations.compile)
into "src/main/resources"
}
task copyTestDependencies(type: Copy) {
from (configurations.testCompile)
into "src/test/resources"
}
build.dependsOn(copyDependencies)
build.dependsOn(copyTestDependencies)
答案 0 :(得分:1)
回答前有两件事:
compile
和testCompile
应被替换implementation
和testImplementation
。有关详情,请参见documentation。我的回答将使用这些来代替。resources
目录,将使事情变得混乱。您确实应该避免这样做,而应创建正确连接以在需要时使用的新目录。您遇到的问题是testImplementation
扩展了implementation
,因此您会将 runtime 和 test 依赖项都收集到测试依赖项的位置
在最简单的途径是创建一个自定义的配置为你的测试依赖关系,这样就可以独立访问运行者的他们:
configurations {
testDependencies
testImplementations {
extendsFrom testDependencies
}
}
dependencies {
testDependencies group: 'junit', name: 'junit', version: '4.10'
// And others
}
和那么你将复制的内容testDependencies
。
此方法的缺点是testDependencies
的分辨率将不受 runtime 依赖关系的影响。这可能是一个问题,但与上下文有关。