简体   繁体   English

m2eclipse 不将 src/main/resources 复制到目标

[英]m2eclipse not copying src/main/resources to target

I have a maven project and I have installed m2eclipse.我有一个 Maven 项目并且我已经安装了 m2eclipse。 When I build the project, the files inside src/main/resources are not getting copied into target/classes folder.当我构建项目时,src/main/resources 中的文件没有被复制到 target/classes 文件夹中。 Any help is highly appreciated.任何帮助都受到高度赞赏。

If resources are not copied could because "resources" are not defined properly.如果资源没有被复制,可能是因为“资源”没有正确定义。 You need to indicate to Maven what a resources is.您需要向 Maven 指明资源是什么。

<build>
    ...
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
        <!-- you can define here more than one resource -->
    </resources>
    ...
 </build>

After reading all your comments about when resources get copied and when they don't (ie, everything works fine until Eclipse build gets called), it's very possible that maven2Builder is disabled.在阅读了您关于何时复制资源以及何时不复制的所有评论后(即,在调用 Eclipse 构建之前一切正常),很可能maven2Builder被禁用。 You could have maven2Nature which neatly configures your classpath, but if you disable maven2Builder, only sources will be compiled and copied to target/classes (or whatever is configured in your .classpath ) due to active Java Builder.您可以使用 maven2Nature 巧妙地配置您的类路径,但是如果您禁用 maven2Builder,由于活动的 Java Builder,只有源代码将被编译并复制到target/classes (或.classpath配置的任何内容)。

Consider a very simple demo-project configuration ( .project file in Eclipse project root):考虑一个非常简单的演示项目配置(Eclipse 项目根目录中的.project文件):

<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
    <name>m2demo</name>
    <comment></comment>
    <projects>
    </projects>
    <buildSpec>
        <buildCommand>
            <name>org.eclipse.jdt.core.javabuilder</name>
            <arguments>
            </arguments>
        </buildCommand>
        <!-- 
        <buildCommand>
            <name>org.eclipse.m2e.core.maven2Builder</name>
            <arguments>
            </arguments>
        </buildCommand>
         -->
    </buildSpec>
    <natures>
        <nature>org.eclipse.jdt.core.javanature</nature>
        <nature>org.eclipse.m2e.core.maven2Nature</nature>
    </natures>
</projectDescription>

In this example, I have commented maven2Builder which results in exactly the same behavior you are getting.在这个例子中,我已经评论了maven2Builder这导致你得到的行为完全相同。

I usually like to see the actual .project and .classpath file contents, but it all can be accessed via Project Properties.我通常喜欢查看实际的.project.classpath文件内容,但它们都可以通过项目属性访问。 You will find a 'Builders' section in you project properties, and there should be 'Maven Project Builder' checkbox item.您会在项目属性中找到“Builders”部分,并且应该有“Maven Project Builder”复选框项。

In my project I observed that even if I do specify the resources tag in the pom.xml and configure the maven2Builder the resources are still not copied over.在我的项目中,我观察到即使我在 pom.xml 中指定了资源标签并配置了 maven2Builder,资源仍然没有被复制。 To fix this I manually edited the .classpath file (which exists at the top level of your project) which originally was为了解决这个问题,我手动编辑了 .classpath 文件(它存在于项目的顶层),它最初是

<classpathentry including="**/*.java" kind="src" output="target/classes" path="java_source">
    <attributes>
        <attribute name="optional" value="true"/>
        <attribute name="maven.pomderived" value="true"/>
    </attributes>
</classpathentry>

to be成为

<classpathentry kind="src" output="target/classes" path="java_source">
    <attributes>
        <attribute name="optional" value="true"/>
        <attribute name="maven.pomderived" value="true"/>
    </attributes>
</classpathentry>

Basically you have to remove the including="**/*.java" for non .class files to be copied over.基本上,您必须删除要复制的非 .class 文件的including="**/*.java"

For me, the problem was an obvious one... Project->Build Automatically was turned off.对我来说,问题很明显…… Project->Build Automatically 被关闭了。 The m2e builder which copies resources is called on build.复制资源的 m2e 构建器在构建时被调用。 Turn on automatic build or do a Project->Build All (ctrl-b) to deploy changed resources to target/classes.打开自动构建或执行 Project->Build All (ctrl-b) 以将更改的资源部署到目标/类。

I had this same issue and removed the maven nature from the project and added it back.我遇到了同样的问题,并从项目中删除了 Maven 性质并将其添加回来。 Then my resources files were copied correctly.然后我的资源文件被正确复制。

I also ran into the problem and it turned out that I had accidentally excluded copying resources with an m2e lifecycle mapping filter:我也遇到了这个问题,结果我不小心排除了使用 m2e 生命周期映射过滤器复制资源:

  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.eclipse.m2e</groupId>
          <artifactId>lifecycle-mapping</artifactId>
          <version>1.0.0</version>
          <configuration>
            <lifecycleMappingMetadata>
              <pluginExecutions>
                <pluginExecution>
                  <pluginExecutionFilter>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-resources-plugin</artifactId>
                    <versionRange>[1.0,)</versionRange>
                    <goals>
                      <goal>resources</goal>
                      <goal>testResources</goal>
                    </goals>
                  </pluginExecutionFilter>
                  <action>
                    <ignore />
                  </action>

The "action" should be "execute" instead ... “动作”应该是“执行”而不是......

I had that problem in the form that, while the resources essentially existed in the target folder, certain changes I had made recently were not available at runtime.我遇到的问题是,虽然资源基本上存在于目标文件夹中,但我最近所做的某些更改在运行时不可用。

I could solve it by executing Maven's "Update project ..." function.我可以通过执行 Maven 的“更新项目...”功能来解决它​​。

This flaw never occurred to me before (overtly) and so far I have no clue what's behind this.我以前从未(公开地)想到过这个缺陷,到目前为止我不知道这背后是什么。 It certainly can be puzzling (it took me quite a while to track down) if the defect leads to only subtle mistakes at runtime.如果缺陷在运行时导致细微的错误,那肯定会令人费解(我花了很长时间才找到)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM