简体   繁体   中英

m2eclipse not copying src/main/resources to target

I have a maven project and I have installed m2eclipse. When I build the project, the files inside src/main/resources are not getting copied into target/classes folder. 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.

<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. 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.

Consider a very simple demo-project configuration ( .project file in Eclipse project root):

<?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.

I usually like to see the actual .project and .classpath file contents, but it all can be accessed via Project Properties. You will find a 'Builders' section in you project properties, and there should be 'Maven Project Builder' checkbox item.

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. To fix this I manually edited the .classpath file (which exists at the top level of your project) which originally was

<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.

For me, the problem was an obvious one... Project->Build Automatically was turned off. The m2e builder which copies resources is called on build. Turn on automatic build or do a Project->Build All (ctrl-b) to deploy changed resources to target/classes.

I had this same issue and removed the maven nature from the project and added it back. 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:

  <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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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