Maven settings.xml to include build tag properties - maven

According to http://maven.apache.org/settings.html we have an example and explanation of all the elements.
But I want, for example, customize some build settings for ALL projects, not to be pushed in every project pom.xml.
Other people looking for this, too, but still no solution.
Maven ignoring build segment in settings.xml?
But, surprisingly, build tag is NOT available in settings.xml.
Is there a way to inject build parameters, like target dir into settings.xml using profiles, etc?

Two things:
The standard way is to create a company parent POM that is used as parent for all the projects. It can configure plugins and other build information.
If you want things be overridden by the settings.xml, use properties and then define the properties in the settings.xml.

Related

Is it possible to see the actual pom.xml executed, including parent dependencies/plugins?

I need to extract a project from a repository which uses several layers of parent projects. Every parent project adds some dependency or plugins or properties. This is becoming a nightmare as I'm not able to build any more the project, once I've manually added pieces from parent projects.
Is there a way to create a list of all dependencies/plugins/properties which are linked by a single pom.xml so that I can build a portable, single Maven project?
Thanks
You can create the effective pom (https://maven.apache.org/plugins/maven-help-plugin/effective-pom-mojo.html) that is a kind of merge with all parent POMs.
This is useful to understand the complete list and configuration of plugins.
Whether this helps you to build a "portable" Maven project, I don't know. Without the appropriate Maven repositories with all the plugins, dependencies and so on, Maven will not build.
Base concept of Maven is CoC (Convention over Configuration). Maven has a SuperPOM and all model is inherited from that. SuperPOM is located in maven-model-builder jar. Here is the source https://maven.apache.org/ref/3.6.2/maven-model-builder/super-pom.html
Each Maven goal is using a merged model called effective pom. The help plugin has effective-pom goal which displays the full model including parent model(s) and SuperPOM.
So the answer is just run: mvn help:effective-pom command to see actual model.

Maven: Is it possible to have more than one settings.xml files?

I have a multimodule project. All of the modules, but one are different. There are some things in the settings.xml file that I want to be different in the one module than from the rest.
Is it possible to have two settings.xml file and use them for different modules?
Think of the settings.xml as the configuration for your installation of maven. It determines the behavior of maven across it's use in your various projects.
This being said, if an individual project, i.e. a pom, requires something unique, it should be in that pom.
I think the thing to remember is that the project should be able to build on an individual dev's machine without any special intervention. In other words, the ideal case is that a given pom can successufl execute mvn install in a vanilla environment. So, don't put something in it that requires tweaking for a dev to get it to work. Also don't put anything in your settings.xml that enables a project to build, but then puts the burden on other devs to know what secrets are in your settings.xml.
You can set up different things in your individual pom files. What's in the pom files will override what's in settings. For instance, if your child poms sets up different repositories they'll be used over what is defined in settings.xml. Settings.xml is the default is nothing else applies. Depending on exactly what you want to do you might also take a look at the profiles feature.

Modern Maven Pom Templates

Every time I make a new proper project using Maven hosted on Github I have to go look at either one of my own old projects and copy the pom file or I go find a project that I think does a good job and copy there POM file. Then I have to go search and replace things like project name... etc.
Now Maven has a solution to this through archetypes but I have yet to see one that is modern enough such that it:
Uses the release plugin and deploys to SonaType Central Maven reop.
Connects to Github (meaning the scm connections and release plugin work do the right thing)
Makes all three jar artifacts (sources, javadoc, and regular jar)
I have contemplated make some giter8 templates but was hoping somebody already did something like this (most of the g8 templates are for sbt).
You can use com.jcabi:parent:pom which does exactly what you need and many more. It deploys to Sonatype, defines common dependencies with versions, pre-configures most popular plugins, and defines a few useful profiles.
This article explains more: Don't Repeat Yourself in Maven POMs; Use Jcabi-Parent
You could have a look at the parent pom released by Sonatype. It's intended to be used as a parent pom for projects that deploy to oss.sonatype.org (which may or may not be promoted to Maven Central).
When the sonatype-oss-release profile is enabled, it will ensure that sources and javadocs are built. It also includes an example of the <scm> pom element.
It turns out its incredible easy to create your own maven archetype.
Just make a generic project with stuff you like to use
In the project directory run mvn archetype:create-from-project
Generally Maven guesses the right things to make variable but if not you just edit the Velocity templates.
Install your archetype locally with mvn install
To use your new archetype: mvn archetype:generate -DarchetypeGroupId=com.mygroup -DarchetypeArtifactId=my-archetype
Now the only caveat is that there is not very good doc on the web that I could find on the archetype system. Like its unclear what variables you have available to you for Velocity (although most of them are obvious).
http://maven.apache.org/archetype/maven-archetype-plugin/create-from-project-mojo.html

In Maven is there a way to replace properties in the POM file itself like Ant's build.properties file?

I'm not talking about the Maven Properties Plugin, or resource filtering. I want Maven to replace properties in the POM itself. For example, let's say I have several POMs that reference a particular dependency's version number. I want to be able to load in one properties file for each of those POMs, and then change the values once. Maven would then read the properties file before processing the POM and replace accordingly. I believe this is how Ant does it in a single pass, and the properties are then immutable within the build.xml.
You can do property replacement on the command line if that fits your situation. I change the version of my project in the pom on the commandline (really through NAnt scripts). For example:
mvn deploy -Dproject.version=1.00.00.0-Something-SNAPSHOT
You mentioned you want to change a property that is within the parent tags. In maven 2 (not sure about maven 3) anything within the parent tags cannot be a property and must be the actual values. Maven doesn't evaluate properties with in the parent tags.
You are asking about a multi-module build which often comes to the point of the version number. But the simple answer for that is: Use the maven release plugin so you don't need to deal with the version numbers. Or you can use the versions-maven-plugin to handle modifications of them in an elegant way. And it's best practive not to use properties for versions of parents in pom's (onr that it will work as you described).

Adding default maven build configuration on Jenkins

I am setting up a CI system using Jenkins for Maven based projects. I was wondering whether there is a way to specify a build configuration which would be common for all the projects deployed on Jenkins.
For instance, I want all the projects to generate JavaDoc's hence I require the maven-javadoc-plugin in maven pom. As I understand, this can't be added to the settings.xml file. And I don't have access to the super pom. And editing the super pom isn't a good idea anyways.
What is the best way to add a common build profile for all the projects?
Corporate POM, that's what I was looking for.

Resources