您好,我尝试从 pom.xml 中获取工件版本号,以便在这个典型的“关于和许可证信息”窗口中使用它。本质上,我实现了 Retrieve version from maven pom.xml in code 中给出的公认解决方案,并且为了解决其中一条评论中提出的有关过滤其他资源文件的问题,我将版本信息分离为一个目录和所有其他内容都位于常规位置。在 pom 文件中,它看起来像这样:
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>src/main/resources/versions</directory>
<filtering>true</filtering>
<includes>
<include>**/version.properties</include>
</includes>
</resource>
</resources>
我使用 Eclipse 中的
mvn clean javafx:run
构建这个
好消息:它有效。
坏消息:具有意想不到的副作用:在我的 target/classes 目录和 target/classes/versions 中,我都有 version.properties 文件。 target/classes/versions 中的仍然包含原始
version=${project.version}
语句,而 target/classes 中的则包含预期的 version=1.0.0
。那么我为什么要抱怨呢?我只是
期望 target/versions 中包含一个包含 version=1.0.0
语句的文件。
有人可以解释一下为什么我在目标树中获取这两个文件,以及是否有一种更干净的方法,这样我就像现在一样,在 src/main/resources 分支中的某个位置拥有一个属性源文件 - 不会干扰与其他资源文件 - 并且只有一个生成的属性文件位于目标树中的某个位置,具有 pom 工件版本 id?
提前谢谢您。
也许您需要整个 pom?
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.aname</groupId>
<artifactId>sifuSays</artifactId>
<version>1.0.0</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<repositories>
<repository>
<id>jcenter</id>
<name>jcenter-bintray</name>
<url>https://jcenter.bintray.com</url>
</repository>
</repositories>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>libraries-bom</artifactId>
<version>26.19.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- JavaFX -->
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>19</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>19</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-media</artifactId>
<version>19</version>
</dependency>
<!-- JAXB API only -->
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>4.0.3</version>
<scope>runtime</scope>
</dependency>
<!-- Framegrabber to get image from movies -->
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv</artifactId>
<version>1.5.9</version>
</dependency>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>ffmpeg-platform</artifactId>
<version>6.0-1.5.9</version>
</dependency>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>flandmark</artifactId>
<version>1.07-1.5.5</version>
</dependency>
<!--https://mvnrepository.com/artifact/com.google.cloud/google-cloud-texttospeech -->
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-texttospeech</artifactId>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>src/main/resources/versions</directory>
<filtering>true</filtering>
<includes>
<include>**/version.properties</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>11</source>
<target>11</target>
<release>11</release>
</configuration>
</plugin>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.8</version>
<executions>
<execution>
<!-- Default configuration for running -->
<!-- Usage: mvn clean javafx:run -->
<id>default-cli</id>
<configuration>
<mainClass>de.aname.sifuSays.App</mainClass>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>de.aname.sifuSays.AppLauncher</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.5.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<outputFile>${dir}/${project.artifactId}.jar</outputFile>
<createDependencyReducedPom>false</createDependencyReducedPom>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>de.aname.sifuSays.AppLauncher</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
您链接的问题链接到解释如何执行此操作的 Maven 文档
将那些不应过滤的文件放入 src/main/resources 中,将其他文件放入 src/main/resources-filtered 中
因此,过滤版本属性的源应该位于 src/main/resources-filtered 下,而不是 src/main/resources (或该目录下的任何目录)。
您还需要调整 pom 中的资源定义以匹配此答案中提到的名称,链接的 Maven 文档逐步告诉您如何执行此操作。