Reputation: 9465
Note, that this question may be partially answered by solving the specific problem, but solving the general problem is also appreciated.
I need to pass command-line options to Gradle code. Gradle doesn't have any sensible way to do it, so I need to extract them somehow from System
properties and later to parse them. I was trying to employ org.apache.common.cli
package for this. But I cannot import it into the build code.
This is roughly what it looks like:
buildscript {
repositories { ... }
dependencies {
classpath group: "commons-cli", name: "commons-cli", version: "1.2"
}
}
...
org.apache.commons.cli.DefaultParser
And I'm getting this:
* What went wrong:
Could not compile build file 'somewhere/build.gradle'.
> startup failed:
build file 'somewhere/build.gradle': 35: unable to resolve class org.apache.commons.cli.DefaultParser
@ line 35, column 1.
import org.apache.commons.cli.DefaultParser
I have no idea what went wrong or how to debug this. I've also tried Groovy's groovy.util.CliBuilder
, but this breaks with even more arcane errors.
So, to solve my immediate problem: I'd be happy to find a simple way to parse command-line arguments passed to Gradle's build.
Is there any way to debug Gradle code? By debugging I don't mean appending --debug
argument to it's command line options. It just prints a ton of irrelevant nonsense, but tells you nothing about what it was trying to do and what went wrong. I mean something like an actual debugger.
Is there a way to know what dependencies it was trying to load, and did it succeed at loading them, and if so, where did it load them from?
Is there any way to translate the "Gradle language" into something that can be analyzed by a linter / editor s.t. one would know what parts of it do / if it makes sense to add other parts of certain kind (what I mean is some sort of intellisense to make understanding problems like unmet dependencies easier)?
Upvotes: 0
Views: 2088
Reputation: 24468
This post is asking several questions and is highly subjective (re: "doesn't have any sensible way"). I'll do what I can.
First, as a sanity check for buildscript, let's try Apache Commons Lang3:
import org.apache.commons.lang3.StringUtils
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath group: 'org.apache.commons', name: 'commons-lang3', version: '3.4'
}
}
println "TRACER : " + StringUtils.class.simpleName
The above script works, but with Gradle 2.14.1, I can confirm that DefaultParser
from Apache Commons CLI 1.3.1 (as an aside, it's not in v 1.2) does not work. I suspect this is related to this bug. The link mentions dom4j
but it may be that cli
is in the same category.
Regarding command-line arguments, the Gradle documentation contains examples of using -P
, -D
, gradle.properties
, and so on. If you have a specific parsing issue, please create a new question with specifics.
Regarding dependencies, the following commands may be of use:
gradle dependencies
gradle buildEnvironment
see this post for the latter.
Upvotes: 1