Replace Maven properties in pom.xml via command line - java

I want to replace a Maven property in a pom.xml file with a command line call via some Maven plugin.
<properties>
<my.property>ISO-8859-1</my.property>
<properties>
Do you know of a plugin which is able to do that?
The versions plugin takes very long since it checks whether some dependency is available. Besides that it doesn't work in my case.

All you need to do is add the following when running maven on the command line
-Dmy.property=propertyValue

Related

Maven project version interpolation from a property file

We are using a Maven for a while in our project and want to automate the release process little bit. we came up with the following idea so that the version will be maintained by developers or in SCM instead of in DevOps tool like jenkins/bamboo.
Anyone following below process instead of setting the interpolation value in arguments as "mvn install -Dapp.version=1.0.0-SNAPSHOPT"
The process we like to follow is to supply the Maven project version through an external property file.
let's assume the following partial POM.xml excerpt as example.
<project>
<groupId>com.home.diary</groupId>
<artifactId>journal</artifactId>
<version>${app.version}</version>
<packaging>war</packaging>
</project>
let's assume i have an version.properties file in my SCM with following content
app.version=2.0.0-RELEASE
while running the mvn goal
mvn install
i want the artifact generated as
journal-2.0.0-RELEASE
I tried using plugin properties-maven-plugin from org.codehaus.mojo
as discussed here How to read an external properties file in Maven
but it's not working.
Anyone did this? could you please share your implementation/ideas?
This is not possible.
First of all: Why not just manage the version in the <version> tag itself? It is the easiest thing and fulfils your requirement (the developer manages the version in the SCM).
If you don't want this, you need to supply the version either in the POM itself or through the command line. Reading external properties with something like the properties maven plugin will always happen too late, i.e. after the version tag is already read.

Update Project version automatically in Maven 3.6.0

I am using Maven 3.6.0 in a project where the version of my project is set using a revision property as described in https://maven.apache.org/maven-ci-friendly.html and I want to automatically update the project's version in command line.
I tried to look on the side of maven release plugin (https://maven.apache.org/maven-release/maven-release-plugin/update-versions-mojo.html) using the release:update-versions command, which seems to work, but instead of updating the property revision it updates the version property.
Current setup :
<version>${revision}</version>
<properties>
<revision>1.1-SNAPSHOT</revision>
</properties>
When running mvn release:update-versions :
<version>1.2-SNAPSHOT</version>
<properties>
<revision>1.1-SNAPSHOT</revision>
</properties>
Expected:
<version>${revision}</version>
<properties>
<revision>1.2-SNAPSHOT</revision>
</properties>
Following is the explanation why this is happening:
When mvn release:update-versions is executed then it will bump the version by first fetching value of revision property. This command does not care about updating property value as well.
In general, property value in maven can be updated using -D flag within command line as follows:
mvn release -Drevision=1.2-SNAPSHOT
Following links could also be helpful:
https://maven.apache.org/maven-release/maven-release-plugin/examples/non-interactive-release.html
https://maven.apache.org/maven-release/maven-release-plugin/update-versions-mojo.html
Finally, I achieved to get the same result by using maven versions plugin, but it's not using ${revision} tag.
mvn versions:set -DnextSnapshot=true
Versions plugin is much more flexible than release plugin for managing your project's version.

maven release plugin - manipulating project release version

I'm trying to configure in Jenkins a maven release build with customized release version that includes the branch from which the release was made.
It looks something like this:
release:prepare -DreleaseVersion=${project.version}-${GIT_LOCAL_BRANCH}-Release release:perform
everything works fine, except that the 'project.version' placeholder, which calculated based on the pom, contains the '-SNAPSHOT' postfix.
is there other placeholder which I can use to get it without the '-SNAPSHOT'?
I know that the maven release plugin, by default, will set the right version - only I want to manipulate that value.
See the offical docu https://maven.apache.org/maven-ci-friendly.html.
There are 3 properties maven supports since 3.5: ${revision}, ${sha1} and ${changelist}. So you can use something like
<version>${revision}${changelist}</version>
...
<properties>
<revision>1.3.1</revision>
<changelist>-SNAPSHOT</changelist>
</properties>
And call it with
release:prepare -DreleaseVersion=${revision}-${GIT_LOCAL_BRANCH}-Release
But the maven-release-plugin does not work nicely together with the CI friendly versions. Read: it will probably overwrite your placeholders in the version tag. So in the end you might just manipulate the -SNAPSHOT string via bash commands.
is there other placeholder which I can use to get it without the '-SNAPSHOT'?
Unfortunately I believe the answer to your question is "no, there is no built-in variable that will satisfy your needs". However, you could try creating a property and then using that property inside of your project's version tag.
I am not able to test this as I don't have the appropriate environment set up and it's rather late so I'm not going to have the time to set it up right now. I will outline my idea and perhaps it will help you:
You could do something like this in your POM:
<version>${artifactReleaseVersion}-SNAPSHOT</version>
<properties>
<artifactReleaseVersion>0.0.1</artifactReleaseVersion>
</properties>
Then run your goals using that property:
release:prepare -DreleaseVersion=${artifactReleaseVersion}-${GIT_LOCAL_BRANCH}-Release release:perform

How to add jar file dependency in pom.xml

I have an application that depends on 2 jar file :
OperatorInterface.jar
Operator.jar
I want to build my project using Maven structure. I've followed the Maven site tutorial to create the structure like so:
mvn -B archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DgroupId=source.app -DartifactId=project
I've put my source file FileProcess.java in E:\project\src\main\java\source\app.
FileProcess has dependency in 2 external .jar files but I don't know how to define this dependency in pom.xml.
Here is the content of the pom.xml so far:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>source.app</groupId>
<artifactId>my-app</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>my-app</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Can anyone help me?
First thing you need to install your jars in your maven local repository. You can follow the official tutorial here:
https://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html
During the installation you will have to choose your own artifactId, groupId and version. You can choose whatever value you want but remember them because you will need those to include your jars in the pom.
After that you can include them in your pom.xml adding these lines under the tag dependencies for each library to include:
<dependency>
<groupId>your_group_id</groupId>
<artifactId>your_artifact_id</artifactId>
<version>your_version</version>
</dependency>
In the case when the JARs in question are your own code rather than third-party, the best approach is to "mavenize" the projects that build them as well. After you do that, running mvn install on those projects will place them in your Maven local repository where they will be available to other local Maven projects who declare them as dependencies.
Avoid adding them to yourpom.xml file straight. When you add .jars using this process, they will automatically reflect in your pom.xml
Steps are -
1. Right click on your project in the file explorer in your eclipse.
2. Go to build Path option.
3. Select configure build path
4. Chose the Libraries tab in the window that appears.
5. Add your .jar file externally.
6. Click ok and come back to your project interface
Update your maven project by pressing Alt+F5 and restart eclipse. Your problem should be solved.
I would take the following route since this is for corporate use. This is the hard and ultimately portable way that sets you up for future Maven usage as it is intended to be done.
1) make those dependent jars Maven projects (because then you can easily version-manage them too using Maven)
2) use a local repository manager and deploy your own projects to it using Maven release management through either the mvn:release plugin, or use a build server such as Hudson to automate the release process with a simple button press which I can highly recommend setting up.
https://maven.apache.org/repository-management.html
http://maven.apache.org/maven-release/maven-release-plugin/
3) mvn:release the dependency jars to your local repository manager so they will be available for other Maven projects
4) you're actually done, when you have a local repository where your deploy your own snapshot and release artifacts to, then your maven build can find your own maven modules and include them in the application dependencies - if you don't forget to configure the repository in the project's pom of course. And your build server if you have one can find them too.
The easy/lazy route is as suggested to manually install the jars in your local .m2 folder where Maven caches dependencies that it downloads, but it is absolutely not a portable solution that will stand the test of time. It won't work when somebody else needs to work on this project until they too install the jars locally. Or if its only you, you need to redo it every time you checkout the project on another computer / as another user. Also you need to update the jars each and every time you make changes to them, everywhere the project is checked out. You may need to do specific setup steps to get it working in an IDE, should you inevitably choose to start to use one.
However if you are having a time-pressure problem, then I would certainly go ahead and do that as a temporary workaround solution to be able to get going.

Passing Maven Compiler Options From The Command-line

I am setting up several projects on a continuous integration server, some of which I don't have access to change the source code to, The server is a linux box, I am running into a problem where maven encoding needs to be changed to UTF8 to be able to compile on the box. Since I don't have access to modify the pom file, I was wondering if I can pass the compiler options as a command-line param? The project uses maven compiler 2.0 and I tried passing -Denconding=UTF8 without success.
You can use the Maven property "project.build.sourceEncoding".
So something along the lines of
mvn clean install -Dproject.build.sourceEncoding=UTF-8 should accomplish what you need.
This is equivalent of
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
in your pom.xml.
Edit: As a point of reference, there is the following link available POM Element for Source File Encoding showing the nuances between these properties for both Maven 2.0 and 3.0

Categories