0

I have xml files in eclipse project's source directory, like:

src/java/main/com/xx/zz.xml

1.When using eclise to build automatically, xml files are copied to target/classes.

2.When using 'mvn complie', xml files are not copied to target/classes.

For the second case, I found this:why xml files in eclipse project's source directory is not copied to target/classes directory?.

but for the first case, I cannot find any document. Can someone explain it for me ?

Thanks!

Community
  • 1
  • 1
bylijinnan
  • 756
  • 3
  • 11
  • 27

3 Answers3

1

If you right click on the project in eclipse and select 'properties', then Java Build Path you see an input at the bottom for the Default Build Path, which should be target/classes. The source folders are shown in the main dialogue. If you click on the source folders then you can modify each, to exclude the xml files (if that is what you want to do).

Maven will include your xml files automatically if you put them in src/main/resources.

robjwilkins
  • 5,462
  • 5
  • 43
  • 59
  • Indeed, it is a maven convention to have non-Java resource files under `src/main/resources/`. Move the XML files there. Java should be under `src/main/java/`. – Joop Eggen Jul 15 '15 at 13:55
1

Eclipse works quite a bit differently than standalone Maven. Maven uses javac from JDK. By default javac only processes .java files and generates .class files in the same directory as .java sources. Maven asks it to generate classes in a separate directory and javac only moves .class files there.

The reason for this is that javac gives you more freedom in organizing your source files than most developers use. For instance javac does not care if your class is located in a folder structure that mimics declared packages. You can put a module together by putting several .java files along with some other files like .properties or .xml in the same folder. Your .java files can have different package declarations. For instance you can have files A.java:

package aaa.bbb;
class A {}

and B.java:

package zzz.uuu;
class B {}

If you ask javac to put classes in a target directory, it will create necessary subfolders for .class files, that will resemble the package structure. However it cannot do so for properties and xml files, because they do not contain package declarations. Such 'resource' management is left for the build tool.

Maven expects that you put such resources in another source folder (resources). They will be copied to generated package structure, so you should match it in resource folder. This is why it does not copy non-java files in source folders.

Eclipse expects you to put even .java files in a target package structure and complains if your package declaration does not reflect relative path of the file. This is where you have less freedom compared to bare javac. Thanks to this rule it can copy resources along with .class files and does not need another 'resource' folder.

You can configure Eclipse to use source folder as output folder. Eclipse will not touch resources in this case.

Michał Grzejszczak
  • 2,599
  • 1
  • 23
  • 28
0

If you don't want to have xml files in build directory, you need to configure eclipse excluded source file types - right-click on the file in the Project Explorer, choose Resource Configurations > Exclude from Build and choose the configurations that you want.