【问题标题】:How to copy resources in maven in the jar of a different module如何在不同模块的jar中复制maven中的资源
【发布时间】:2022-02-02 12:29:56
【问题描述】:

我在一个java项目中有以下模块结构

-根
-- 主模块
-- 另一个模块

并且使用 maven-resources-plugin ,我试图将存在于 Main Module 中的一些资源复制到 Another Module

Main Module 的 pom.xml 中使用下面的 sn-p 我运行 mvn clean install,因此 basedir 是 Main Module 的基本目录。

<execution>
    <id>copy-resources-json2</id>
    <phase>process-resources</phase>
    <goals>
        <goal>copy-resources</goal>
    </goals>
    <configuration>
        <outputDirectory>${session.executionRootDirectory}/Another-Module/src/main/resources/config</outputDirectory>
        <resources>
            <resource>
                <directory>${basedir}/src/main/resources/config</directory>
            </resource>
        </resources>
    </configuration>
</execution>

虽然它确实在项目中本地复制资源,但问题是,当我检查 Another Module 的 jar 时,配置目录中不存在资源。

请问我做错了什么。

【问题讨论】:

  • 永远不要这样做!如果您需要另一个模块中的资源,请将它们移到另一个模块中...
  • @khmarbaise 我理解它的不好做法,但是在这种情况下我别无选择,关于如何进行复制有什么建议吗?
  • 没有选项?为什么它完全是错误的。会引起很多问题。并说明为什么需要将资源从一个模块复制到另一个模块...?

标签: java maven


【解决方案1】:

最好将 Maven 配置文件移动到您想要使用它们的模块中。

但如果你有这样的结构:

.
 |-- my-module
 |   `-- pom.xml
 `-- parent
     `-- pom.xml

也许你想用这样的相对路径来构建你的 pom.xml:

<project>
...
    <parent>
        <groupId>com.mycompany.app</groupId>
        <artifactId>my-app</artifactId>
        <version>1</version>
        <relativePath>../parent/pom.xml</relativePath>
    </parent>
...
</project>

您可以查看Maven docs中的pom结构示例。

【讨论】:

  • 相对路径在这里有什么帮助?除此之外,这也是不好的做法。将父元素移动到父目录,这样您就可以完全省略相对路径。 (遵循约定而不是配置!!)
猜你喜欢
  • 1970-01-01
  • 2011-11-24
  • 2016-12-03
  • 1970-01-01
  • 1970-01-01
  • 2017-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多