How to configure Dokka for package documentation using Gradle? - gradle

I am unable to configure Dokka to include the documentation of my packages without using a full absolute path from the root of the file system. I have in my Gradle file (the includes line is the one in problem):
dokka {
outputFormat = 'html'
outputDirectory = "$projectDir/../doc"
configuration {
// Use to include or exclude non public members.
includeNonPublic = false
// Do not output deprecated members. Applies globally, can be overridden by packageOptions
skipDeprecated = false
// Emit warnings about not documented members. Applies globally, also can be overridden by packageOptions
reportUndocumented = true
// Do not create index pages for empty packages
skipEmptyPackages = true
includes = ["${projectDir}/app/src/main/kotlin/com/biexpertise/simplewebview/packages.md"]
}
}
When, I run ./gradlew dokka if I have this error:
> Task :app:dokka FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:dokka'.
> org.codehaus.groovy.runtime.GStringImpl cannot be cast to java.lang.String
If I remove $projectDir and use an absolute path, things are working. It is possible to use a relative path instead?

So that's in fact a problem with dokka's Gradle plugin, or to be more specific, with Groovy String implementation. Groovy uses it's own GStringImpl for strings, instead of the String class, which leads to a problem when casting a List<GStringImpl> to List<String> (this cast doesn't succeed).
The easiest solution is to call .toString() on your include, like this:
includes = ["${projectDir}/app/src/main/kotlin/com/biexpertise/simplewebview/packages.md".toString()]
This should be fixed on the dokka side though, you can file a bug report on GitHub

Related

How do you access gradle.ext properties in Plugin Java source code?

I need a property to (one) be available when a plugin is applied and (two) allow for a calculated override value in the settings.gradle file. A project property would be ideal as it can have a default set in gradle.properties:
# gradle.properties
myProp=originalValue
This is great because it can be overrode with a command line argument like -PmyProp=newValue, but I was not able to find a good way to override the property in the settings.gradle file before the build.gradle executes (i.e. before the plugins are applied).
For instance all of these leave rootProject.myProp unaltered at plugin application:
// settings.gradle
rootProject.getProperties.put("myProp", "overrideValue")
settings.ext.myProp = "overrideValue"
settings.extensions.myProp = "overrideValue"
gradle.startParameters.projectProperties.myProp = "overrideValue"
We cannot do any magic in the build.gradle either because no logic can exist before the plugins block:
// build.gradle
plugins {
id 'com.myCompany.myPlugin' version 1.0.0 // 'myProp' must be set by now
}
One workaround I can think of would be to use:
// settings.gradle
gradle.ext.myProp = "overrideValue"
... but there doesn't seem to be a good way to access gradle.ext properties in Java source code (for a plugin), or is there?
This seems to work for the gradle.ext.myProp use case, but it is surprising to me that the only workable approach is to cast the Gradle object to an ExtensionAware object:
// MyPlugin.java
String myProp = (String) project.getRootProject().getProperties().getOrDefault("myProp", null);
Gradle gradle = project.getRootProject().getGradle();
if ((myProp == null) && (gradle instanceof ExtensionAware)) {
ExtensionAware gradleExtensions = (ExtensionAware) gradle;
myProp = (String) gradleExtensions.getExtensions().getExtraProperties().get("myProp");
}
It seems like what I'm trying to do should be commonplace, so is there a better way like solely using project properties?
If so, then how do you change the values in the settings.gradle file?
This is probably not what you’re looking for but maybe it still helps: have you considered an initialization script? In such a script it is possible to override a project property.
Example:
$ ./gradlew -PmyProp=originalValue properties | grep myProp
myProp: originalValue
$ ./gradlew -PmyProp=originalValue -I init.gradle properties | grep myProp
myProp: overrideValue
… where init.gradle is the following:
allprojects {
project.ext.myProp = 'overrideValue'
}
Note that there are also other ways of specifying the init script.

How to define Gradle project properties with Kotlin DSL

I'd like to check if two project properties are set and if not, set them to empty values in order to avoid a build failure. These properties are supposed come from ~/.gradle/gradle.properties (if configured).
The goal is to have credentials to a Maven repository in S3 defined in that local file. Every user has to put his own data there, but I want the build to just output a warning and continue if these are not set. Chances are high it will still be successful even without contacting S3.
I have got it running with Groovy DSL, but I am now switching to Kotlin and I just can't get the syntax right.
This is how ~/.gradle/gradle.properties looks like:
s3AccessKeyId=ABCDEFGHIJKLMNOPQRST
s3SecretKey=abcdefghijklmnopqrstuvwxyz1234567890abcd
And here are the relevant sections of the build.gradle.kts
if (!project.hasProperty("s3AccessKeyId") || !project.hasProperty("s3SecretKey")) {
logger.lifecycle("WARNING: s3AccessKeyId and s3SecretKey not set!")
project.extra["s3AccessKeyId"] = ""
project.extra["s3SecretKey"] = ""
}
repositories {
mavenLocal()
maven {
url = uri("s3://maven-repo.mycompany.com.s3.eu-central-1.amazonaws.com/")
credentials(AwsCredentials::class) {
accessKey = project.extra["s3AccessKeyId"].toString()
secretKey = project.extra["s3SecretKey"].toString()
}
}
}
No matter how I write the s3AccessKeyId="" lines, I am always getting the error:
Cannot get property 's3AccessKeyId' on extra properties extension as it does not exist
If all artifacts are found in the local Maven repository, I expect the build to work, even without gradle.properties. Only if some artifact is missing, the build should abort with some "credentials wrong" error.
As I said, it already worked with Groovy.
For that you need to use the properties attibute which is different from the extra like:
project.properties["s3SecretKey"].toString()
And then you need to have in your gradle.properties:
s3AccessKeyId=ABCDEFGHIJKLMNOPQRST
If the value is not there it might return null so you can do:
(project.properties["s3SecretKey"] ?: "default value").toString()
And it should work

How can I reference a Travis secure variable in my build.gradle?

One of my project dependencies sits on a private Bintray repo, which requires a username and password to access. Locally, I have these set in my gradle.properties:
bintrayUsername=<myUserName>
bintrayPassword=<myPass>
This works (locally), where hasProperty(X) resolves true and it uses this property:
allprojects {
repositories {
jcenter()
def mahBintrayUsername = hasProperty(bintrayUsername) ? bintrayUsername : System.getenv('bintrayUsername')
def mahBintrayPassword = hasProperty(bintrayPassword) ? bintrayPassword : System.getenv('bintrayPassword')
maven {
credentials {
username mahBintrayUsername
password mahBintrayPassword
}
url 'http://dl.bintray.com/my-repo-org/maven-private'
}
}
}
On Travis, I use secure variables so I don't have to expose these values in my public repo, but with the aim of being able to build directly from my public repo. When the build starts, you can see that the variables are exported:
Setting environment variables from .travis.yml
$ export bintrayUsername=[secure]
$ export bintrayPassword=[secure]
$ export TERM=dumb
...
FAILURE: Build failed with an exception.
* Where:
Build file '/home/travis/build/ataulm/wutson/build.gradle' line: 15
* What went wrong:
A problem occurred evaluating root project 'wutson'.
> Could not find property 'bintrayUsername' on repository container.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
I'm unsure how to reference the exported environment variables in my build.gradle such that they would be found.
I've checked this answer which doesn't seem to work (as above), as well as this comment which results in the same build failure.
The series of commits I've tried can be seen here, with the latest: https://github.com/ataulm/wutson/commit/9331b8d91b4acf11fd3e286ff8ba1a24ed527177
The error is a result of your ternary statement attempting to evaluate bintrayUsername as part of the condition.
The hasProperty() method takes a String argument so you should use hasProperty('bintrayUsername'), instead of hasProperty(bintrayUsername). Doing the latter will attempt to evaluate a property that may not exist, leading to the MissingPropertyException.
Simply remember that trying to evaluate any symbol that doesn't exist will typically result in a MissingPropertyException.
Below an example to define a project property -not a local variable- if it is not defined, by getting the value from the Environment, i.e. where Travis puts your secure variable.
project.ext {
if (! project.hasProperty('some_prop')) {
some_prop = System.getenv('some_prop')
}
}
If you never set it as a property (not using gradle.properties files) and you always want to set it from the environment, just remove the IF part. :)
Note: I wanted a project property so I can use it also to set values in my spring-boot YAML file... both locally and in CI.

Resolve a dependency dynamically inside a gradle task

I am trying to build a gradle plugin, which does the following:
As part of one its tasks, it creates a new configuration
It adds a DefaultExternalModuleDependency to this configuration - more specifically, it constructs a dependency to the application server zip file (available on Nexus). This information can be overridden by the invoking project as well.
Tries to resolve this newly added dependency and then unpacks the file to a local folder
All of this was working well when I had the details hard coded in a build file, but it looks like adding dependencies as part of a task are not treated the same way as having that information available at the parsing time.
So my question is, how do I get the project to reload the configurations / dependencies?
The code looks like the following:
#TaskAction
void installAppserver() {
Dependency dependency = new DefaultExternalModuleDependency(group,name,version)
Configuration configuration = project.configurations.detachedConfiguration(dependency)
configuration.setTransitive(false)
configuration.files.each { file ->
if (file.isFile() && file.name.endsWith('.zip')) {
println 'Attempting to unzip: ' + file + ' into folder: ' + appServerFolder
new Copy().from(project.zipTree(file)).into(appServerFolder).execute()
}
}
}
The problem is that the actual artifacts are not getting resolved!
A task can't configure the build model (that's what plugins do). It's fine to create and resolve a detached configuration in a task. If this doesn't work, there is likely a problem with the task's code, or the dependency it tries to resolve. Note that dependencies can only be resolved if the correct repository(s) are defined.
Instead of new DetaultExternalModuleDependency() (which is an internal class), project.dependencies.create() should be used. Instead of new Copy().execute() (Task#execute must not be called from user code), project.copy should be used.

OSGi bundle build issue in Gradle

I have a simple use case of building an OSGi bundle using Gradle build tool. The build is successful if there are java files present in the build path, but it fails otherwise.
I am using 'osgi' plugin inside the gradle script and trying to build without any java files. The build always fails with following error:
Could not copy MANIFEST.MF to
I am sure there must be some way to do it in Gradle but not able to fine. Any idea what can be done to resolve this depending on your experience.
I ran into this today as well, and #Peter's fix didn't work for me (I hadn't applied the java plugin in the first place...). However, after hours of Googling I did find this thread, which helped me find the problem.
Basically, it seems that the error occurs (as Peter stated) when no class files are found in the jar - my guess is because the plugin then cannot scan the classes for package names on which to base all the Import and Export information.
My solution was to add the following to the manifest specification:
classesDir = theSourceSet.output.classesDir
classpath = theSourceSet.runtimeClasspath
In my actual build code, I loop over all source sets to create jar tasks for them, so then it looks like this:
sourceSets.each { ss ->
assemble.dependsOn task("jar${ss.name.capitalize()}", type: Jar, dependsOn: ss.getCompileTaskName('Java')) {
from ss.output
into 'classes'
manifest = osgiManifest {
classesDir = ss.output.classesDir
classpath = ss.runtimeClasspath
// Other properties, like name and symbolicName, also set based on
// the name of the source set
}
baseName = ss.name
}
}
Running with --stacktrace indicates that the osgi plugin doesn't deal correctly with the case where both the osgi and the java plugins are applied, but no Java code is present. Removing the java plugin should solve the problem.
I had the same issue also when java code was present.
Adding these two lines to the osgiManifest closure fixed the problem:
classesDir = sourceSets.main.output.classesDir
classpath = sourceSets.main.runtimeClasspath
-- erik

Resources