有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Change mavenshadeplugin从命令行输出文件

我在pom中使用maven shade插件。xml和我希望能够从命令行动态设置,类似这样:mvn -outputFile=C:/Users/Oscar/Desktop/MyJar.jar

我不想直接在pom中硬编码文件路径。xml,因为我不希望它出现在回购协议中。在我们的生产服务器上,我希望能够为着色jar指定一个输出路径。在我的本地开发机器上,我希望能够指定我自己的路径

有可能这样做吗

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

共 (2) 个答案

  1. # 1 楼答案

    可以使用系统属性定义outputFile的值

    更改pom.xml中的行

    <outputFile>${outputFilePath}</outputFile>
    

    然后在命令行中使用变量

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

    您可以在pom.xmlproperties中定义默认值

    <properties>
        <outputFilePath>C:/Users/Oscar/Desktop/Default/MyJar.jar</outputFilePath>
    </properties>
    
  2. # 2 楼答案

    我建议使用属性文件来设置值,然后将属性文件添加到.gitignore。这实际上是一个非常常见的用例,因为这样的值在部署环境中会有所不同。关于这个问题的讨论,见this questionthis article