How to replace a property in a POM in a Maven/Jenkins job? - maven

I have a project with client and server components. Both have their own Maven multi-module build projects.
The correct server version must be referenced in various frontend modules. To accomplish this I set a property in my client parent POM like this:
<properties>
<server.version>1.2.3</server.version>
</properties>
Now I'd like to update the version number in the POM (i.e. not just injecting a different version from the command line with -D...) during/after a Jenkins build job. Is there a way to do this?

I found the com.google.code.maven-replacer-plugin:replacer Maven plugin which works perfectly for my case. It accepts an xpath and a regex to define what to replace in an XML file.
Example plugin configuration:
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
<version>1.5.3</version>
<configuration>
<file>${project.basedir}/pom.xml</file>
<xpath>/project/properties/server.version/text()</xpath>
<token>^.*$</token>
<value>${newServerVersion}</value>
</configuration>
</plugin>
And the plugin can be run for each affected maven module:
mvn --non-recursive replacer:replace -DnewServerVersion=xxxx

At itembase we use Jenkins Pipeline Plugin. It comes with some useful built in functions like for example readMavenPom and writeMavenPom. So in your build pipeline you could do something like:
def pom = readMavenPom file: 'pom.xml'
//Do some manipulation
writeMavenPom model: pom

Did you check maven versions plugin?
(Tbe website is shutdown but the plugin page is still avaiable in google cache)
mvn -DnewVersion=<version> versions:set

Related

Why do I need to add Spring Boot plugin but I don't need to add install plugin in Maven pom.xml?

I'm learning about Maven and I want to ask you why do I need to add the Spring Boot Maven plugin in the pom.xml if I want to run it?
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
But I don't need to add the install, deploy, compiler, clean and all other plugins. How does this plugins come in my project?
So I need to add this code in the pom.xml if I want to use this command: mvn spring-boot:run, but I don't need to add any code for these commands: mvn install:install, mvn compiler:compile, etc. I don't understand how. Thank you!
Compile, install, test, etc is part of the default maven lifecycle, specified in default-bindings.xml in your maven installation. This lifecycle configuration connects lifecycle stages (for instance compile) with plugins (maven-compiler-plugin), and is inherited by all your maven projects.
The spring-boot-maven-plugin on the other hand is not part of the any default lifecycle, and thus needs to be specified in each project.

How to find the version of a Maven plugin in the project?

I'm trying to find the version of maven-wagon plugin that's being used in my project. Is there a way to find the version of a used plugin via command line?
There are several ways to do this:
1) Check the dependency tree:
To find out the libraries and versions you are using you can use the Maven dependency tree, just execute this where you have your project (pom.xml):
mvn dependency:tree -Dverbose
This is useful detect which version of an specific library your project is using, but I think it doesn't include plugins.
2) Describe the specific plugin:
If you want to know what version of an specific plugin you have installed you can do this:
mvn -Dplugin=: help:describe
mvn -Dplugin=org.codehaus.mojo:wagon-maven-plugin help:describe
This shows you something like this:
Name: Maven Wagon plugin
Description: Maven plugin that can be used to access various operations on a
given URL using a supported maven wagon. Supports recursive upload, download,
and list directory content functionality.
Group Id: org.codehaus.mojo
Artifact Id: wagon-maven-plugin
Version: 1.0
Goal Prefix: wagon
This plugin has 11 goals:
...
...
3) Check the effective pom:
Execute this:
mvn help:effective-pom
and go through the pom looking for the plugin you need to clarify, there you will find something like this:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>wagon-maven-plugin</artifactId>
<version>1.0</version>
</plugin>

How to force Maven to always create a new jar file?

If all classes are up-to-date "Nothing to compile - all classes are up to date"
so will maven create jar again?
As I am seeing in my log that jar is not creating again. so maven come to know that all classes are up-to-date.
Question: is there any process or another thing which work on this?
The Maven Jar Plugin will create a jar via its jar goal if none exists or skip its creation if existing but nothing changed.
You can force the creation of the jar via its forceCreation option (since version 2.2). From official documentation:
Require the jar plugin to build a new JAR even if none of the contents appear to have changed. By default, this plugin looks to see if the output jar exists and inputs have not changed. If these conditions are true, the plugin skips creation of the jar. This does not work when other plugins, like the maven-shade-plugin, are configured to post-process the jar. This plugin can not detect the post-processing, and so leaves the post-processed jar in place. This can lead to failures when those plugins do not expect to find their own output as an input. Set this parameter to true to avoid these problems by forcing this plugin to recreate the jar every time.
Its default value is at false, which explains the behavior you are having.
If you want to force it always, you can add to your pom file:
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<forceCreation>true</forceCreation>
</configuration>
...
</plugin>
</plugins>
</build>
...
</project>
Or just on a single build, invoke it as following:
mvn package -Djar.forceCreation=true
So, going back to your question:
is there any process or another thing which work on this?
The answer is: Yes, the Maven Jar Plugin works on this and the option above will change its behavior.

mvn clean tomcat:run command

When I run "mvn clean tomcat:run" (without specifying any tomcat version) command from command prompt for running my web application, it download tomcat 6.0.29 version dependency as shown below:
org/apache/tomcat/juli/6.0.29/juli-6.0.29.pom
org/apache/tomcat/annotations-api/6.0.29/annotations-api-6.0.29.pom
org/apache/tomcat/catalina-ha/6.0.29/catalina-ha-6.0.29.pom
org/apache/tomcat/coyote/6.0.29/coyote-6.0.29.pom
org/apache/tomcat/tribes/6.0.29/tribes-6.0.29.pom
org/apache/tomcat/jasper-el/6.0.29/jasper-el-6.0.29.pom
org/apache/tomcat/dbcp/6.0.29/dbcp-6.0.29.pom
pom.xml file of the application does not contain any tomcat version it require to run
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
<finalName>esa</finalName>
</build>
So my query is how does it decide to download particular this tomcat version dependency.
Probably the default settings of the tomcat plugin you run.
Try running mvn tomcat:help -Ddetails to see what version of the plugin you use, and how it can be configured.
I think it is an earlier version of the plugin, and you can now use explicit versions, such as
mvn org.apache.tomcat.maven:tomcat6-maven-plugin:2.0:run
mvn org.apache.tomcat.maven:tomcat7-maven-plugin:2.0:run
(or the shorter form)
Seems, you are running the tomcat-maven-plugin from codehaus, whihc has tomcat 6.0.29 built-in. (Seems there was no activity since 2010.)
You should try the tomcat7 plugin from apache.
Regards
Tibor
In command line for maven use --debug option to get explanation of build process. For our case output looks like:
...[DEBUG] Resolving plugin prefix tomcat from [org.apache.maven.plugins, org.codehaus.mojo]
...
[DEBUG] Resolved plugin version for org.codehaus.mojo:tomcat-maven-plugin to 1.1 from repository central (http://repo.maven.apache.org/maven2, releases)...
Actually to explain why we've got tomcat v1.1 without specifying anything about tomcat, remember that maven build process has been customized with build plugins. And each build plugins has own build plugins. So it is enough to examine effective pom file to get clear understanding that almost empty initial pom.xml has quite big effective pom.xml.
To overcome issue just use explicit version of the tomcat plugin.

mvn release plugin doesn't update included pom file

the maven release plugin doesn't update the pom files which are included:
<plugin>
<artifactId>maven-invoker-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
....
....
<configuration>
<projectsDirectory>.</projectsDirectory>
<streamLogs>true</streamLogs>
<pomIncludes>
<pomInclude>pomDisContainer.xml</pomInclude>
</pomIncludes>
how can i force the release plugin to update all my pom.xml files?
regards
Perhaps you are looking at this from the wrong direction. Would a better solution be to have the Maven Invoker Plugin update the invoked pom.xml files every time instead?
The Invoker plugin will automatically replace properties in the pom, providing they are using the alternative syntax:
#project.version#
Will be replaced by the invoking project's version.

Resources