java 依赖项的 Maven 项目变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2234124/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 20:11:45  来源:igfitidea点击:

Maven project variables for dependencies

javamaven-2filteringresources

提问by Erik Ackerman

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.

我有一个加载小程序的 html 文件。html 需要通过名称来引用 jar,并且由于 maven 根据 artifactid、version 等命名它,因此 html 需要随着项目的发展而动态更新。似乎资源过滤是要走的路,但我无法弄清楚要插入的变量应该是什么样子。我想要一些类似于 ${project.dependencies.myartifactid.version} 的东西,但这似乎不是一个选项,而且我在谷歌上搜索时运气不好。

回答by Pascal Thivent

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

你会需要这样的东西${project.dependencies[0].artifactId}哪里0是你的War模块中的小应用程序的依赖性的指数(见PLXUTILS-37)。事实上,使用资源过滤应该有效。

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.

更新:Maven 资源插件中似乎存在错误,此属性未按此问题中所述进行过滤。您可能必须使用此答案中建议的解决方法。

回答by Brett Porter

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-applethere: http://svn.apache.org/repos/asf/archiva/tags/archiva-1.3/archiva-modules/archiva-web/archiva-webapp/pom.xml

第一个是从名称中删除版本,以便 HTML 不需要更改。您可以通过在archiva-applet此处搜索来查看实际示例: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/classesanyway, 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-packagephase 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.

然后您可以${applet.version}在 HTML 中使用,并且仍然只需要在一个地方更改它。

回答by matt b

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

来自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

${project.build.finalName}指构建的项目打包时创建的文件的最终名称

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

您的另一个选择是${project.build.finalName}在 POM中将 set 更改为静态字符串:

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