2

I've got three questions here. First, does pom.xml of maven have any difference from other xml file?

Second, if I would like to rename a file in the same directory as the pom.xml by scripts, can I do it in pom.xml? If possible, then how should I do it?

Last, how can I get the current time in pom.xml?

In fact, what I want to achieve is that I would like to backup the current package(.war) by appending it with the timestamps before I mvn package it. I want the whole process automated by the pom.xml instead of manually processing it.

Hi there,

I find a way to get the timestamp, will it work?

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactID>buildnumber-maven-plugin</artifactId>
  <execution>
  <phrase>package</phrase>
  <configuration>
    <format>{0, date, yyyy-MM-dd HH:mm:ss}</format>
    <items>timestamp</items>  
  </configuration>
  <execution>
</plugin>

will it work? if it works, how can I obtain the timestamp variable? $items or $timestamp?

Thanks

Newbie
  • 2,545
  • 6
  • 31
  • 38

2 Answers2

2
  1. The Maven pom.xml is just another XML file and there is no difference from others. It follows the same XML standards.

Now for Question 2 and 3, First of all, when you will run mvn clean, it will flush off all the files, including the war from the target folder.

You can run ANT tasks to rename and backup the current war, but it cannot be executed before clean. I would suggest you the following approach:

  • Run maven-antrun-plugin as a part of package goal.
  • In the ANT target, rename and backup the current packaged war.

Alternatively:
What I would suggest is: In the WAR pom.xml, specify the <version> element with the current timestamp and the desired version. This way, each war would be copied into different directories in the maven local repository.

PS: You cannot generate timestamp in maven as per my knowledge. Pass it through some external variable

Arpit
  • 6,280
  • 7
  • 35
  • 67
  • 1
    hi friend, running maven-antrun-plugin looks good good. but I can only some files, I don't the tag of rename. Can you provide it to me with an example illustrating its usage, please? Thanks in advance!! – Newbie Jun 08 '11 at 08:07
  • and please note that I have found a way to find the timestamps by an plugin. How can I embed it in the name of the file which is going to be backuped. – Newbie Jun 08 '11 at 08:10
  • 2
    Yes, you can only copy the artifact(`WAR`) after packaging. Need not move it. `` – Arpit Jun 08 '11 at 08:27
  • looks good! everything works fine except the method I found does not work. it just give me the xxx_${timestamp} instead of real time there. hm...what's wrong with the codes? – Newbie Jun 08 '11 at 09:24
  • the official website does not show much detail about it : http://mojo.codehaus.org/buildnumber-maven-plugin/create-mojo.html – Newbie Jun 08 '11 at 09:26
  • 1
    How are you calling your POM's ? You can pass the timestamp variable there itself ... `mvn clean install -Dtimestamp="08/05/2011 03:03:00"` – Arpit Jun 08 '11 at 09:34
1

Perhaps you should set the context for your questions to get better answers. Anyway, here goes.

  • pom.xml is an xml file, with its own schema. Thus it is different from other xml files.

  • There are a couple of ways to rename a file using maven. This SO discussion gives the options. One of them is as follows:

Essentially, you would have a pom with entries similar to the one in this example (external build.xml). Your ant build.xml would have entries similar to this:

<?xml version="1.0"?>
<project name="dummy">
    ...
    <target name="rename">
        <move file="file.orig" tofile="file.moved"/>
    </target>
    ...
</project>
  • You can use a special maven variable for timestamp as documented here. Refer to this SO discussion on how to use it.

    maven.build.timestamp The timestamp that denotes the start of the build. Since Maven 2.1.0-M1

Community
  • 1
  • 1
Raghuram
  • 49,195
  • 9
  • 101
  • 120
  • Thanks a lot! your explanation is detailed and helpful. It would be a great help if you can give me an example of codes showing me how to rename file, because the link you've just given is too difficult for me to understand. Thanks, – Newbie Jun 08 '11 at 07:04