views:

433

answers:

3

I have an html file which loads an applet. The html needs to refer to the jar by name, and since maven names it based on the artifactid, version, etc, the html needs to be dynamically updated as the project evolves. It seems like resource filtering is the way to go, but I can't figure out what the variable to interpolate should look like. I'd like something along the lines of ${project.dependencies.myartifactid.version}, but that doesn't seem to be an option and I've had woeful luck googling.

A: 

From http://docs.codehaus.org/display/MAVENUSER/MavenPropertiesGuide:

${project.build.finalName} refers to the final name of the file created when the built project is packaged

Another option you have is to change set ${project.build.finalName} to a static string in your POM:

<build>
    <finalName>foo</finalName>
</build>
matt b
Yes, but the applet is very likely in a jar on which the war depends and this won't give you the name of the jar for the applet.
Pascal Thivent
Didn't realize the JAR was a dependency. Then yeah, this won't help.
matt b
+2  A: 

You'll need something like ${project.dependencies[0].artifactId} where 0 is the index of the dependency of the applet in your war module (see PLXUTILS-37). And indeed, using resources filtering should work.

Update: It appears that there is bug in the Maven Resources Plugin, this property doesn't get filtered as mentioned in this question. You may have to use the workaround suggested in this answer.

Pascal Thivent
Bingo! Hadn't found any indication that you could index the variables that way. Thank you sir. (Now if only I could identify it by name rather than index. Oh, well, this will certainly work.)
Erik Ackerman
@Erik Yes, it will be fragile (e.g. adding a dependency could break everything so this need to be documented) but sadly, I don't think there is anything better.
Pascal Thivent
+2  A: 

As I understand it you are trying to keep the version up to date, while expecting the rest to stay the same. There are two alternatives.

The first is to remove the version from the name so that the HTML need not change. You can see a practical example by searching for archiva-applet here: http://svn.apache.org/repos/asf/archiva/tags/archiva-1.3/archiva-modules/archiva-web/archiva-webapp/pom.xml

In this example, since you don't want the applet in WEB-INF/classes anyway, it is omitted from the webapp, and then included via the Dependency plugin:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-dependency-plugin</artifactId>
  <version>2.0</version>
  <executions>
    <execution>
      <id>copy</id>
      <phase>process-resources</phase>
      <goals>
        <goal>copy</goal>
      </goals>
      <configuration>
        <artifactItems>
          <artifactItem>
            <groupId>${project.groupId}</groupId>
            <artifactId>archiva-applet</artifactId>
            <version>${project.version}</version>
            <outputDirectory>src/main/webapp</outputDirectory>
            <destFileName>archiva-applet.jar</destFileName>
          </artifactItem>
        </artifactItems>
      </configuration>
    </execution>
  </executions>
</plugin>

If you are using Maven 2.1.0+ you can use the prepare-package phase and copy it straight to the output without modifying your source directory.

You then refer to the applet in HTML with the single name.

An alternative solution if you want to continue filtering and keep the version, is to use a shared property:

<properties>
  <applet.version>1.2.3</applet.version>
</properties>

...

<dependency>
  <groupId>my.group</groupId>
  <artifactId>my.applet</artifactId>
  <version>${applet.version}</version>
</dependency>

...

You can then use ${applet.version} in the HTML and still only have to change it in one place.

Brett Porter