views:

77

answers:

2

I want to generate an zip file that will update an application with maven. The zip will be hosted on a server and I am using the assembly plugin to generate the zip. However I would like maven to automatically generate a text file that stores the current version number outside the zip. Is this possible?

EDIT: I successfully did it using the maven Assembly Plugin and two descriptor to create two custom assemblies. One has a directory-single goal and it just creates a folder with the updated version.txt based on filtering. Then another one with a single goal actually packages the zip file. This seems to be very inelegant and I guess it will not properly up date the maven repo with the whole updated folder. If there is a better way to do this please let me know.

+2  A: 

What you are referring to is called filtering

You need to enable filtering on a particular resource, and then use ${project.version} which will be substituted as part of your build

Jon Freedman
+4  A: 

Sure. Create a text file somewhere in src/main/resources, call it version.txt (or whatever)

File content:

${project.version}

now in your pom.xml, inside the build element, put this block:

<resources>
  <resource>
    <directory>src/main/resources</directory>
    <filtering>true</filtering>
    <includes>
      <include>**/version.txt</include>
    </includes>
  </resource>
  <resource>
    <directory>src/main/resources</directory>
    <filtering>false</filtering>
    <excludes>
      <exclude>**/version.txt</exclude>
    </excludes>
  </resource>
  ...
</resources>

after every build, the file (which you can find in target/classes) will contain the current version.

Now if you want to move the file somewhere else automatically, you are probably going to need to execute an ant task via the maven-antrun-plugin.

Something like this:

  <plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.4</version>
    <executions>
      <execution>
        <phase>process-resources</phase>
        <configuration>
          <tasks>
             <copy file="${project.build.outputDirectory}/version.txt"
                   toFile="..." overwrite="true" />
          </tasks>
        </configuration>
        <goals>
          <goal>run</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
seanizer
For some reason the first block doesn't seem to put the version in target/classes. The second block of code seems to work if I create the version.txt.
tony
Is your project a webapp? Then resource filtering works a bit differently. I'll update my answer in a few hours (dinner first) :-)
seanizer
No but the project I am working on is just a maven module with a parent project that contains the whole application.
tony