Adding entries to a properties file before maven compile phase - maven

We are using maven filtering for properties file, in which we inject the build number and time from out Jenkins server.
It would be cool if we could add there properties to our app.local.properties file dynamically, thus not forcing each project maintainer to add them (one time operation, but annoying never the less).
I would like to
Add params to a properties file, before the filter plugin kicks in.
If the destination properties file does not exist -> create it and then add.
I need to edit the properties file in the target directory (the src/main/resources dir should remain untouched).
Nice to have:
Add configuration settings only if they aren't already defined in the file (to avoid duplicating entries added manually)
Is there some maven plugin / hook that I can tap into to do this?

you could build a custom maven build extension and retrieve those properties dynamically from a cetntral service and inject them into the POM in memory, right after the build process starts. Indeed that is how we solved this very problem in our company setup.
I've just posted a full recipe to this Stackoverflow answer

I see a conflict here:
Add params to a properties file, before the filter plugin kicks in.
So you need to edit the properties in src/main/resources, as filter plugin works on these, but at the same time:
I need to edit the properties file in the target directory (the src/main/resources dir should remain untouched).
What I understand is, you'd like to do this filtering somehow "on the fly". I don't think Maven mechanisms could do it; I'd do it completely with the Antrun plugin (excluding the property file from Maven handling) which I'd invoke in the process-resources phase. There's a propertyFile task for editing and saving property files; after saving your changes, you could process the file once more with the regexp task, for example, to apply your filters.

Related

How to skip a maven build step without modifying the pom itself?

We have a maven based Java EE project controlled by the customer. For internal reasons, we cannot execute one of the build steps, but the rest works fine and produces the jar we want.
Since editing the pom file would require taking care when committing to customer's SVN and copying the pom file would require taking care to sync changes comming from there, we are looking for a way to skip this specific step in the build section during the maven call itself, so to say mvn clean install but-leave-out-this-build-plugin-step, is there any?
Edit:
The plugin in question is the rpm-maven-plugin, which prevents the build from running on Windows. We found information how to make it work which won't really fit in our current setup. And since we cannot modify the customer's pom, I was looking for a way to trigger the skipping externally. But maybe there are other ways to just ignore/skip/fake this step?
It depends on what plugin you want to skip. Many plugins have ability to be skipped via system property (-Dblabla).
For deploy plugin it is -Dmaven.deploy.skip=true, for surefire -DskipTests=true.
Read plugin documentation, maybe you can find skip property
The rpm plugin hase a property disabled, unfortunately it is not accessible by a property. So, if setting this property in the customer's pom (or asking for editing it) with a default value of false is an option, this may be the solution.

How to make configuration read from an xml file available at configuration phase of gradle build?

I have a ton of ant projects which I want to migrate to gradle. All projects have in common a config.xml file. This file exists for reasons independent of the build, but I want to keep its information in a single place, so I evaluate it during the ant build as well.
I created a custom plugin with a PluginExtension and a ParserTask. The PluginExtension allows the user to provide the config.xml path as a propertie. The ParserTask locates the file automatically or using the provided path and parses the file if found. It then fills the remaining PluginExtension properties with the respective configuration data from config.xml.
This is working perfectly but has a major drawback: since parsing is done in a task, the respective properties of the PluginExtension are not filled with the information from config.xml during configuration phase of my build. This leads to all kind of smelly workarounds in my build file because I can't set the archives basename at configuration phase or configure the manifest in that phase, so I need to update this information during the execution phase.
How can I parse the config.xml during my build such that the properties are already filled with the correct values during configuration phase?
Moving the configuration information to a properties file and regenerating the config.xml during build time is NOT an option, because I need to maintain the ant build for a while as well. Additionally I need to config.xml file to be part of the version control.
Edit: despite having found an answer myself, I'm still interested in other responses, especially if they address the downside that I have left.
I now moved the parsing into the PluginExtension and called the parser from the PluginExtension Constructor. One downside is left. I can no longer configure where to search for the config.xml using that extension. Instead I have to rely entirely on file search. This is OK for my current use case.

Custom Maven Plugin: Programmatically Adding Source Directory to Project

As a Maven end-user, it is simple to add an additional directory to the list of source directories that will be compiled during the "compile" phase. I would use the build-helper-maven-plugin approach.
However, in my own custom plugin I would like to do this programmatically. My plugin will generate some java code. I would subsequently like to add the output directory (containing generated .java files) to the list of source paths.
At the moment I’m manually having to set the build-helper-maven-plugin config in all of my POMs to get the files I’m generating to be compiled.
Any pointers on what part of the Maven API to be looking at? My searches have only yielded queries from end-users, which are solved with the build-helper-maven-plugin approach.
To find my answer I took a look at the source code for the ANTLR maven plugin, which I know adds sources to the path. See AbstractAntlrMojo.
The solution is to add a MavenProject member variable to your Mojo with an expression to bind it to the project:
#Parameter(defaultValue="${project}")
private MavenProject project;
Once one has a reference to the project, it's a simple method invocation:
project.addCompileSourceRoot("<DIRECTORY-PATH-HERE>");
This will ensure that the new directories housing generated code will be compiled.

how to make checkstyle ignore xml files in my application

i've configured my checkstyle plugin with customized checkstyle.xml and it works fine with the java classes but having a lot of warnings with the xml files can anyone suggest how to disable the check style from any xml file in my application.
thanks in advance.
I can think of two ways to do that:
You could suppress findings from non-Java files. This works very well inside and outside of your IDE.
If you are using Eclipse, you can also configure Eclipse not to feed the non-Java files to Checkstyle. In order to do that, right-click your project and click Checkstyle. Uncheck use simple configuration. Specify a file set using \.java$ as regexp. You will see the list of files in the bottom half of the dialog window change to only Java files.
In earlier versions of Checkstyle, I think I remember that there also was a file name filter in the configuration XML, but I can't seem to find it in the docs now, so maybe that feature is no more.
There is a basedir property at the start of the Checker module in the check style config file. Uncomment it if it is commented.
Set it's value to the folder you want to apply your checkstyle rules to.
E.g. src folder of any eclipse project only contains java files.
<!-- If you set the basedir property below, then all reported file names
will be relative to the specified directory. See http://checkstyle.sourceforge.net/5.x/config.html#Checker -->
<property name="basedir" value="/MyEclipeProject/src"/>

In a maven project, is it possible to use a plugin just built within the same project?

Suppose you have some sort of input files which are to be processed in some custom way (even the file format is very particular to the package of files in question).
To process them I decide to make a maven plugin, which happens to be useful only in the context of these files.
Is it possible to have them all (the afore mentioned files and source code for the plugin) in one project, build the plugin, run the plugin over the input files and collect its output as the output of this project?
Short answer is you can't, because Maven resolves plugin classpath on start.
However you can create separate modules, first being a plugin and second would use that plugin to process files. Both modules can be invoked from parent pom.

Resources