Getting settings object from the buildscript - gradle

I am trying to access the settings object from the root project's build script.
The reason is I want to define a list in the settings.gradle file which will be a list of subprojects, kind of:
settings.gradle
projectNames = ['prjA', 'prjB']
Would like to do something like:
build.gradle (root project)
projectNames = settings.projectNames
// Use projectName in tasks
And then access it in build.gradle for various tasks, such as resolving those names into URLs to git-clone them. However I can't seem to find a way to declare some arbitrary groovy object which is visible between these two scripts. Note I may potentially like that list to be related but not equal to the project names. I guess the question sums up to sharing POGOs between those two files and accessing the settings object.
I'm pretty new to Gradle.

There isn't a way to get to the settings object from a build script. However, both scripts share a gradle object, which you could use to set an extra property in the settings script (e.g. gradle.ext.foo = "bar"), and read it in the build script (e.g. println gradle.foo).

If you need access to the Settings instance from your build.gradle file after the
settings have been loaded and evaluated, you can register a lifecycle closure or listener.
A great place to start is the method Gradle#settingsEvaluated(Closure)
that provides the Settings object as a closure parameter.

Related

Use variables from settings.xml but override with environment variables in Maven

I'm trying to use a property in my Maven POM file to specify the URL / username / password to a private repository. I would like to get the value for this property from a settings.xml file (from either the global, user or project settings file) but use the value from a specific environment variable to override any of that if it is set.
I've searched a bunch and I've found some references to using build profiles which doesn't seem to be quite what I need, but then again I'm not very fluent in Maven so I might just be missing something there. I feel like an idiot because I would think this is a pretty common use case.
In general I'm not too keen on defining secrets (passwords or API keys) in environment variables but for now we need to do that to avoid having to change the whole CI pipeline.

Execute action in Gradle after all plugins applied, but before "afterEvaluate()"

I try to configure my project from a custom plugin. I want to set a default URL and credentials for the publishing extension if plugin maven-publish is applied. I need know whether the plugin is applied or not.
In a function of my custom plugin I want to write something like this (not actual code)
project.afterPluginApply {
if (project.pluginc.any("maven-publish")) {
setDefaultUrl()
}
}
I don't want use project.pluginManager.withPlugin("maven-publish")
because I want see all applied plugins for the project (I want to configure many extensions and some value may change if whether or not other plugins are applied).
I don't want use subProject.afterEvaluate {} because, if I configured a field in build.gradle.kts I would overwrite it with my plugin.
I could write some dirty code like this
(it.pluginManager as DefaultPluginManager).idMappings[DefaultPluginId.unvalidated("maven-publish")] != null
But I'd rather like to find a better solution.

Display gradle property in readme.md file

I have a gradle project with a gradle.properties file. One of the properties displays the current version of my project and I would like to include this proprerty in the project's README.md on github. How can I do this?
The Gradle Copy task is capable of such functionality. Simply use its expand method to specify the values to insert. Of course you'll need to define a template somewhere in your project:
task copy(type: Copy) {
from 'src/templates'
into "$buildDir"
include 'projectinfo.html.template'
rename { file -> 'projectinfo.html' }
expand(project: project, title: 'ProjectInfo', generated: new Date())
}
I took this example from a post of Mr. Hakis Blog.
This functionality is based on the Groovy SimpleTemplateEngine. Of course you can simply use this class or any other templating engine to implement the required functionality in your build script on your own.
#KrispyK,
I believe this would be possible. You could write a simple serverless script using webtask (or similar service) that reads your gradle properties file and creates a custom status badge using a badge service like shields.io. Finally, you would only need to add this badge to your markdown file.
Please refer to this webtask script that I created. This calls an external API and uses the data returned by that API to create a custom Shields badge.
I've then used this badge in my readme file.
Hope this helps.

Maven plugin: copy a file content into another file

I am looking for the appropriate plugin to copy a file content into another file.
My resource.xml has content like this:
<class>my.path.ResourceA</class>
<class>my.path.ResourceB</class>
<class>my.path.ResourceC</class>
and must be copied to destination.xml at the place of ${content}:
<aaa>some info</aaa>
${content}
What the proper maven plugin to do that task, please?
Thank you in advance.
Nic
In general with Maven, you must first thinkg WHAT you want to do. Might seem weird at first sight, but as Maven is an opinionated build tool, it will generally be not simple to do weird/workaround things ;-).
Here, you would need two things:
load that file into a "content" property (before resource filtering starts by binding to an early phase, obviously, see the next point)
just activate resource filtering and be done
Unfortunately, there's no well-known/standard plugin for the (1) able to load a file inside a property.
A possible way, before rewriting it through a dedicated plugin or so, would be by using antrun-maven-plugin (through LoadFile task?) or gmaven plugin to load that file into a property (during the initialize phase, for example, so that it happens before process-resources, see the documentation about lifecycle.)
Then, for (2), you simply have to activate filtering (see the standard documentation of the maven-resources-plugin).
The answer from Baptiste does not work with the antrun plugin, as the properties from the LoadFile task are Ant properties are not accessible in Maven for filtering.
Instead, one can use the readfiles-maven-plugin, which you can find here: https://github.com/chonton/readfiles-maven-plugin

Team City 7.1 Configuration Parameters across Build Configurations

I would like to define a variable on a TC configuration and would like to get its value on a different TC configuration that has a trigger dependency on the first one.
Is it possible to do this?
If both configurations are in the same project, you can define a build configuration template that contains the shared parameter (i.e. variable). Both active configurations must then reference that template -- use the 'Associate with Template' action to accomplish this. The value of the shared parameter must be set in the template in order for the same value to be known to both configurations.
If you're trying to share build numbers (or other system-level parameters) between the configurations, there is a special method for that.
I have to use something like this to actually dynamically change the configuration parameters.
Invoke-WebRequest -Uri "http://build/guestAuth/action.html?add2Queue=bt876&name=env&value=test&name=env.number&value=%target.env.number%" -Method "Get" -Verbose

Resources