Maven 安装生命周期阶段不调用资源阶段

Posted

技术标签:

【中文标题】Maven 安装生命周期阶段不调用资源阶段【英文标题】:Maven install lifecycle phase does not call the resources phase 【发布时间】:2015-11-20 04:39:49 【问题描述】:

我用 maven 创建了一个 java 项目。在我的项目中(在 src/main/resources 下)有一些资源文件我想复制到目标/类中。

我将这些行添加到我的 pom xml 中:

<build>
     <resources>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.*</include>
            </includes>
        </resource>
    </resources>
</build>

当我通过命令行运行 mvn resources:resources 时,我得到资源文件被复制到目标/类中(因此生命周期中的资源阶段正在工作)。但是,当我在 cmd 中输入 mvn install:install 时,资源文件不会被复制到目标/类中。

我明白了:

[INFO] 
[INFO] --- maven-install-plugin:2.4:install (default-install) @ mqm-data-population ---
[INFO] Installing C:\xxx\xlation\pom.xml to C:\Users\xxx\xon-12.50.14-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.278 s

生命周期中的安装阶段似乎没有调用资源阶段或类似的东西..

我认为它可能与资源插件有关,所以我添加了:

    <build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.7</version>
        </plugin>
    </plugins>

    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.*</include>
            </includes>
        </resource>
    </resources>
</build>

这也没有帮助....

有人可以帮忙吗? 谢谢。


解决了


在网上快速搜索后,我发现了为什么资源文件没有被复制到我的目标/类中。我需要使用 maven 资源插件,并指出我希望将资源复制到目标/类中的阶段(在我的例子中,是“安装”阶段……)。

看过这里之后: https://maven.apache.org/plugins/maven-resources-plugin/examples/copy-resources.html

我将它添加到我的 pom.xml 中,并且正在运行……

【问题讨论】:

你想看看我的POM in this answer吗?快速浏览后,您的 sn-p 对我来说是正确的(我从 IDE 运行 Maven,我认为它等于 mvn install)。 你试过只运行mvn install吗? 首先,如果您调用mvn install 而不是mvn install:install,则不需要定义默认值src/main/resources,这是完全不同的。第一个调用包括资源部分的生命周期,而第二个只是调用不运行生命周期的适当插件的目标。而且您不需要资源插件,因为它是default life cycle 的一部分。 我使用 maven 和 intellij。如何使用默认安装(您提到的第一个选项)? 【参考方案1】:

可能您的默认配置文件未激活,通过添加 true 来修改它

<profiles>
<profile>
<id>development</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>

【讨论】:

以上是关于Maven 安装生命周期阶段不调用资源阶段的主要内容,如果未能解决你的问题,请参考以下文章

Maven 构建生命周期

Maven专题3——生命周期与插件

Maven实战生命周期

maven入门maven的生命周期

Maven实战生命周期

笔记:Maven 生命周期与命令行详解