views:

56

answers:

2

Hi,

First of all, I do have some sort of understanding that the following might not be the generally accepted way to do things.

We have a Maven 2 project that has a version number which should be updated each week or so, during a new release. During this process, I've tried to eliminate all the things one has to remember and I've made a bash script that handles the process interactively.

However, my problem is updating the pom version from the command line. I can do this with sed but I don't think it is very convenient. I was wondering if there is any maven plugin that would be able to modify the pom.xml directly from the command line. The version is set in the properties section of the pom. Would it be possible to write a plugin that would change the properties?

Thanks in advance.

Update

It seems that my issue was with project versions defined as properties (that were applied when filtering) which seems now a bit dumb.

One thing that I'm still looking for an answer is how to get the version of certain project reliably to the command line. Previously I had a "pretty unique" property that I got using grep, but now the <version> element is not unique as in child project there is at least two of these. I would need some sort of XML parser if Maven has no solutions, but my goal is to make the script as independent as possible.

I'm not sure if I should've created a new question from this, but I didn't. Getting the version is very closely related to the setting the version.

+3  A: 

I was wondering if there is any maven plugin that would be able to modify the pom.xml directly from the command line.

The Versions Maven Plugin can do this. Check the following goal:

  • versions:set can be used to set the project version from the command line, updating the details of any child modules as necessary.
Pascal Thivent
Thanks! I did find that plugin on my own but somehow missed the fact that the plugin can actually update the pom.xml and not just display dependencies and such. Setting the version does not work since the version is defined in the parent pom as a property, which I did not realize to mention. However, I found that there is a goal "update-properties" which might do the trick.
mkko
Apparently "update-properties" is only for updating dependencies and not setting any values. "versions:set" is the nearest match, but it seems that there is no way to define the version in the parent pom this way - or at least as a property.
mkko
@mkko: 1. You actually **must** hard code the version in the parent POM and in the `<parent>` elements of child modules, see [MNG-624](http://jira.codehaus.org/browse/MNG-624) for exhaustive discussions on this topic 2. Dependencies should use the built in property `${project.version}`, there is no valid reason to use a property for the version. That's how it works with Maven. 3. And then you'll be able to use `versions:set` (but this is an indirect consequence of getting things right).
Pascal Thivent
That I do already, it's fixed to some permanent value. I haven't found much use for the parent version since the actual core project is a child and has its own version. This core is something that can be deployed on its own and I think the parent version should not be used here. I have no idea how to reference a sibling project if the version is defined in the `<version>` of the child project. This is why the version is defined as a property, in the parent pom.
mkko
@mkko: `I haven't found much use for the parent version since the actual core project is a child and has its own version` If you don't need inheritance, don't use it. But if you have a parent element, you must hard code the parent version. `This core is something that can be deployed on its own and I think the parent version should not be used here.` You would just need to deploy the parent too, this is how it works, this is not an issue. `I have no idea how to reference a sibling project if the version is defined in the version of the child project` Use `${project.version}` as I wrote.
Pascal Thivent
I think I need the inheritance. I need the possibility to share common components, properties and functional tests with some projects. I would love to use the pom version for each of the projects instead of separate properties, but I have no idea what kind of project structure would help me achieve this. Basically I would have projects A, B, X and F. A and B would use the X and F would be the functional tests considering all the projects. Any suggestions? The F is a separate project since testing A and B requires the same resources and it is easier to run the integration tests.
mkko
Great, it seems that the value for `${project.version}` is retrieved from the child pom when filtering resources for that given child. This is actually what I needed. All that is needed is to specify the `artifactId` with the new version. I thought that the version would from the parent pom. One thing I don't understand is what is the parent version then used for. The only thing I can come up with is specifying the exact dependency to the parent in the child projects.
mkko
A: 

From Maven POM reference:

env.X: Prefixing a variable with "env." will return the shell's environment variable. For example, ${env.PATH} contains the PATH environment variable. Note: While environment variables themselves are case-insensitive on Windows, lookup of properties is case-sensitive. In other words, while the Windows shell returns the same value for %PATH% and %Path%, Maven distinguishes between ${env.PATH} and ${env.Path}. As of Maven 2.1.0, the names of environment variables are normalized to all upper-case for the sake of reliability.

That means that you can have an environment variable like $MYMAVENPROJECTVERSION and read it as this:

<version>${env.MYMAVENPROJECTVERSION}</version>

You can update this environment variable every week, before running build.

Hope this will help you.

SourceRebels
This won't work with multi-modules builds (you **must** hard code the version in the `<parent>` element).
Pascal Thivent
What does it means? You cannot use placeholders in parent section? its a bug?
SourceRebels
Exactly, you can't use placeholders in parent section. It's by design. Version less parent element will be allowed in Maven 3.1. See http://jira.codehaus.org/browse/MNG-624 and all the related issues for interesting discussions on this topic.
Pascal Thivent
Thanks for your explanation, I've never worked with multi-modules builds and it sounds strange for me that placeholders won't work :-)
SourceRebels
Tthe project is indeed a multi-module project. The version number is challenging since there are dependencies that specify the version also. The version is currently defined in the parent pom.xml as a property.
mkko