【问题标题】:build/install/<project>-shadow directory -- what is it?build/install/<project>-shadow 目录——它是什么?
【发布时间】:2017-11-13 10:04:51
【问题描述】:

build/install/gradleHelloWorld-shadow 是什么?该目录中应该或不应该有什么?

最简单的“hello world”构建失败:

thufir@dur:~/NetBeansProjects/gradleHelloWorld$ 
thufir@dur:~/NetBeansProjects/gradleHelloWorld$ gradle clean runShadow

> Task :shadowJar 
A problem was found with the configuration of task ':shadowJar'. Registering invalid inputs and outputs via TaskInputs and TaskOutputs methods has been deprecated and is scheduled to be removed in Gradle 5.0.
 - No value has been specified for property 'mainClassName'.
The SimpleWorkResult type has been deprecated and is scheduled to be removed in Gradle 5.0. Please use WorkResults.didWork() instead.

> Task :startShadowScripts 
Using TaskInputs.file() with something that doesn't resolve to a File object has been deprecated and is scheduled to be removed in Gradle 5.0. Use TaskInputs.files() instead.


FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':installShadowDist'.
> The specified installation directory '/home/thufir/NetBeansProjects/gradleHelloWorld/build/install/gradleHelloWorld-shadow' is neither empty nor does it contain an installation for 'gradleHelloWorld'.
  If you really want to install to this directory, delete it and run the install task again.
  Alternatively, choose a different installation directory.

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

* Get more help at https://help.gradle.org

BUILD FAILED in 1s
5 actionable tasks: 5 executed

Publishing build scan...
https://gradle.com/s/t7jbmhjz23giw

thufir@dur:~/NetBeansProjects/gradleHelloWorld$ 

构建文件:

plugins {
    id 'com.gradle.build-scan' version '1.8' 
    id 'java'
    id 'application'
    id 'com.github.johnrengelman.shadow' version '2.0.1'
}

buildScan {
    licenseAgreementUrl = 'https://gradle.com/terms-of-service'
    licenseAgree = 'yes'
    publishAlways()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

mainClassName = 'net.bounceme.dur.gradle.hello.App'

shadowJar {
    baseName = 'greeter'
    classifier = null
    version = null
}

repositories {
    jcenter()
}

configurations {
    provided
}

dependencies {
} 

中止后的项目runShadow:

thufir@dur:~/NetBeansProjects/gradleHelloWorld$ 
thufir@dur:~/NetBeansProjects/gradleHelloWorld$ tree
.
├── build
│   ├── classes
│   │   └── java
│   │       └── main
│   │           └── net
│   │               └── bounceme
│   │                   └── dur
│   │                       └── gradle
│   │                           └── hello
│   │                               └── App.class
│   ├── install
│   │   └── gradleHelloWorld-shadow
│   ├── libs
│   │   └── greeter.jar
│   ├── scriptsShadow
│   │   ├── gradleHelloWorld
│   │   └── gradleHelloWorld.bat
│   └── tmp
│       ├── compileJava
│       └── shadowJar
│           └── MANIFEST.MF
├── build.gradle
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── settings.gradle
└── src
    ├── main
    │   └── java
    │       ├── dur
    │       └── net
    │           └── bounceme
    │               └── dur
    │                   └── gradle
    │                       └── hello
    │                           └── App.java
    └── test
        └── java

29 directories, 12 files
thufir@dur:~/NetBeansProjects/gradleHelloWorld$ 

有问题的 /home/thufir/NetBeansProjects/gradleHelloWorld/build/install/gradleHelloWorld-shadow 目录已被 clean 删除,从而确定 gradle 正在构建此目录。

刚升级的gradle:

thufir@dur:~$ 
thufir@dur:~$ sdk ls gradle
==== INTERNET NOT REACHABLE! ===================================================

 Some functionality is disabled or only partially available.
 If this persists, please enable the offline mode:

   $ sdk offline

================================================================================

--------------------------------------------------------------------------------
Offline: only showing installed gradle versions
--------------------------------------------------------------------------------
 > 4.3.1
 * 4.2.1
--------------------------------------------------------------------------------
* - installed                                                                   
> - currently in use                                                            
--------------------------------------------------------------------------------
thufir@dur:~$ 

(Wi-Fi 可能有点不稳定。)

【问题讨论】:

  • No value has been specified for property 'mainClassName'. 指定一个主类并进行干净的构建。
  • @LazerBanana 请参阅下面的解决方法。此外,在构建文件中指定了 mainClassName 属性——可能该错误与 shadowJar 插件本身有关。

标签: java gradle build.gradle uberjar shadowjar


【解决方案1】:

您的问题我不清楚,据我所知,错误消息很清楚出了什么问题。

不确定您发布的解决方法是什么,我在评论中说要运行干净,因为大概这是插件要求的,如果有兴趣,只需查看插件的源代码即可。

无论如何。

Shadow 插件还会在应用程序插件存在时配置分发任务。该插件将创建 shadowDistZip 和 shadowDistTar,它们分别创建 Zip 和 Tar 分布。每个发行版都将包含影子 JAR 文件以及启动应用程序所需的启动脚本。

此外,该插件将创建 installShadowDist 和 startShadowScripts 任务,这些任务将分发所需的文件暂存到 build/install/-shadow/。

另一件事是mainClassName

就像普通的 jar 任务一样,当应用程序插件被应用时,shadowJar 清单将被配置为包含 Main-Class 属性,其值在项目的 mainClassName 属性中指定。

runShadow 是 javaExec 任务可能需要配置?

runShadow {
  // classpath = sourceSets.main.runtimeClasspath

  main = 'net.bounceme.dur.gradle.hello.App'

  // arguments to pass to the application
  // args 'appArg1'
}

当与应用程序插件一起应用时,将创建 runShadow 任务以从影子 JAR 启动应用程序。 runShadow 任务是配置为执行 java -jar myproject-all.jar 的 JavaExec 任务。它可以像任何其他 JavaExec 任务一样配置。

ShadowJar Documentation

【讨论】:

  • 请再看一下构建文件。请考虑设置主类的错误与构建文件无关。查看其他错误以了解上下文。另外,请注意,当使用shadowJarassembleShadowDist 构建JAR 时,会设置主类入口点。因为为什么?因为它是在 gradle.build 中配置的......
  • 证明shadowJar 正确构建了一个JAR:gist.github.com/THUFIR/ed567bb3681370e7eeddacd04e544d88 但是,为什么runShadow 不起作用?
  • 是的,我看到了build.gradle,我想你误解了我的意图,RunShadow 是一个 JavaExec 任务,我认为你需要将 mainClass 放入 agrs。
  • 你的 gradle 版本是什么?我认为这是一个错误 - 请参阅我的新答案。
【解决方案2】:

解决方法:

thufir@dur:~/NetBeansProjects/gradleHelloWorld$ 
thufir@dur:~/NetBeansProjects/gradleHelloWorld$ gradle clean assembleShadowDist

> Task :shadowJar 
A problem was found with the configuration of task ':shadowJar'. Registering invalid inputs and outputs via TaskInputs and TaskOutputs methods has been deprecated and is scheduled to be removed in Gradle 5.0.
 - No value has been specified for property 'mainClassName'.
The SimpleWorkResult type has been deprecated and is scheduled to be removed in Gradle 5.0. Please use WorkResults.didWork() instead.

> Task :startShadowScripts 
Using TaskInputs.file() with something that doesn't resolve to a File object has been deprecated and is scheduled to be removed in Gradle 5.0. Use TaskInputs.files() instead.


BUILD SUCCESSFUL in 0s
6 actionable tasks: 6 executed
thufir@dur:~/NetBeansProjects/gradleHelloWorld$ 
thufir@dur:~/NetBeansProjects/gradleHelloWorld$ cd build/libs/
thufir@dur:~/NetBeansProjects/gradleHelloWorld/build/libs$ 
thufir@dur:~/NetBeansProjects/gradleHelloWorld/build/libs$ ll
total 20
drwxr-xr-x 2 thufir thufir 4096 Nov 13 02:53 ./
drwxr-xr-x 7 thufir thufir 4096 Nov 13 02:53 ../
-rw-r--r-- 1 thufir thufir 1492 Nov 13 02:53 greeter.jar
thufir@dur:~/NetBeansProjects/gradleHelloWorld/build/libs$ 
thufir@dur:~/NetBeansProjects/gradleHelloWorld/build/libs$ jar xf greeter.jar 
thufir@dur:~/NetBeansProjects/gradleHelloWorld/build/libs$ 
thufir@dur:~/NetBeansProjects/gradleHelloWorld/build/libs$ ll
total 28
drwxr-xr-x 4 thufir thufir 4096 Nov 13 02:53 ./
drwxr-xr-x 7 thufir thufir 4096 Nov 13 02:53 ../
-rw-r--r-- 1 thufir thufir 1492 Nov 13 02:53 greeter.jar
drwxr-xr-x 2 thufir thufir 4096 Nov 13 02:53 META-INF/
drwxr-xr-x 3 thufir thufir 4096 Nov 13 02:53 net/
thufir@dur:~/NetBeansProjects/gradleHelloWorld/build/libs$ 
thufir@dur:~/NetBeansProjects/gradleHelloWorld/build/libs$ cat META-INF/MANIFEST.MF 
Manifest-Version: 1.0
Main-Class: net.bounceme.dur.gradle.hello.App

thufir@dur:~/NetBeansProjects/gradleHelloWorld/build/libs$ 
thufir@dur:~/NetBeansProjects/gradleHelloWorld/build/libs$ java -jar greeter.jar 
Nov 13, 2017 2:53:38 AM net.bounceme.dur.gradle.hello.App run
INFO: helloooo
thufir@dur:~/NetBeansProjects/gradleHelloWorld/build/libs$ 
  1. 为什么会这样?
  2. 为什么runShadow 不起作用?

【讨论】:

    【解决方案3】:

    更好的解决方案,仍然是一种解决方法。从 4.3.1 切换回 gradle 4.2.1 可以:

    thufir@dur:~/NetBeansProjects/gradleHelloWorld$ 
    thufir@dur:~/NetBeansProjects/gradleHelloWorld$ sdk use gradle 4.3.1
    
    Using gradle version 4.3.1 in this shell.
    thufir@dur:~/NetBeansProjects/gradleHelloWorld$ 
    thufir@dur:~/NetBeansProjects/gradleHelloWorld$ gradle clean runShadow
    
    > Task :shadowJar 
    A problem was found with the configuration of task ':shadowJar'. Registering invalid inputs and outputs via TaskInputs and TaskOutputs methods has been deprecated and is scheduled to be removed in Gradle 5.0.
     - No value has been specified for property 'mainClassName'.
    The SimpleWorkResult type has been deprecated and is scheduled to be removed in Gradle 5.0. Please use WorkResults.didWork() instead.
    
    > Task :startShadowScripts 
    Using TaskInputs.file() with something that doesn't resolve to a File object has been deprecated and is scheduled to be removed in Gradle 5.0. Use TaskInputs.files() instead.
    
    
    FAILURE: Build failed with an exception.
    
    * What went wrong:
    Execution failed for task ':installShadowDist'.
    > The specified installation directory '/home/thufir/NetBeansProjects/gradleHelloWorld/build/install/gradleHelloWorld-shadow' is neither empty nor does it contain an installation for 'gradleHelloWorld'.
      If you really want to install to this directory, delete it and run the install task again.
      Alternatively, choose a different installation directory.
    
    * Try:
    Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
    
    * Get more help at https://help.gradle.org
    
    BUILD FAILED in 1s
    5 actionable tasks: 5 executed
    thufir@dur:~/NetBeansProjects/gradleHelloWorld$ 
    thufir@dur:~/NetBeansProjects/gradleHelloWorld$ sdk use gradle 4.2.1
    
    Using gradle version 4.2.1 in this shell.
    thufir@dur:~/NetBeansProjects/gradleHelloWorld$ 
    thufir@dur:~/NetBeansProjects/gradleHelloWorld$ gradle clean runShadow
    Starting a Gradle Daemon (subsequent builds will be faster)
    
    > Task :shadowJar
    The SimpleWorkResult type has been deprecated and is scheduled to be removed in Gradle 5.0. Please use WorkResults.didWork() instead.
    
    > Task :runShadow
    Nov 13, 2017 12:02:51 PM net.bounceme.dur.gradle.hello.App run
    INFO: helloooo
    
    
    BUILD SUCCESSFUL in 12s
    6 actionable tasks: 6 executed
    thufir@dur:~/NetBeansProjects/gradleHelloWorld$ 
    

    这是bug吗?

    【讨论】:

    • 某些运行时 API 可能更改了?
    • @LazerBanana 这真的让我头疼。我认为shadow 1.) 没有声明一个主要方法,或者没有正确配置它,并且 2.) 当它处于相反状态时,它期望这个目录为空或不为空。也许它在构建过程中被填充然后被删除?您可能比我更了解,但这是我有根据的猜测......而且,它可能在构建期间处于一种状态,但在构建完成后处于不同状态(填充或空)。我想你在某处提到了一些关于这个的东西……不过,你必须再读一遍。
    • 我不知道我只是想帮助阅读文档:)
    猜你喜欢
    • 2010-11-03
    • 2013-12-23
    • 2014-03-30
    • 1970-01-01
    • 2017-08-16
    • 1970-01-01
    • 1970-01-01
    • 2021-06-12
    • 1970-01-01
    相关资源
    最近更新 更多