1

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>
Jose Da Silva Gomes
  • 3,814
  • 3
  • 24
  • 34
  • You can do that with profiles (http://maven.apache.org/guides/introduction/introduction-to-profiles.html). Essentially you would put the "project default" version in the pom, and then each developer enables a profile that overrides that value for local work. Other answers based on property plugin or system properties also would work -- best choice depends on your specific situation. – Gus Mar 26 '18 at 21:11
  • What if I want my "project default" to be nothing? If I dont specify anything, I don't event want the to be in my pom.xml. –  Mar 30 '18 at 11:09
  • Then don't include `` in the pom at all. If that element is mandatory, it may refuse to build without an enabled profile that includes it. Worst case you set it to the plugin default (try `maven help:effective-pom` to see the verbose version of the project pom) – Gus Mar 30 '18 at 19:04

2 Answers2

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>
Jose Da Silva Gomes
  • 3,814
  • 3
  • 24
  • 34
0

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.

m_kinsey
  • 194
  • 10
  • Is it possible to add some logic like if I don't specify a property, it will default to its normal location which in my case is /target? –  Mar 26 '18 at 22:10