Maven plugin: copy a file content into another file - maven

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

Related

How do you set the --frames option for a Javadoc task in Gradle?

We recently updated to JDK 11 and the javadoc team has seen fit to remove frame generation from the javadocs in favor of a search box. Our community is comprised heavily of students who don't know what keywords to search for, but who can browse a sidebar and find what they need. For them a search box is useless. They require discovery through browsing. For that reason, we would like to turn frames back on by adding the --frames option to the javadoc task, and then we'll just never move off of JDK 11.
We are using Gradle 7.4.2 and there doesn't appear to be a method exposing the --frames option in the StandardJavadocDocletOptions class.
If the options section of my Javadoc task looks like this:
options.memberLevel = JavadocMemberLevel.PROTECTED
options.links "https://developer.android.com/references"
options.encoding = 'UTF-8'
Then I see those options in gradle's generated options file.
Adding...
options.setStringOption('-frames')
...does not result in any new option appearing in the generated options file. To be honest I have no idea what setStringOption() without the second string parameter actually does.
Adding...
options.setStringOption('-frames', '')
... results in --frames '' appearing on the generated options file, but that confuses javadoc.
I don't see anything in the Gradle documentation that indicates how one might add simply '--frames' to the command line of the javadoc executable. Or any other option that javadoc might expose, but the gradle version one is sitting on does not expose for that matter.
To set flags use addBooleanOption()
// build.gradle.kts
tasks.javadoc {
options {
require(this is StandardJavadocDocletOptions) // workaround https://github.com/gradle/gradle/issues/7038
addBooleanOption("frames", true)
}
}
Note that the Kotlin DSL requires a workaround https://github.com/gradle/gradle/issues/7038#issuecomment-448294937

How to make maven plugin get properties from the pom file, the project that uses it?

I am writing a maven plugin. I need the following functionality. So that my plugin can read properties from the properties section of the project file that my plugin will use. Is it real?
Primary goal:
The data that is needed for my plugin to work can be passed through the <-Configuration-> section, but I need the ability to pass parameters through the command line when calling my plugin.
Unfortunately, the same parameter will be specified in both the <-configuration-> section and on the command line, then the maven, in priority, will take values from the <-configuration-> set.
I need that, even if the value is specified in the <-configuration-> section, it will return the value that will be passed on the command line.

Maven: How to test that generated documents in target folder are properly generated?

I've got a Java/Maven project that uses RestDocs to generate documentation of our endpoints.
For the uninitiated of RestDocs, what it does is monitor tests during the build and use information from the tests to generate "snippets" that get used to create API documenation using asciidoc during the mvn package phase.
The problem I'm facing is that if the asciidoc (.adoc) file is referencing a non-existent snippet, the doc gets generated and will say something like:
"Unresolved directive in myDoc.adoc - include::{snippets}/error-example/response-fields.adoc[]"
Since this happens after the tests, I don't know how to test for something like this so that the build can stop and let the developer know that they need to update either the .adoc or the snippet.
Any guidance would be much appreciated.
Adding this to the configuration of the asciidoctor plugin fails the build when a snippet is not found that the .adoc is expecting:
<logHandler>
<outputToConsole>true</outputToConsole>
<failIf>
<containsText>include file not found</containsText>
</failIf>
</logHandler>

Load different stylesheets when packaging a war file with Grails

I want to package a Grails application for different brands. While generating a war file, I want to pass some custom parameter that refers to a certain brand and styles the application by loading the stylesheet for the brand.
I read online and one approach I found was with maven. I tried working with maven but I am stuck while initially compiling the application. The error is
Failed to execute goal org.grails:grails-maven-plugin:2.2.1:maven-compile
(default-maven-compile) on project foreAction: Forked Grails VM exited with error.
I am stuck as to what approach to take now. I searched for the above error and tried different solutions but nothing seems to work.
If there is a different way without using Maven I am willing to give it a shot.
You could always hook into the events using scripts/_Events.groovy and replace the appropraite CSS/assets. The documentation explains how to hook into build events.
Your code inside scripts/_Events.groovy might look something like this:
// This is called after the staging dir is prepared but before the war is packaged.
eventCreateWarStart = { name, stagingDir ->
// place your code here that copies your resources to the appropriate location in the staging directory.
Ant.copy(
file: System.getProperty('somePassedInFile'),
toFile: "${stagingDir}/assets/stylesheets/whatever.css",
overwrite: true
)
}
Then you can pass in the value of the source file from grails prod war like this:
grails prod war -DsomePassedInFile=/path/to/file.css
Hope this helps at least give you an idea of how you can accomplish this. (All written off the top of my head so beware of typos etc.)

Property Replacement in Maven Site Content

I'm generating maven site content using the site plugin. I want to have a little table that shows my maven group id, artifact id, parent info, etc on the module. I don't see a plugin for it, so I was going to use the APT format and create a content page with a table for it. The documentation says I can use property replacement in the site descriptor via ${project.name} etc. This fails when i use it in both the descriptor and in my index.apt file. Has anybody seen this done or know how to do it?
The mvn site command generates a Project Summary page which has this information on it. Does that work for you?
For example, when I execute mvn site, get an output here target/site/project-summary.html where the page has a section like so:
Build Information
Field Value
GroupId com.a.b
ArtifactId myapp
Version 1.01.13-SNAPSHOT
Type jar
If you want to create pages which use placeholder like ${project.version} etc. you need to use index.apt.vm (velocity files) wher the replacement works. But for the information you have asked the solution which has been described (project-summary.html) is the right way cause it's automatically generated.

Resources