简体   繁体   English

依赖项的 Maven 项目变量

[英]Maven project variables for dependencies

I have an html file which loads an applet.我有一个加载小程序的 html 文件。 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. html 需要通过名称来引用 jar,并且由于 maven 根据 artifactid、version 等命名它,因此 html 需要随着项目的发展而动态更新。 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.我想要一些类似于 ${project.dependencies.myartifactid.version} 的东西,但这似乎不是一个选项,而且我在谷歌上搜索时运气不好。

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 ).您将需要类似${project.dependencies[0].artifactId} ,其中0是您的 war 模块中小程序依赖项的索引(请参阅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 .更新: Maven 资源插件中似乎存在错误,此属性未按此问题中所述进行过滤。 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.第一个是从名称中删除版本,以便 HTML 不需要更改。 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您可以通过在此处搜索archiva-applet来查看实际示例: 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:在这个例子中,因为无论如何你都不想要WEB-INF/classes的小程序,它从 webapp 中被省略,然后通过依赖插件包含:

<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.如果您使用 Maven 2.1.0+,您可以使用prepare-package阶段并将其直接复制到输出,而无需修改源目录。

You then refer to the applet in HTML with the single name.然后,您在 HTML 中使用单一名称引用小程序。

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.然后您可以在 HTML 中使用${applet.version}并且仍然只需要在一处更改它。

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

${project.build.finalName} This is by default defined as ${project.artifactId}-${project.version} ${project.build.finalName}默认定义为 ${project.artifactId}-${project.version}

Another option you have is to change set ${project.build.finalName} to a static string in your POM:您的另一个选择是将 set ${project.build.finalName}更改${project.build.finalName} POM 中的静态字符串:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM