开发者

Maven project variables for dependencies

开发者 https://www.devze.com 2022-12-20 04:23 出处:网络
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 pr

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 th开发者_如何学编程e 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.


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.


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.


https://cwiki.apache.org/confluence/display/MAVEN/Maven+Properties+Guide

${project.build.finalName} This is by default defined as ${project.artifactId}-${project.version}

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

<build>
    <finalName>foo</finalName>
</build>
0

精彩评论

暂无评论...
验证码 换一张
取 消