Not able to pass the dynamic value from eclipse to POM - maven

Passing the version to POM.xml.
It shows warning. 'version' contains an expression but should be a constant.
I have to remove this.
I have one Ant project and another maven project. I made one runconfiguration for Ant project where I made one variable with some value suppose 13.2.1
by opting external tool configuration that variable come in list now.now able to pass that variable like -Dversion="${versionjar}".
its working perfactly.
since this versionjar is already set in eclipse. I want to use this while I want to run the maven project by runconfiguration.
want to pass in create manage and runconfiguration like in goal section install -Dversion="${versionjar}" but its not working .
I have to set this variable here also by add button.
How can I pass the variable from eclipse command line like :install -version="${version}"? version is vailable in eclipse variable list while "${version}" is properly send to pom when it is hard coded.
"${version}" is not resolving when send in goals section of run configuration
I'm facing problem when passing the eclipse variable from command line configuration.
in POM
<version>${version}</version>
<packaging>jar</packaging>

Related

Maven overwrite version tag

In my project's POM file, there are some tags
<groupId>foo.bar</groupId>
<artifactId>foobar</artifactId>
<version>X</version>
<description>foo bar foo bar</description>
specified. I'm doing some builds using Jenkins and I want to overwrite that X value. How can I do that?
As an example, I'm more familiar with MSBuild and there I can do something like
msbuild.exe project.sln ...some options... /p:AssemblyVersion=X
Is that AssemblyVersion that I'm talking about. Is this possible with maven?
Besides that, is this even a good practice? At the moment, I have version 1.0-SNAPSHOT. Should I manually change that value? How to perform a release?
To update version in pom files automatically from Jenkins, use -DnewVersion argument along with mvn command.
Example:
mvn versions:set -DnewVersion=someversion
During buildtime, if you'd like to update the buildnumber as your artifact version, then you can just pass that environment variable like -DnewVersion=${BUILD_NUMBER}.
Edit 1:
Credits to this SO thread updating-version-numbers-of-modules-in-a-multi-module-maven-project. It has a clear explanation.

How can i print the value of maven pre-defined variables directly inside pom file without using any third party plugin

I am working on a maven project and i want to see what ${project.basedir} contains ! As per all documentation it should be root path in which my project resides but what i am getting is rootPath/doc !
what can be wrong here ?

Using Maven property inside TeamCity Build Step

I would like to use a property, which I defined inside my pom.xml. Now I would like to refer to this property value inside my TeamCity Build Step.
At the moment I'm only able to refer the other way around to use a TeamCity property inside Maven.
In particular I want to do a SSH Deployer with a target like url/path/%maven.output.type%/something with
<properties>
<!-- Art der Entwicklung -->
<output.type>testing</output.type>
</properties>
What I tried was to define a parameter in TeamCity but I have no idea how to define the value of this parameter.
Is there any way to use this property inside the TeamCity build?
You can run a script that will set a teamcity parameter that you can use in a another build step. Here are the build configuration settings I use:
Create a configuration parameter with an empty text value with a name used in the next step (e.g. outputType).
Add a build step, with runner type Command line:
Select to run a custom script.
In the custom script field, enter a script that will extract the value from the pom file, and tell teamcity to set it in the parameter. For example:
testing=sed -n 's:.*<output\.type>\(.*\)</output\.type>.*:\1:p' pom.xml
echo "##teamcity[setParameter name='outputType' value='$testing']"
This will set the teamcity parameter outputType with the value of an element named output.type found in the current project pom file.
In another build step, you can use that parameter in a field like, for instance, the target field:
somepath/%outputType%

Is there a way to set the Maven version number dynamically?

I would like to use Maven to produce an artifact in zip format. To give you some background; my project includes an APS package (Application Packaging Standard, used to provision cloud applications on the Parallels platform). This package is a zip file that contains a combination of XML as well as PHP files. It is generated by an APS plugin from within Eclipse and its name always includes the version and release number of its contents.
What I am trying to do is generate a zip file with Maven that would be kind of a release candidate that will be eventually sent to customers and would include not only the actual APS package but also other files such as README, User Guide.pdf, etc;. I would like the name of this zip file to contain the version number of the version number of the APS package. Currently I can generate this manually by using something like "mvn -Dversion=1.2.3-4 package" but I would like to automate the process and ideally run this from Jenkins.
Basically, my strategy is to run a script that would extract the version number from the initial APS package, once that is done, my script can invoke Maven and can pass this parameter to it so it can generate the final zip with the proper version number. This is fine but again, I need to run this script manually and I am looking for an automated process.
My question is; is it possible to invoke this script from within Maven and use its return as a parameter to set the version name (or the name of the file that will be generated) at run time? As I mentioned, I would like eventually Jenkins to handle this. It can pick up the pom file but I am not sure how it could kind of "auto configure" itself to have the proper version number.
Thanks is advance.
From jenkins build you can use profile with ${BUILD_NUMBER}:
<profile>
<id>jenkins</id>
<build>
<finalName>${artifactId}-${version}-${BUILD_NUMBER}</finalName>
</build>
</profile>
Then run in jenkins:
clean install -Pjenkins
I use the SVN (or any source versioning system) version to identify the software builds.
By simply executing this
REVISION=`svn info | grep '^Revision:' | sed -e 's/^Revision: //'`
on the sourcers folder you get the right value in $REVISION, then you can use it for your maven build
mvn -Dversion=1.2.3-$REVISION package
easy and clean

Activate a profile based on environment

Here's my scenario:
Maven 2.0.9 is our build system
We install code to multiple environments
All of our environment-specific properties are contained in property files, one for each environment
We currently read these properties into maven using the properties-maven-plugin; this sub-bullet is not a requirement, just our current solution
Goal:
Perform certain parts of the build (ie. plugin executions) only for certain environments
Control which parts are run by setting values in the environment-specific property files
What I've tried so far:
Maven allows plugins executions to be put inside pom profiles, which can be activated by properties; unfortunately these must be system properties - ie. from settings.xml or the command-line, not from properties loaded by the properties-maven-plugin
If possible, we'd like to keep everything encapsulated within the build workspace, which looks something like this:
project
pom.xml
src
...
conf
dev.properties
test.properties
prod.properties
build-scripts
build.groovy <-- the script that wraps maven to do the build
install.groovy <-- ... wraps maven to do the install
Running a build looks like:
cd build-scripts
./build.groovy
./install.groovy -e prod
Is there any possible way to accomplish these goals with the version of maven we are using? If not, is it possible with a newer version of maven?
This isn't possible using just Maven. (See also How to activate profile by means of maven property?) The reason is that profiles are the first thing evaluated before anything else to determine the effective POM.
My suggestion is to write some preprocessor that parses your environment specific property files and converts them to the required system properties before launching Maven. This script can be included in your ~/.mavenrc so that it runs automatically before Maven is launched. Here is an example script that that assumes the properties file is in a fixed location:
properties=`cat /etc/build-env.properties`
while read line; do
MAVEN_OPTS="$MAVEN_OPTS -D$line"
done <<< "$properties"
If the properties file is not fixed, you'll just need to add something to the script to discover the location (assuming it is discoverable).

Resources