gradle拷贝静态资源文件,如何在gradle中复制依赖库库JAR

i got a runnable jar with this build.gradle

apply plugin: 'java'

apply plugin: 'application'

manifest.mainAttributes("Main-Class" : "com.test.HelloWorld")

repositories {

mavenCentral()

}

dependencies {

compile (

'commons-codec:commons-codec:1.6',

'commons-logging:commons-logging:1.1.1',

'org.apache.httpcomponents:httpclient:4.2.1',

'org.apache.httpcomponents:httpclient:4.2.1',

'org.apache.httpcomponents:httpcore:4.2.1',

'org.apache.httpcomponents:httpmime:4.2.1',

'ch.qos.logback:logback-classic:1.0.6',

'ch.qos.logback:logback-core:1.0.6',

'org.slf4j:slf4j-api:1.6.0',

'junit:junit:4.+'

)

}

but it run failed, because the dependencies jars can't find.

and then i add this code:

task copyToLib(type: Copy) {

into "$buildDir/output/libs"

from configurations.runtime

}

but nothing change...i can't find the folder output/libs...

how can i copy the dependencies libs jars to a specified folder or path?

解决方案

Add:

build.dependsOn(copyToLib)

When gradle build runs, Gradle builds tasks and whatever tasks depend on it (declared by dependsOn). Without setting build.dependsOn(copyToLib), Gradle will not associate the copy task with the build task.

So:

apply plugin: 'java'

apply plugin: 'application'

manifest.mainAttributes("Main-Class" : "com.test.HelloWorld")

repositories {

mavenCentral()

}

dependencies {

compile (

'commons-codec:commons-codec:1.6',

'commons-logging:commons-logging:1.1.1',

'org.apache.httpcomponents:httpclient:4.2.1',

'org.apache.httpcomponents:httpclient:4.2.1',

'org.apache.httpcomponents:httpcore:4.2.1',

'org.apache.httpcomponents:httpmime:4.2.1',

'ch.qos.logback:logback-classic:1.0.6',

'ch.qos.logback:logback-core:1.0.6',

'org.slf4j:slf4j-api:1.6.0',

'junit:junit:4.+'

)

}

build.dependsOn(copyToLib)

task copyToLib(type: Copy) {

into "$buildDir/output/libs"

from configurations.runtime

}

你好!针对你的问题,我可以提供以下的解决方案: 首先,需要确定你的Gradle Copy任务是否正确配置。你可以检查以下几个方面: 1. 你的Copy任务是否正确指定了源文件和目标文件路径。例如,你需要通过类似下面的代码指定源文件和目标文件路径: ``` task copyResources(type: Copy) { from 'src/main/resources' into 'build/resources/main' } ``` 这里的 `from` 表示源文件路径,`into` 表示目标文件路径。 2. 你的Copy任务是否在正确的任务之前运行。例如,如果你希望在编译Java代码之前拷贝静态资源文件,你需要将Copy任务添加到Java编译任务之前。例如: ``` tasks.compileJava.dependsOn copyResources ``` 这样,每次执行 `compileJava` 任务时,Gradle会先执行 `copyResources` 任务,然后再编译Java代码。 如果以上两个方面都已经检查过了,但Copy任务依然无法复制文件,那么你可以考虑以下几种可能的原因: 1. 源文件路径错误。你需要确保 `from` 指定的路径是正确的,并且源文件确实存在于该路径下。 2. 目标文件路径错误。你需要确保 `into` 指定的路径是正确的,并且该路径存在于你的项目。 3. Copy任务的执行条件不满足。例如,你可能在执行Copy任务之前需要创建目标文件夹,否则Copy任务无法成功执行。你可以考虑添加一个 `doLast` 闭包,在任务执行完成后创建目标文件夹。 希望以上解决方案能够帮助你解决问题!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值