Gradle构建任务失败,出现java.io.IOException:无法删除文件

时间:2017-12-26 14:28:52

标签: java gradle log4j build.gradle

我有一个使用gradle进行依赖关系管理的java项目。我第一次使用build.gradle脚本使用" gradle installApp"来构建项目。命令它成功创建项目的jar文件,并使用java -jar命令完美地运行jar。

Log4j被用作日志记录实用程序,当我使用java -jar命令执行jar时,它根据log4j.properties生成日志文件。

我面临的问题是,当我尝试在不停止jar的情况下重新构建项目时,我的构建失败并出现以下异常

D:\MyProject\backend\mymodule\MyModuleJAR>gradle installApp --stacktrace
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:jar
:installApp FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':installApp'.
> java.io.IOException: Unable to delete file: D:\MyProject\backend\mymodule\MyModuleJAR\build\install\MyModuleJAR\log\my.log

* Try:
Run with --info or --debug option to get more log output.

* Exception is:
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':installApp'.
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:69)
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:46)
        at org.gradle.api.internal.tasks.execution.PostExecutionAnalysisTaskExecuter.execute(PostExecutionAnalysisTaskExecuter.java:35)
        at org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter.execute(SkipUpToDateTaskExecuter.java:64)
        at org.gradle.api.internal.tasks.execution.ValidatingTaskExecuter.execute(ValidatingTaskExecuter.java:58)
        at org.gradle.api.internal.tasks.execution.SkipEmptySourceFilesTaskExecuter.execute(SkipEmptySourceFilesTaskExecuter.java:52)
        at org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter.execute(SkipTaskWithNoActionsExecuter.java:52)
        at org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execute(SkipOnlyIfTaskExecuter.java:53)
        at org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter.execute(ExecuteAtMostOnceTaskExecuter.java:43)
        at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:203)
        at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:185)
        at org.gradle.execution.taskgraph.AbstractTaskPlanExecutor$TaskExecutorWorker.processTask(AbstractTaskPlanExecutor.java:66)
        at org.gradle.execution.taskgraph.AbstractTaskPlanExecutor$TaskExecutorWorker.run(AbstractTaskPlanExecutor.java:50)
        at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor.process(DefaultTaskPlanExecutor.java:25)
        at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter.execute(DefaultTaskGraphExecuter.java:110)
        at org.gradle.execution.SelectedTaskExecutionAction.execute(SelectedTaskExecutionAction.java:37)
        at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:37)
        at org.gradle.execution.DefaultBuildExecuter.access$000(DefaultBuildExecuter.java:23)
        at org.gradle.execution.DefaultBuildExecuter$1.proceed(DefaultBuildExecuter.java:43)
        at org.gradle.execution.DryRunBuildExecutionAction.execute(DryRunBuildExecutionAction.java:32)
        at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:37)
        at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:30)
        at org.gradle.initialization.DefaultGradleLauncher$4.run(DefaultGradleLauncher.java:154)
        at org.gradle.internal.Factories$1.create(Factories.java:22)
        at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:90)
        at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:52)
        at org.gradle.initialization.DefaultGradleLauncher.doBuildStages(DefaultGradleLauncher.java:151)
        at org.gradle.initialization.DefaultGradleLauncher.access$200(DefaultGradleLauncher.java:32)
        at org.gradle.initialization.DefaultGradleLauncher$1.create(DefaultGradleLauncher.java:99)
        at org.gradle.initialization.DefaultGradleLauncher$1.create(DefaultGradleLauncher.java:93)
        at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:90)
        at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:62)
        at org.gradle.initialization.DefaultGradleLauncher.doBuild(DefaultGradleLauncher.java:93)
        at org.gradle.initialization.DefaultGradleLauncher.run(DefaultGradleLauncher.java:82)
        at org.gradle.launcher.exec.InProcessBuildActionExecuter$DefaultBuildController.run(InProcessBuildActionExecuter.java:94)
        at org.gradle.tooling.internal.provider.ExecuteBuildActionRunner.run(ExecuteBuildActionRunner.java:28)
        at org.gradle.launcher.exec.ChainingBuildActionRunner.run(ChainingBuildActionRunner.java:35)
        at org.gradle.launcher.exec.InProcessBuildActionExecuter.execute(InProcessBuildActionExecuter.java:43)
        at org.gradle.launcher.exec.InProcessBuildActionExecuter.execute(InProcessBuildActionExecuter.java:28)
        at org.gradle.launcher.exec.ContinuousBuildActionExecuter.execute(ContinuousBuildActionExecuter.java:77)
        at org.gradle.launcher.exec.ContinuousBuildActionExecuter.execute(ContinuousBuildActionExecuter.java:47)
        at org.gradle.launcher.exec.DaemonUsageSuggestingBuildActionExecuter.execute(DaemonUsageSuggestingBuildActionExecuter.java:51)
        at org.gradle.launcher.exec.DaemonUsageSuggestingBuildActionExecuter.execute(DaemonUsageSuggestingBuildActionExecuter.java:28)
        at org.gradle.launcher.cli.RunBuildAction.run(RunBuildAction.java:43)
        at org.gradle.internal.Actions$RunnableActionAdapter.execute(Actions.java:170)
        at org.gradle.launcher.cli.CommandLineActionFactory$ParseAndBuildAction.execute(CommandLineActionFactory.java:237)
        at org.gradle.launcher.cli.CommandLineActionFactory$ParseAndBuildAction.execute(CommandLineActionFactory.java:210)
        at org.gradle.launcher.cli.JavaRuntimeValidationAction.execute(JavaRuntimeValidationAction.java:35)
        at org.gradle.launcher.cli.JavaRuntimeValidationAction.execute(JavaRuntimeValidationAction.java:24)
        at org.gradle.launcher.cli.CommandLineActionFactory$WithLogging.execute(CommandLineActionFactory.java:206)
        at org.gradle.launcher.cli.CommandLineActionFactory$WithLogging.execute(CommandLineActionFactory.java:169)
        at org.gradle.launcher.cli.ExceptionReportingAction.execute(ExceptionReportingAction.java:33)
        at org.gradle.launcher.cli.ExceptionReportingAction.execute(ExceptionReportingAction.java:22)
        at org.gradle.launcher.Main.doAction(Main.java:33)
        at org.gradle.launcher.bootstrap.EntryPoint.run(EntryPoint.java:45)
        at org.gradle.launcher.bootstrap.ProcessBootstrap.runNoExit(ProcessBootstrap.java:54)
        at org.gradle.launcher.bootstrap.ProcessBootstrap.run(ProcessBootstrap.java:35)
        at org.gradle.launcher.GradleMain.main(GradleMain.java:23)
Caused by: org.gradle.api.UncheckedIOException: java.io.IOException: Unable to delete file: D:\MyProject\backend\mymodule\MyModuleJAR\build\install\MyModuleJAR\log\my.log
        at org.gradle.util.GFileUtils.deleteDirectory(GFileUtils.java:142)
        at org.gradle.api.internal.file.copy.SyncCopyActionDecorator$SyncCopyActionDecoratorFileVisitor.maybeDelete(SyncCopyActionDecorator.java:84)
        at org.gradle.api.internal.file.copy.SyncCopyActionDecorator$SyncCopyActionDecoratorFileVisitor.visitDir(SyncCopyActionDecorator.java:73)
        at org.gradle.api.internal.file.collections.jdk7.Jdk7DirectoryWalker$1.postVisitDirectory(Jdk7DirectoryWalker.java:108)
        at org.gradle.api.internal.file.collections.jdk7.Jdk7DirectoryWalker$1.postVisitDirectory(Jdk7DirectoryWalker.java:51)
        at org.gradle.api.internal.file.collections.jdk7.Jdk7DirectoryWalker.walkDir(Jdk7DirectoryWalker.java:51)
        at org.gradle.api.internal.file.collections.DirectoryFileTree.walkDir(DirectoryFileTree.java:143)
        at org.gradle.api.internal.file.collections.DirectoryFileTree.visitFrom(DirectoryFileTree.java:127)
        at org.gradle.api.internal.file.collections.DirectoryFileTree.visit(DirectoryFileTree.java:117)
        at org.gradle.api.internal.file.copy.SyncCopyActionDecorator.execute(SyncCopyActionDecorator.java:58)
        at org.gradle.api.internal.file.copy.NormalizingCopyActionDecorator.execute(NormalizingCopyActionDecorator.java:52)
        at org.gradle.api.internal.file.copy.DuplicateHandlingCopyActionDecorator.execute(DuplicateHandlingCopyActionDecorator.java:42)
        at org.gradle.api.internal.file.copy.CopyActionExecuter.execute(CopyActionExecuter.java:38)
        at org.gradle.api.tasks.AbstractCopyTask.copy(AbstractCopyTask.java:83)
        at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:75)
        at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.doExecute(AnnotationProcessingTaskFactory.java:227)
        at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.execute(AnnotationProcessingTaskFactory.java:220)
        at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.execute(AnnotationProcessingTaskFactory.java:209)
        at org.gradle.api.internal.AbstractTask$TaskActionWrapper.execute(AbstractTask.java:585)
        at org.gradle.api.internal.AbstractTask$TaskActionWrapper.execute(AbstractTask.java:568)
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeAction(ExecuteActionsTaskExecuter.java:80)
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:61)
        ... 57 more
Caused by: java.io.IOException: Unable to delete file: D:\MyProject\backend\mymodule\MyModuleJAR\build\install\MyModuleJAR\log\my.log
        at org.apache.commons.io.FileUtils.forceDelete(FileUtils.java:1390)
        at org.apache.commons.io.FileUtils.cleanDirectory(FileUtils.java:1044)
        at org.apache.commons.io.FileUtils.deleteDirectory(FileUtils.java:977)
        at org.gradle.util.GFileUtils.deleteDirectory(GFileUtils.java:140)
        ... 78 more


BUILD FAILED

下面是我的build.gradle脚本

apply plugin: 'java'
apply plugin: 'eclipse'

sourceCompatibility = 1.8
targetCompatibility = 1.8

eclipse {
    project {
        name = 'MyModuleJAR'
    }
}

repositories {

    mavenCentral()
}

dependencies {
    compile group: 'log4j', name: 'log4j', version: '1.2.17'
    compile group: 'org.apache.commons', name: 'commons-collections4', version: '4.1'
    compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.5'
    compile group: 'javax.mail', name: 'mail', version: '1.4.7'
}

def jarName = "${project.name}.jar"

def jarCopySpec = copySpec {

    def cfg = new Properties()
    cfg.put('jarname', jarName.toString())

    from(file("src/main/resources")) {
        include '*.properties'
    }

    from(file("src/dist")) {
        include 'deploy/*.xml'
    }

    from(jar) {
        rename jarName, "${cfg.jarname}"
    }

    into("lib") {
        from(configurations.runtime)
    }
}

jar () {

    manifest {
        def manifestClasspath = configurations.runtime.collect { "lib/" + it.getName() }.join(' ')
        attributes 'Implementation-Title': 'mymodule',
                'Built-Date': new Date(),
                'Built-JDK': System.getProperty('java.version'),
                'Main-Class': 'org.module.main.MyMainClass',
                'Class-Path': manifestClasspath
    }
}

task installApp(type: Sync) {
    description 'Installs MyModule application'
    into { file("${project.buildDir}/install/${project.name}") }
    with jarCopySpec
}

tasks.withType(JavaCompile) {
  options.compilerArgs << "-Xlint:all" << "-Xlint:-processing" << "-Xlint:-serial" << "-Werror"
}

我的log4j.properties文件具有以下配置

log4j.rootLogger=INFO, console, RollingAppender

log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.target=System.out
log4j.appender.console.immediateFlush=true
log4j.appender.console.encoding=UTF-8  
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.conversionPattern=[%p] %d [ %C{1} : %M : %L ] - %m%n%throwable{n}

#log4j.logger.org.apache=ERROR, RollingAppender
log4j.appender.RollingAppender=org.apache.log4j.DailyRollingFileAppender
log4j.appender.RollingAppender.File=./log/myLog.log
log4j.appender.RollingAppender.DatePattern='_'yyyy-MM-dd
log4j.appender.RollingAppender.layout=org.apache.log4j.EnhancedPatternLayout
log4j.appender.RollingAppender.layout.ConversionPattern=[%p] %d [ %C{1} : %M : %L ] - %m%n%throwable{n}

log4j.properties文件存储在以下路径

src/main/resources/log4j.properties

jar是在以下路径生成的

D:\MyProject\backend\mymodule\MyModuleJAR\build\install\MyModuleJAR\myModuleJAR.jar

日志是在下面提到的路径生成的

D:\MyProject\backend\mymodule\MyModuleJAR\build\install\MyModuleJAR\log\myLog.log

我的目标是实现

  1. 我的&#34; gradle installApp&#34; build命令应该能够覆盖 新代码更改的现有项目jar,我没有要求 显式关闭jar以重新构建它。

  2. 我的日志文件不应该被每个新鲜的gradle build删除     建立。相反,它应该附加由生成的日志语句     对现有日志文件的新构建。

0 个答案:

没有答案