Maven filtered properties file

70 Views Asked by At

Hi I tried to get hold of the artifact version number from my pom.xml to use it in this typical about & license information window. In essence I implemented the accepted solution given in Retrieve version from maven pom.xml in code, and in order to adress the concern, raised in one of the comments, about filtering the other resource files, I seperated the version information in one directory and all the rest, in the conventional location. In the pom file this looks like this:

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

I build this using mvn clean javafx:run from within Eclipse

Good news: It works.

Bad news: With unintended side effects: Both, in my target/classes directory and in target/classes/versions I have the version.properties file. The one in target/classes/versions still contains the original version=${project.version} statement and the one in target/classes the expected version=1.0.0. So why do I complain? I only expected the one file in target/versions containing the version=1.0.0 statement.

Can somebody please explain why I get the two files in the target tree, and whether there is a cleaner way, such that I have, like now, one properties source file somewhere in the src/main/resources branch - without interferring with the other resource files - and only one resulting properties file somewhere in the target tree, having the pom artifact version id?

Thank you in advance.

Maybe you need the entire 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>
2

There are 2 best solutions below

0
jewelsea On

The answer to the question you linked links to the Maven documentation that explains how to do this

put those files into src/main/resources which should not filtered and the other files into src/main/resources-filtered

So the source for your filtered version properties should be under src/main/resources-filtered instead of src/main/resources (or any directory under that).

You also need to adjust the resources definition in the pom to match the names mentioned in this answer. The linked Maven documentation tells you how to do that, step by step.

0
Rob Spoor On

Right now the versions directory is included by both <resource> elements. That's why it shows up twice. It's also the reason why the file in target/classes/versions is unfiltered (processed by the first <resource> element) and the file in target/classes is filtered (processed by the second <resource> element).

I haven't tried it, but if you exclude the versions folder from the first <resource> element you will probably get the result you want - only one copy of the version files in target/classes with filtering applied. If you instead want to put the files in target/classes/versions, read jewelsea's answer