'Change maven-shade-plugin outputFile from command line

I am using the maven-shade-plugin in my pom.xml and I want to be able to dynamically set the <outputFile /> from the command line, something like this:

mvn -outputFile=C:/Users/Oscar/Desktop/MyJar.jar

I don't want to hardcode a filepath directly in the pom.xml because I don't want it in the repo. On our production server, I want to be able to specify an output path for the shaded jar and on my local development machine I want to be able to specify my own path.

Is it possible to do something like this?

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.0.0</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <createDependencyReducedPom>false</createDependencyReducedPom>
                <relocations>
                    <relocation>
                        <pattern>com.zaxxer.hikari</pattern>
                        <shadedPattern>io.github.hornta.lib.hikari</shadedPattern>
                    </relocation>
                    <relocation>
                        <pattern>com.google.gson</pattern>
                        <shadedPattern>io.github.hornta.lib.gson</shadedPattern>
                    </relocation>
                    <relocation>
                        <pattern>org.slf4j</pattern>
                        <shadedPattern>io.github.hornta.lib.slf4j</shadedPattern>
                    </relocation>
                    <relocation>
                        <pattern>org.flywaydb.core</pattern>
                        <shadedPattern>io.github.hornta.lib.flywaydb</shadedPattern>
                    </relocation>
                </relocations>
                <outputFile>I NEED TO SET THIS PATH FROM COMMAND LINE (not having it in the repo)</outputFile>
            </configuration>
        </execution>
    </executions>
</plugin>


Solution 1:[1]

You can use a system property to define the value of outputFile

Change the line in your pom.xml

<outputFile>${outputFilePath}</outputFile>

And then use the variable in command line

mvn -DoutputFilePath=C:/Users/Oscar/Desktop/MyJar.jar ...

You can define the default value in the properties of your pom.xml

<properties>
    <outputFilePath>C:/Users/Oscar/Desktop/Default/MyJar.jar</outputFilePath>
</properties>

Solution 2:[2]

I would suggest using a properties file to set the value, and then add the properties file to your .gitignore. This is actually a pretty common use case, as values like this will vary across deployment environments. See this question or this article for discussion around the issue.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 m_kinsey