Maven project variables for dependencies

11,382

Solution 1

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.

Solution 2

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: https://github.com/apache/archiva/blob/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.

Share:
11,382
Erik Ackerman
Author by

Erik Ackerman

Husband, father, coder, podcaster.

Updated on June 04, 2022

Comments

  • Erik Ackerman
    Erik Ackerman almost 2 years

    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.

  • Devanshu Mevada
    Devanshu Mevada over 14 years
    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.
  • matt b
    matt b over 14 years
    Didn't realize the JAR was a dependency. Then yeah, this won't help.
  • Erik Ackerman
    Erik Ackerman over 14 years
    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.)
  • Devanshu Mevada
    Devanshu Mevada over 14 years
    @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.
  • JayZee
    JayZee over 9 years
    actually this works only until maven antrun plugin version 1.3. From 1.4 onward ${project.dependencies[0].artifactId} is not interpolated anymore.