64

I am using Weld as CDI implementation. My integration test, that tries to assemble object graph instantiating Weld container works well, when I have empty beans.xml in src/test/java/META-INF/beans.xml. Here is that simple test:

public class WeldIntegrationTest {
    @Test
    public void testInjector() {
        new Weld().initialize();
        // shouldn't throw exception
    }
}

Now when I run mvn clean install, I always get: Missing beans.xml file in META-INF!

My root folders are "src" and "web" which contains WEB-INF folder, but I also tried to use default maven structure and renamed "web" to "webapp" and moved it to src/main. I tried all the reasonable locations I could thought of:

 - src/main/java/META-INF/beans.xml
 - src/test/java/META-INF/beans.xml
 - web/WEB-INF/beans.xml
 - src/main/webapp/WEB-INF/beans.xml
 - src/main/webapp/META-INF/beans.xml
 - src/main/webapp/META-INF/(empty) and src/main/webapp/WEB-INF/beans.xml

Nothing works so far :/

maba
  • 47,113
  • 10
  • 108
  • 118
Xorty
  • 18,367
  • 27
  • 104
  • 155
  • In my CDI project the beans.xml is in src\main\webapp\WEB-INF, and in my war file it is in WEB-INF dir also. – kaos Oct 24 '12 at 19:49

3 Answers3

116

For EJB and JAR packaging you should place the beans.xml in src/main/resources/META-INF/.

For WAR packaging you should place the beans.xml in src/main/webapp/WEB-INF/.

Remember that only .java files should be put in the src/main/java and src/test/java directories. Resources like .xml files should be in src/main/resources.

maba
  • 47,113
  • 10
  • 108
  • 118
  • 1
    In my case I'm testing cdi-unit (uses Weld) I have to put it in my src – dalvarezmartinez1 Jan 10 '14 at 15:36
  • @dalvarezmartinez1 I bet you could move beans.xml to /src/main/resources in your case; it should end up in the 'classes' folder after building – Jon Onstott Aug 31 '15 at 21:57
  • @JonOnstott You are right. Actually right now it's in src/main/webapp/WEB-INF but this is just because we're using gradle, and the war plugin expects it in this location by default. I would say it all depends of the tool which packages the war/jar. – dalvarezmartinez1 Sep 04 '15 at 09:38
  • For me whether `jar or war` I had to put it in the `/src/main/resources/WEB-INF`. Anyways thanks – Jose Mhlanga Mar 28 '20 at 13:33
14

Just to complement the above answer, here is an official reference on this: https://docs.oracle.com/javaee/6/tutorial/doc/gjbnz.html

quote:

An application that uses CDI must have a file named beans.xml. The file can be completely empty (it has content only in certain limited situations), but it must be present. For a web application, the beans.xml file must be in the WEB-INF directory. For EJB modules or JAR files, the beans.xml file must be in the META-INF directory.

99Sono
  • 3,554
  • 27
  • 39
  • 8
    In Java EE 7 `beans.xml` is no longer mandatory. See [Oracle Java EE Tutorial](https://docs.oracle.com/javaee/7/tutorial/cdi-basic013.htm) – José Andias Apr 05 '16 at 10:08
0

http://www.javamonamour.org/2017/11/cdi-in-java-se-8.html here you can see where I have put it with success, that is under src/META-INF . The post contains a full working example of CDI in Java SE.

Pierluigi Vernetto
  • 1,954
  • 1
  • 25
  • 27