【问题标题】:changing destination folder of resources in maven在maven中更改资源的目标文件夹
【发布时间】:2022-01-25 20:53:22
【问题描述】:

我从我的 pom.xml 中的doc 复制粘贴了以下 sn-p

  <plugin>
    <!-- <groupId>org.apache.maven.plugins</groupId>-->
    <artifactId>maven-resources-plugin</artifactId>
    <version>3.2.0</version>
    <executions>
      <execution>
        <id>copy-resources</id>
        <phase>validate</phase>
        <goals>
          <goal>copy-resources</goal>
        </goals>
        <configuration>
          <outputDirectory>${basedir}/resources</outputDirectory>
          <resources>                                        
            <resource>
              <directory>${basedir}/template</directory>
              <filtering>true</filtering>
            </resource>             
          </resources>   
        </configuration>            
      </execution>
    </executions>
  </plugin>

但当我尝试触发部署时收到以下消息:

$ mvn resources:copy-resources 
[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------------< org.test:myproject >------------------------
[INFO] Building myproject-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:3.2.0:copy-resources (default-cli) @ myproject ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.440 s
[INFO] Finished at: 2021-12-26T12:30:59+01:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:copy-resources (default-cli) on project myproject: The parameters 'resources', 'outputDirectory' for goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:copy-resources are missing or invalid -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginParameterException

这是我的目录结构:

./resources
./template
./template/test.txt
./pom.xml

如何更改资源的目标文件夹?为什么maven没有解析插件中的配置指令?

【问题讨论】:

  • 首先为什么要更改目标文件夹,因为资源目录旨在打包到生成的工件中?你想完成什么?
  • 仅用于学习目的。我有一些外部文件要参数化,我的目标是只使用 maven 完成工作。我知道这不是正确的工具,但我想强制项目配置完成额外的琐事。无论如何,我研究 maven 的主要原因是在构建时参数化源代码,即在构建时解析变量,例如 -D 在启动时。不确定使用预处理是否可行
  • 如果您确实需要对源代码进行一些参数化,有更好的解决方案,例如:mojohaus.org/templating-maven-plugin
  • 是的,我在 java 源代码中使用了 template-maven-plugin。但我需要参数化一些数据文件,这就是我使用资源插件的原因。

标签: java maven deployment


【解决方案1】:

执行特定 maven 目标的完整命令语法是:

mvn <plugin>:<goal>@<executionId>

不指定executionId时,默认等于default-cli

现在您执行 mvn resources:copy-resources 相当于 mvn resources:copy-resources@default-cli 但您的 pom.xml 没有为此目标定义此执行 ID。所以它抱怨一些参数丢失并且没有配置好。

当您将执行 ID 定义为 copy-resources 时,这意味着您应该执行:

mvn resources:copy-resources@copy-resources

或者当您将此目标绑定到 validate 阶段时,这意味着您也可以简单地执行它:

mvn validate

【讨论】:

  • 这根本不回答问题...
  • 为什么?你能解释一下吗?当他执行mvn resources:copy-resources 时,我正在回答 OP 的问题why maven did not parse the configuration directives in the plugin。然后我建议他应该使用什么命令,以便他在 pom.xml 中配置的配置生效
  • 首先你没有看标题:changing destination folder of resources in maven...其次The parameters 'resources', 'outputDirectory' for goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:copy-resources are missing or invalid -&gt; [Help 1]这个错误信息表明问题不在于命令行调用并且你没有给出任何建议如何改变……此外,使用对插件目标的显式调用通常不是一个好主意……最好将其保留在生命周期中……
  • 好吧。是还是不是。需要 OP 澄清他想做什么。但在我看来,他只是想使用maven-resources-plugin 将文件从模板文件夹复制到另一个目标文件夹,他在问为什么执行mvn resources:copy-resources 在这个pom.xml 下不起作用,并询问如何更改目标文件夹template 文件夹中的文件将被复制到其中。我认为我的回答应该对他第一个问题有所帮助。谢谢。
  • 我的目标是更改默认目录结构并在 java 源文件和资源文件中应用一些参数替换。我使用 templating-maven-plugin 管理 java 代码中的参数替换,但我无法完成资源工作。那是我第一次使用 maven,我只阅读了手册的介绍部分。我需要更深入地了解手册以了解构建生命周期和阶段。你的回答很有帮助。非常感谢您的帮助和时间。
猜你喜欢
  • 2018-12-28
  • 2017-05-23
  • 2014-01-08
  • 1970-01-01
  • 2018-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-09
相关资源
最近更新 更多