【问题标题】:Maven rename included fileMaven重命名包含文件
【发布时间】:2019-03-12 09:24:39
【问题描述】:

在构建jar 时,我想包含一个文件,重命名它并将其放在jar 中,但Maven 将重命名的文件放在目标文件夹中,而不是放在jar 中。如何重命名要包含在 jar 中的文件?

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>3.0.1</version>
            <executions>
                <execution>
                    <id>filter-resources</id>
                    <phase>process-resources</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}</outputDirectory>
                        <resources>
                            <resource>
                                <directory>src/main/resources</directory>
                                <includes>
                                    <include>../rest-api/src/main/resources/xcs-${xcs.rest.api.version}.yaml</include><!-- File I want to rename -->
                                </includes>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>com.coderplus.maven.plugins</groupId>
            <artifactId>copy-rename-maven-plugin</artifactId>
            <version>1.0</version>
            <executions>
                <execution>
                    <id>copy-and-rename-file</id>
                    <phase>compile</phase>
                    <goals>
                        <goal>rename</goal>
                    </goals>
                    <configuration>
                        <sourceFile>../rest-api/src/main/resources/xcs-${xcs.rest.api.version}.yaml</sourceFile>
                        <destinationFile>${project.build.directory}/xcs.yaml</destinationFile><!-- Desired name -->
                    </configuration>
                </execution>
            </executions>
        </plugin>

    </plugins>
</build>

【问题讨论】:

  • 首先,您自己将文件放在目标目录中,因为 ${project.build.directory} 是指目标目录。您可以使用 ${project.build.outputDirectory} ,它将文件放在 target/classes 文件夹下。如果仍然没有将重命名的文件放入jar,请查看您用于构建jar的插件配置。
  • @MadhavKumarJha 成功了!知道为什么它会移动我要重命名的文件而不是复制它吗?
  • 我在想的问题是:为什么你需要重命名它,因为结果应该总是xcs.yaml?那么为什么不把它放到项目中而不是重命名呢?还有一个有趣的事情:你为什么要使用来自不同模块的资源(基于../rest-api/..)?
  • @khmarbaise 我正在使用来自不同模块的文件,因为我想重用该文件中的 Swagger 配置。我在我的第二个 Swagger 配置中引用了这个文件,但是因为我不能在 Swagger 配置中使用 Maven 变量,所以我试图通过重命名它来从文件中删除版本。我不想硬编码文件名,然后在每次版本更改时替换文件引用。

标签: maven jar maven-3 maven-plugin


【解决方案1】:

您正在尝试使用错误的目标和执行 ID 复制和重命名文件。 试试这个:

<executions>
    <execution>
        <id>copy-rename</id>
        <phase>compile</phase>
        <goals>
            <goal>copy</goal>
        </goals>
...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-20
    • 1970-01-01
    • 1970-01-01
    • 2013-10-14
    • 2012-03-28
    • 2019-09-04
    • 2012-08-15
    • 1970-01-01
    相关资源
    最近更新 更多