Resources not copied to output path in IntelliJ 12

2019-02-04 12:32发布

问题:

I seem to have done something to tell IntelliJ to not copy source resources (e.g. XML and property files) to the compiler output folder.

Resources are not being copied to the Compiler output path. Test resources are copied to the test ouput folder, but no source resources are copied.

Source folder: src

(this is C:\dev\myproject\src and contains XML files)

Test Source folder: tests\integration\src

(this is C:\dev\myproject\tests\integration\src and contains XML files)

Compiler output:

  • Use module compile output path
  • Output path: C:\dev\myproject\build\classes
  • Test output path: C:\dev\myproject\build\test

Settings -> Compiler -> Resource patterns: ?*.properties;?*.xml;?*.gif;?*.png;?*.jpeg;?*.jpg;?*.html;?*.dtd;?*.tld;?*.ftl

This is preventing me from running integration tests which load files from the classpath. (I do not have full control over the structure of this legacy project and most of the other developers use Eclipse.)

Can anyone give me some pointers as to what I need to do in order to have IntelliJ copy the resource to the output folders?

回答1:

i had same problems with IntelliJ IDEA 13 using Maven.

i solved it by adding this to my build tag in the pom.xml file:

<build>
...
<resources>
  <resource>
    <directory>src/com</directory>
    <targetPath>com</targetPath>
    <includes>
      <include>**/*.xml</include>
    </includes>
  </resource>
</resources>
...
</build>

change path according to your project. More about the Maven Resources Plugin here.



回答2:

I have faced the same issue. Idea seems to have a bug in projects with maven dependencies.

The workarounds I could found so far:

  1. Disable the external build.
  2. Generate an ant build script (Idea does this for you) and compile with that script.

Once you have successfully created your artifacts with either way, Idea continues to use them until the next maven dependency change.



回答3:

When using Gradle, try adding the resources to the sourceSets (use below code when you have the resource files in the same folder as the source files, otherwise adapt the path)

sourceSets {
    main {
        java {
            srcDirs 'src'
        }
        resources {
            srcDirs 'src'
        }
    }
    test {
        java {
            srcDirs 'test'
        }
        resources {
            srcDirs 'test'
        }
    }
}

Refer to How do I add resources to sourceSet with gradle?



回答4:

In the case of jpa when you add a jpa facet, Intellij creates:

META-INF/persistence.xml under src/main/java

But it does not update the build

Added this to pom.xml to fix the problem:

<resource>
  <directory>src/main/java</directory>
  <includes>
     <include>**/*.xml</include>
  </includes>
  <filtering>true</filtering>
</resource>